In my application using spring and angularjs and java, On clicking a button ,there is an ajax call which fetches the data from db and the data needs to be written to an excel file and the same file needs to be downloaded in the browser itself. I am attaching the code snippet for the same .
Now the problem is, even though the data is being fetched and I am able to bind it as worksheet using poi , the excel file never comes in the browser as download.
kindly help me in finding a right solution. Thank You.
fileName.append(Calendar.get(Calendar.SECOND));
fileName.append(oCalendar.get(Calendar.MILLISECOND));
Sheet sheet = workbook.createSheet("Transaction" + fileName.toString());
CellStyle style = workbook.createCellStyle();
style.setFont(font);
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Name");
header.createCell(1).setCellValue("ACC");
header.createCell(2).setCellValue("Date");
header.createCell(0).setCellValue("TRANSACTION_RECONCILIATION_IDENTIFIER");
header.createCell(1).setCellValue("ORIGINAL_RECONCILIATION_IDENTIFIER");
header.createCell(2).setCellValue("STR_TRANSACTION_Date");
response.setHeader("Content-Disposition", "attachment; filename=\"TransactionDetails.xls\"");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
After writing the output stream you should use
response.flushBuffer();
Looks like you're missing the contentType definition. Before calling the write() method try setting the contentType like this:
response.setContentType("application/octet-stream");
This should tell the browser that a download is expected from the server. A relevant question was asked here: response.setContentType("APPLICATION/OCTET-STREAM")
In my code I write like this:
response.setContentType("application/octet-stream");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition",
"attachment; filename=TransactionDetails.xls");
workbook.write(response.getOutputStream());
Hope this helps
Related
I am using Grails.
I have a sample code to download an excel file:
XSSFWorkbook workbook = new XSSFWorkbook()
....
FileOutputStream out = new FileOutputStream(new File("C:\\excel.xlsx"))
workbook.write(out)
out.close()
Here the excel file will be downloaded automatically. I would want system to prompt the user to download the file in the browser window.
I tried using the below code:
response.setHeader("Cache-Control", "public")
response.setContentType("application/vnd.ms-excel")
response.setHeader('Content-Disposition', 'Attachment;Filename="excel.xlsx"')
ServletOutputStream outputStream = response.getOutputStream()
workbook.write(outputStream)
outputStream.flush()
outputStream.close()
Which doesn't work. How to achieve this?
Thanks in advance.
Quoted form W3C
Parameter values are normally case sensitive, but certain parameters are interpreted to be case- insensitive, depending on the intended use. (For example, multipart boundaries are case-sensitive, but the "access- type" for message/External-body is not case-sensitive.)
There for, try changing
response.setHeader('Content-Disposition', 'Attachment;Filename="excel.xlsx"')
to
response.setHeader('Content-Disposition', 'attachment;filename="excel.xlsx"')
I currently create xls files by outputting an HTML table with the proper header and file extension. Here is the sample code:
String tabledata = "<table>MORE TABLE HTML HERE</table>";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"somefile.xls\"");
out.println(tabledata);
This works great except you get an error before opening "The file format and extension of 'somefile.xls' don't match." Once opened, it looks exactly how I want it.
I used Apache POI elsewhere, so figured I could try using this, but I don't want to have to build all elements in some long script looking for rows, columns and different styles. I was thinking that I could convert the HTML to an InputStream and then create the Workbook, but it doesn't like the headers. Here is the code:
InputStream is = new ByteArrayInputStream(tabledata.getBytes("UTF-8"));
HSSFWorkbook wb = new HSSFWorkbook(is);
FileOutputStream fileOut = new FileOutputStream("somefile.xls");
wb.write(fileOut);
fileOut.close();
Is something like this possible if the headers were added/changed? If so, how would I modify this code to work properly?
I have an application(web one) where the users can view jasper reports. The only problem is that when they decided to save the pdf to their local computers, the pdfs have incorrect filenames. In Chrome they are named download.pdf and in Firefox they are named something like sgsgjsg.pdf.
What can I so in order for the files to be saved with the correct name ?
Note that I set the header type like this:
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"%s\"" + exportFile.getName());
inputFileInputStream = new FileInputStream(fullPathFile);
outputFileOutputStream = response.getOutputStream();
Put inside header filename the value what you needed.
For instance:
response.setHeader("Content-Disposition", "attachment; filename="\"download.pdf\"");
It is should works same for all browsers.
in my application user is able to download an excel report. When user downloads xls using FF then encoding is fine and umlauts are shown correct. If user does the same in IE and Chrome then umlauts in xls are broken.
Excel report is generated in a servlet using Apache POI v3.10.1 and filled with the same data every time. But the xls files downloaded in FF and IE (or Chrome) are different.
Any idea why?
BTW, I tried to set encoding in response header
response.setHeader("Content-Type", "application/vnd.ms-excel; charset=UTF-8");
but no success.
UPD 03.04. A piece of code from servlet:
OutputStream out = response.getOutputStream();
...
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls" + "\"");
...
workbook.write(out);
IE somehow cannot understand your response.setHeader("Content-Type", "application/vnd.ms-excel; charset=UTF-8");. Try this instead response.setContentType("application/vnd.ms-excel");. It works on my IE8 and FF35.
<%
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=\"" + "test.xls\"");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setContentType("application/vnd.ms-excel");
InputStream is = new FileInputStream(realPath);
//OutputStream outStream = response.getOutputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(is,
parameters, new JRBeanCollectionDataSource(pdfList));
JRAbstractExporter exporter = new JExcelApiExporter();
exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, out);
exporter.exportReport();
outStream.flush();
outStream.close();
out.clear();
out =pageContext.pushBody();
%>
we use the code above to generate an excel, and it works well in tomcat + windows, but after we upload to linux + weblogic server, the excel is corrupted. I use text editor to open the excel, I found it add several empty line in the excel, which caused the excel can not be open successfully, anyone can point me the right direction ? Why there are space ? How it comes ?
Thanks in advance !
I suspect that your use of pageContext.pushBody() may be the culprit.
pushBody is, as far as I know, meant for updating the output in the scope of a JSP tag.
When you are generating binary content such as an excel file, where you need to be absolutely sure that the intended bytes, and only the intended bytes reach the browser, you need to write those bytes, flush the output, and then make sure that nothing else gets written. By invoking pushBody(), you are making it so that more content can be written to the output, and any blank lines (and the carriage returns / line feeds between them) in the JSP page could be output.
All in all, I suggest that you just not do this in a JSP - do it in a Servlet instead.