本文概览:介绍了直接PrintWrirter方法和CsvPrinter两种方法进行下载csv文件,推荐使用CsvPrinter。
1 直接使用response.getWriter
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@Controller public class CsvFileDownloadController { private Logger logger = LoggerFactory.getLogger(CsvFileDownloadController.class); private static final String NEW_LINE_SEPARATOR = "\n"; @Autowired private FeeActualStatisticsService feeActualStatisticsService; @RequestMapping(value = "/actualFee/downloadCSV") public void downloadCSV(String begDate, String endDate, Integer dimension, HttpServletResponse response) { try { // 1.设置response头部属性 // 1.1csv文件中文乱码,需要设置成gbk response.setCharacterEncoding("gbk"); response.setContentType("text/csv"); String reportName = new String(FeeStatisticsDimensionType.codeOf(dimension).getDes() + ".csv"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(reportName, "UTF-8")); // 2设置response数据 String[] FILE_HEADER = {"统计维度", "支付笔数", "交易金额", "费用", "费率"}; List<FeeTotalDailyVo> totalDailyVoList = feeActualStatisticsService.queryBankFeeTotalDailyVo(begDate, endDate, dimension); exportWithOutputStream(response, totalDailyVoList, FILE_HEADER); } catch (Exception e) { logger.error("下载文件失败,error={}", e.getMessage(), e); } } |
通过printWriter的方法如下,csv文件中内容以“逗号”隔开,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * 直接通过outputStream来实现 * * @param response * @param totalDailyVoList * @param header */ private void exportWithOutputSteam(HttpServletResponse response, List<FeeTotalDailyVo> totalDailyVoList, String[] header) { try { // 生成第一行 String headLine = "统计维度,支付笔数,交易金额,费用,费率"; PrintWriter printWriter = response.getWriter(); printWriter.println(headLine); // 生成数据 for (FeeTotalDailyVo vo : totalDailyVoList) { StringBuilder contentLine = new StringBuilder(); contentLine.append(vo.getName()).append(","); contentLine.append(vo.getTotalTrans()).append(","); contentLine.append(vo.getTotalAmount()).append(","); contentLine.append(vo.getTotalFeeActual()).append(","); contentLine.append(vo.getAveRateActual()); printWriter.println(contentLine.toString()); } printWriter.flush(); } catch (Exception e) { logger.error("使用outputStream下载文件失败,errMessage=" + e.getMessage(), e); } } |
2 使用apach的CsvPrinter
定义exportWithCsvPrinter来替换上面的exportWithOutputStream方法,就可以了。
1、maven
1 2 3 4 5 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.1</version> </dependency> |
2、代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/** * 使用apache的CsvPrinter * * @param response * @param totalDailyVoList */ private void exportWithCsvPrinter(HttpServletResponse response, List<FeeTotalDailyVo> totalDailyVoList, String[] header) { CSVPrinter csvFilePrinter = null; //创建 CSVFormat CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR); try { csvFilePrinter = new CSVPrinter(response.getWriter(), csvFileFormat); //创建CSV文件头 csvFilePrinter.printRecord(header); // 写入数据 for (FeeTotalDailyVo vo : totalDailyVoList) { List<String> records = Lists.newArrayList(); // 维度名字 records.add(vo.getName()); // 支付笔数 records.add(vo.getTotalTrans().toString()); // 支付金额 records.add(vo.getTotalAmount()); // 支付费用共 records.add(vo.getTotalFeeActual()); // 支付费率 records.add(vo.getAveRateActual()); csvFilePrinter.printRecord(records); } } catch (Exception e) { try { csvFilePrinter.close(); } catch (Exception ce) { } } } |
(全文完)