Broken encoding in Excel report from Java - java

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.

Related

sending bytes as pdf

EDIT: The data I wanna send is in an rds. I fetch that into an output stream and then try to send it over as a pdf to the user.
The file gets generated but on opening the browser shows 'Failed to load PDF document.'
I have read that setting the ContentType to "application/pdf" helps but it does not in my case. The code is given below
byte[] b = generateFileService
.getDeviceHumidityRecordByPeriod(deviceIdValue, parseUnixTimestamp(startTime), parseUnixTimestamp(endTime));
OutputStream output = response.getOutputStream();
response.setContentType("application/pdf");
response.addHeader("Content-disposition", "attachment;filename=test.pdf");
output.write(b);
output.close();
response.flushBuffer();
}
if I change the file name to test.csv and then use content type as txt/plain, it works perfectly and a csv file is written.
I used Apache PDFBox to write data into a pdf file. The page offset needs to be tracked to add pages dynamically. Then you can convert the pdf into bytes and send it to the client by specifying response.setContentType("application/pdf") and response.addHeader("Content-disposition", "attachment;filename=test.pdf")

Excel download not working after populating the values in java

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

Jasper report pdf has wrong name when downloaded in java

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.

can not export excel successfully in weblogic 11 + linux

<%
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.

Jasper report pdf name problem

I am creating pdf report in spring mvc 3 using dynamic jasper report. I am setting these headers before writting report to stream.
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
response.setContentType("application/pdf");
Report is generated and display correctly in browser but it misses its name when I try to save it, I set name here fileName.
response.setHeader("Content-Disposition", "inline; filename=" + fileName);
What could be the issue.
With attachment, the file will be served with the provided name properly. When inline, browsers seem to ignore filename, and usually give the servletname part of the URL as default name when saving the inline contents.
You could try mapping that URL to an appropriate filename, if that is suitable. For example, with <servlet-mapping>. I'm not familiar with spring mvc, so maybe there's an equivalent.
Here's a SO related question: Securly download file inside browser with correct filename
You may also find this link useful: Filename attribute for Inline Content-Disposition Meaningless?

Categories