public static void exportCSV(HttpServletResponse response, String text, String filename) {
// text为要导出的内容,一般来说用text由StringBuffer拼接而成,使用,分隔列、使用\r\n换行
//filename为(xx.csv)
response.setCharacterEncoding("UTF-8");
// 设置文件的名称和格式
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }));
osw.write(text);
osw.flush();
osw.close();
} catch (Exception e) {
// LOGGER.error("导出文件文件出错:{}",e);
} finally {
try {
osw.flush();
osw.close();
} catch (Exception e) {
// LOGGER.error("关闭流对象出错 e:{}",e);
}
}
}
Java 将文本导出为.csv文件
最新推荐文章于 2024-04-26 12:04:39 发布