Jsp Generating Excel Spreadsheet (Xls) to Download

JSP generating Excel spreadsheet (XLS) to download

While you can use a full fledged library like JExcelAPI, Excel will also read CSV and plain HTML tables provided you set the response MIME Type to something like "application/vnd.ms-excel".

Depending on how complex the spreadsheet needs to be, CSV or HTML can do the job for you without a 3rd party library.

How to generate and download an Excel report using jsp servlet

you should add the following in your servlet method..

try{
//This is for downloading
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=nameOfExcel");
File file = new File("path_to_the_file/excelfile.xls"); //<- the name of excel that you have already created.
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();

byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();
}
catch(IOException e){
e.printStackTrace();
}

Java-Spring-JSP download xls file - POST OK, GET not working

Your GET request is working correctly.

This is what happens: When you do the POST variant, the browser itself handles the response with the attachment and asks you if you want to download/view the attachment.

When you run the GET variant, your javascript callback to $.ajax receives the response (and the browser does not intervene). And since it does nothing with the data, nothing happens (except that you toggle visibility of the indicator and button).

To handle your original problem (how to download without a form post) please read this question and it answers.

Create an excel file for users to download using Apache POI

Do the job in a normal servlet instead of a JSP file. A JSP file is meant for dynamically generating HTML code and is using a character writer for that instead of a binary output stream and would thus only corrupt your POI-generated Excel file which is in essence a binary stream.

So, basically all you need to do in the doGet() method of the servlet is the following:

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=filename.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
// ...
// Now populate workbook the usual way.
// ...
workbook.write(response.getOutputStream()); // Write workbook to response.
workbook.close();

Now, to download it, invoke the servlet by its URL instead of the JSP file.

How to let user download the data in database to an excel sheet file from web application in Java/Struts?

Just use a servlet. Feed response.getOutputStream() to POI HSSF to write the workbook to. Most important bit is the Content-Disposition response header. If you set it to attachment, then the browser will pop a Save As dialogue.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=name.xls");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...

Then let the download URL point to that servlet, if necessary with some request parameters or path info.



Related Topics



Leave a reply



Submit