实现多个csv文件导出

public R<Boolean> exportCsv(HttpServletResponse response, @RequestBody DrQueryDto queryDto) throws Exception {
     //解决excel打开csv文件中文乱码
     //
byte[] utf8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
     String encoding = "GBK";
Integer countyId = queryDto.getCountyId();
String yearAnnual = queryDto.getYearAnnual();
        String url = "E:\\" + queryDto.getCountyName() + "\\";
        String urlCounty = "E:\\" + queryDto.getCountyName();
        File fileCounty = new File(urlCounty);
        if (!fileCounty.exists()) {
            fileCounty.mkdir();
        }
        File fi = new File(url);
        if (!fi.exists()) {
            fi.mkdir();
        }
        Map<String, List<String[]>> exportData = workService.exportCSV(countyId, yearAnnual);
        Set<String> keySet = exportData.keySet();
        for (String s : keySet) {
            String csvFileName = s + ".csv";
            File file = new File(url + csvFileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(url + csvFileName);
       //fileOutputStream.write(utf8bom);
       CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(fileOutputStream,encoding));
String[] title = {"name", "value"}; // 添加第一行到CSV文件 csvWriter.writeNext(title,false); List<String[]> list = exportData.get(s); if (CollectionUtils.isNotEmpty(list)) { for (int i = 0; i < list.size(); i++) {
            //false:格式不包含双引号,true:包含双引号 默认值为false
csvWriter.writeNext(list.get(i),
false); } } csvWriter.close(); } return R.ok(true); }

 

posted @ 2024-02-22 09:25  懂得归零  阅读(18)  评论(0编辑  收藏  举报