这个很多网站都有,找了一个,感觉好用:
StringqueryResult=...;
response.setContentType("application/csv");
response.setHeader("Content-Disposition","inline;filename="result.csv"");
PrintWriterout=newPrintWriter(newOutputStreamWriter(
response.getOutputStream(),"UTF-8"));
out.print(queryResult);
out.close();
但我自己在做的时候遇到一个实际问题,因为我要导出的结果中有时间,它的格式是这样的2007-09-01 07:00:00,用notepad打开的话肯定没问题,但如果用excel打开就有点问题了,时间段会变成####,因为excel的时间格式是01/09/2007 07:00:00。所以要在导出前处理下这个string。
privatestaticfinalDateFormatformatVLS=newSimpleDateFormat("yyyy-MM-ddHH:mm");
privatestaticfinalDateFormatformatCSV=newSimpleDateFormat("dd/MM/yyyyHH:mm");

privateStringconvertTimeForCSV(Stringinput)throwsVLSException...{
try...{
returnformatCSV.format(formatVLS.parse(input));
}catch(ParseExceptione)...{
//TODOAuto-generatedcatchblock
thrownewVLSException(VLSConstants.ERROR_FORMAT);
}
}
本文介绍了一种解决CSV文件中特定时间格式与Excel兼容性问题的方法。通过使用Java SimpleDateFormat类进行日期格式转换,确保导出的数据能在Excel中正确显示。
1942

被折叠的 条评论
为什么被折叠?



