当前位置:   article > 正文

Java关于Excel文件的导入导出

Java关于Excel文件的导入导出

人生如梦 荣华富贵 如木槿之花 朝荣夕逝

在这里插入图片描述


需求

  • 导出: 能够将库表内的数据导出多个Excel表,并且生成一个压缩包,提供用户下载
  • 导入: 能够将一个压缩包内的多个Excel表解压,并获取表内的所有数据

在这里插入图片描述

FileUtils 工具类

public class UnitFileUtils {
    /**
     * @Description: 导入文件(返回文件全部数据)
    */
    public static Map<String, List<List<String>>> importCsv(File file) throws IOException {
        String tableName = file.getName().replace(".csv", "");
        Map<String, List<List<String>>> result = new HashMap<>();
        List<List<String>> dataList = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                String[] data = line.split(",");
                List<String> list = new ArrayList<>();
                for (int i = 0; i < data.length; i++) {
                    list.add(data[i]);
                }
                dataList.add(list);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            br.close();
        }
        result.put(tableName, dataList);
        return result;
    }
    
    /**
     * @Description: 导出文件
    */
    public static void exportCsv(List<List<String>> listData, String tableName, String path) {
        path = path + File.separator + tableName + ".csv";
        try (FileWriter writer = new FileWriter(path)) {
            for (List<String> list : listData) {
                StringBuilder rowData = new StringBuilder();
                for (int i = 0; i < list.size(); i++) {
                    if (i == list.size()) {
                        rowData.append(list.get(i));
                    } else {
                        rowData.append(list.get(i)).append(",");
                    }
                }
                writer.write(rowData.toString());
                writer.write(System.lineSeparator());
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
  • 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
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

Impl 实现类

    /**
     * @Description: 文件导入
     * multipartFile 文件
     */
    @Override
    public void importCsv(MultipartFile multipartFile) throws IOException, SQLException {
    
        // 获取导入文件数据
        List<Map<String, List<List<String>>>> fileDataList = transformImportCsv(multipartFile);
		...


    /**
     * @Description: 文件数据解析
    */
    public List<Map<String, List<List<String>>>> transformImportCsv(MultipartFile multipartFile) throws IOException, SQLException {
        // 获取临时文件夹对象
        File tempDir = new File(tempPath);
        // 如果文件夹不存在,则抛出异常
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
        //生成临时文件名称
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //通过multipartFile将压缩文件保存到临时路径
        String temp = FileUtils.saveZipFile(multipartFile, tempPath);
        //解压zip
        FileUtils.unzip(temp, tempPath + fileName);
        //读取解压文件
        File folder = new File(tempPath + fileName);
        File[] files = folder.listFiles();
        List<Map<String, List<List<String>>>> fileList = new ArrayList<>();
        Map<String, List<List<String>>> fileData = null;
        for (File file : files) {
            //遍历zip中的csv文件
            fileData = UnitFileUtils.importCsv(file);
            fileList.add(fileData);
        }
        //删除临时文件夹以及压缩文件
        FileUtils.deleteFileOrDir(temp);
        FileUtils.deleteFileOrDir(tempPath + fileName);
        return fileList;
    }


    /**
     * @Description: 导出文件
    */
    public void exportCsv(HttpServletResponse response, List<String> tableNames) {
        String dirName = tempPath + File.separator + UUID.randomUUID().toString().replace("-", "");
        // 获取临时文件夹对象
        File tempDir = new File(dirName);

        // 如果文件夹不存在,则创建
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
        //导出csv文件到指定文件夹
        for (String tableName : tableNames) {
            try {
            		List<List<String>> listData = new ArrayList<>();
            		...
            		
                UnitFileUtils.exportCsv(listData, "unit_sharding_version", path);
            } catch (Exception e) {
                throw new RuntimeException("导出表【" + tableName + "】失败!",e);
            }
        }
        //将文件夹压缩为压缩包
        FileUtils.zip(dirName, dirName + ".zip");
        //通过流返回
        FileUtils.doGet(dirName + ".zip", response);
        //删除临时文件夹以及压缩文件
        FileUtils.deleteFileOrDir(dirName);
        FileUtils.deleteFileOrDir(dirName + ".zip");
    }
  • 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
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/726057
推荐阅读
相关标签
  

闽ICP备14008679号