代码如下所示:
private void buildStatCsv(List<DwsAccessEntity> list, String folder) {long currentTime = System.currentTimeMillis();String fileName = currentTime + 34;.csv";File file = new File(folder, fileName);try (FileOutputStream fos = new FileOutputStream(file);OutputStreamWriter osWriter = new OutputStreamWriter(fos, StandardCharsets.UTF_8);BufferedWriter bw = new BufferedWriter(osWriter, 1024)) {// 写headerbw.write("统计韶光,用户,访问次数");for(DwsAccessEntity item: list) {String line = item.getStartTime() + "," + entity.getUserName() + "," + entity.getAccessCount();bw.rrite(line + "\r");}} catch(Exception e) {log.error("write csv error");}}
为理解决在微软下的乱码显示问题,加入以下代码
重新运行项目,点击下载导出,在wps和office下导出的csv文件都能正常显示出csv的头部信息,不会再显示出乱码。
通过Java导出csv适宜百万级以下数据,如果要导出百万级以上数据,请参考
Java通过clickhouse-client导出csv,百万数据3s内导出