// 添加super-csv依赖
net.sf.supercsv
super-csv
2.4.0
// 后端controller层
@RequestMapping(value = "/apps/boot-percentage", method = RequestMethod.GET)
public void bootPercentage(HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/csv");
String current = new Date().toString().replace(" ", "-");
String filename = URLEncoder.encode("SpringBoot应用占比统计表-" + current + ".csv", GlobalConstants.UTF_8);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
// 防止乱码出现
Writer writer = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8);
// 写入字节流,让文档以UTF-8编码
writer.write('\uFEFF');
ICsvBeanWriter csvWriter = new CsvBeanWriter(writer, CsvPreference.STANDARD_PREFERENCE);
String[] header = {"groupName", "backEndSpringBootAppsTotal", "backEndAppsTotal", "springBootPercentage"};
csvWriter.writeHeader(header);
List bootPercentageList = appsService.bootPercentage(); // 这是一个业务代码 返回我要导出去的数据
for (BootPercentageResp bootPercentage : bootPercentageList) {
csvWriter.write(bootPercentage, header);
}
csvWriter.close();
}
// 前端
Q1: 没有中文标题
/*
本来csv就是以 ,进行分割单元格 csvWriter.writeComment(""); 这个可以写入行
文档地址:[http://super-csv.github.io/super-csv/apidocs/index.html](http://super-csv.github.io/super-csv/apidocs/index.html)
*/
Map> upgradeInfoList = appsService.upgradeInfo();
for (String groupName : upgradeInfoList.keySet()) {
csvWriter.writeComment(groupName);
csvWriter.writeComment("应用名称,上线时间");
for (UpgradeInfoResp upgradeInfoResp : upgradeInfoList.get(groupName)) {
csvWriter.write(upgradeInfoResp, header);
}
csvWriter.writeComment("");
}
Q2: 没有好看的间距