boot spring 解析csv_Spring boot with csv

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 Async​zhuanlan.zhihu.comnetkiller:Springboot with Ethereum (web3j)​zhuanlan.zhihu.comnetkiller:Spring boot with Data restful​zhuanlan.zhihu.comnetkiller:Spring boot with Git version​zhuanlan.zhihu.comnetkiller:Spring boot with ELK​zhuanlan.zhihu.comnetkiller:Spring boot with Apache Kafka​zhuanlan.zhihu.comnetkiller:Spring boot with RabbitMQ(AMQP)​zhuanlan.zhihu.comnetkiller:Spring boot with Hessian​zhuanlan.zhihu.comnetkiller:Spring boot with Scheduling​zhuanlan.zhihu.comnetkiller:Spring boot with Email​zhuanlan.zhihu.comnetkiller:Spring boot with Caching​zhuanlan.zhihu.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值