Spring boot with csv多维度架构 - 知乎www.zhihu.com
下面是一个导出 CSV 文件的例子
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
response.setContentType("application/csv");
// response.setContentType("application/csv;charset=gb18030");
response.setHeader("Content-Disposition", "attachment; filename=\"file.csv\"");
BufferedWriter writer = new BufferedWriter(response.getWriter());
// 需要写入 utf8bom 头否则会出现中文乱码
// byte[] uft8bom = { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
String bom = new String(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
writer.write(bom);
writer.write("A,B,C");
writer.newLine();
tableRepository.findAll().forEach(table -> {
try {
String tmp = String.format("%s,%s,%s", table.getId(), table.getMethod(), table.getMoney());
writer.write(tmp);
writer.newLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
writer.flush();
writer.close();
}netkiller:Spring boot with Asynczhuanlan.zhihu.comnetkiller:Springboot with Ethereum (web3j)zhuanlan.zhihu.comnetkiller:Spring boot with Data restfulzhuanlan.zhihu.comnetkiller:Spring boot with Git versionzhuanlan.zhihu.comnetkiller:Spring boot with ELKzhuanlan.zhihu.comnetkiller:Spring boot with Apache Kafkazhuanlan.zhihu.comnetkiller:Spring boot with RabbitMQ(AMQP)zhuanlan.zhihu.comnetkiller:Spring boot with Hessianzhuanlan.zhihu.comnetkiller:Spring boot with Schedulingzhuanlan.zhihu.comnetkiller:Spring boot with Emailzhuanlan.zhihu.comnetkiller:Spring boot with Cachingzhuanlan.zhihu.com