我正在使用 JSF 动态/运行时生成 CSV 文件,代码如下
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();
以下是打开时文件的内容
Disconnect Time Calling Number
1/6/2010 1:00 4.47744E+11
实际预期结果将是完整的日期格式 wrt AM/PM 和完整长度的数字。
我已经尝试过给定Excel CSV - 数字单元格格式和其他一些添加空格的双引号示例,但它们没有用。用户还将对数字列执行算术运算。
提前致谢。