I am generating a CSV file on the fly/runtime with JSF and the code is as follows
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = HttpServletResponse)context.getExternalContext().getResponse();
int read = 0;
byte[] bytes = new byte[1024];
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
ServletOutputStream os = null;
StringBuffer stringBuffer1 = new StringBuffer("");
stringBuffer1.append("Disconnect Time");
stringBuffer1.append(',');
stringBuffer1.append("Calling Number");
stringBuffer1.append("01/06/2010 01:00:35 AM");
stringBuffer1.append(", ");
stringBuffer1.append("447744369900");
ByteArrayInputStream bis1;
try {
bis1 = new ByteArrayInputStream(stringBuffer1.toString().getBytes("UTF-8"));
os = response.getOutputStream();
while ((read = bis1.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
os.flush();
os.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
and below is the content of the file when opened
Disconnect Time Calling Number
1/6/2010 1:00 4.47744E+11
The Actual expected result will be complete date format w.r.t AM/PM and number in complete length.
I have already tried example of double quotes as given Excel CSV – Number cell format and some other of adding blank space but they did not worked. Also the user will be performing arithmetic operation on the number columns.
Thanks in advance.
I’m no JSF expert but this seems entirely related to how Excel reads this information.
I tried playing a bit with the data, but could not make it do what you want it to do in CSV.
I created a sample CSV file with this content (using Nopepad++):
And when I viewed it using Excel it gave me the same output you got – so the question you are facing is related to showing CSV in Excel – and not JSF.