大批量到处excel,防止内存溢出

2024-05-13 13:32

本文主要是介绍大批量到处excel,防止内存溢出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在做项目功能时 ,发现有20万以上的数据。要求导出时直接导出成压缩包。原来的逻辑是使用poi导出到excel,他是操作对象集合然后将结果写到excel中。

使用poi等导出时,没有考虑数据量的问题,大数据量无法满足,有个几千行jvm就哭了。更别提几万行几百万行数据了。

经过一天的研究发现一种不会消耗过多内存的方法:

导出成csv格式

大数据量的导出成csv格式分为以下几步:

1.首先引入需要的jar包 一下是我maven的配置方式

1

2

3

4

5

6

7

8

9

10

<dependency>

        <groupId>org.mvel</groupId>

        <artifactId>mvel2</artifactId>

        <version>2.2.8.Final</version>

    </dependency>

    <dependency>

        <groupId>net.sourceforge.javacsv</groupId>

        <artifactId>javacsv</artifactId>

        <version>2.0</version>

    </dependency>

 2.以下是具体的执行代码,我是用的是jdbcTemplate

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

public class DownloadVehicleRepair extends AbstractJob {

 

    @Autowired

    private JdbcTemplate jdbcTemplate;

 

    @Override

    protected void executeBusiness(Long aLong) {

        System.out.println("开始执行!!!!!!!!!!");

        final String fileName = "车辆维修清单.csv";//压缩包里面的文件

        final String[] header = {"序号""第三方机构代码""机构名称""分公司""合作机构""单位类别""主品牌""品牌名称",

                "被投诉""涉及欺诈""黑名单""审核状态""维护时间""维护人员代码"};

        final String sql = "您需要执行sql”;

 

        jdbcTemplate.execute(new PreparedStatementCreator() {

            @Override

            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement pstmt = connection.prepareStatement(sql);

                return pstmt;

            }

        }, new PreparedStatementCallback<Integer>() {

            @Override

            public Integer doInPreparedStatement(PreparedStatement preparedStatement) throws SQLException, DataAccessException {

                ResultSet rs = preparedStatement.executeQuery();

                try {

                    CsvUtil.writeCsv(RuntimeEnvironmentUtil.getValue(SysConstent.code,SysConstent.path) + "\\VehicleRepairDetail.zip",

                            fileName, header, rs);//RuntimeEnvironmentUtil.getValue()是为了获取你导出到服务器的路径

                catch (Exception e) {

                    e.printStackTrace();

                }

                return 0;

            }

        });

        System.out.println("导出完成!!!!!!!!!!!");

 

    }

}

 3.以下是帮助类

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

77

78

79

80

81

82

public class CsvUtil {

    // 编码类型

    public static final Charset CHARSET = Charset.forName("GBK");

 

    // 分隔符

    public static final char DELIMITER = ',';

 

    // 文件后缀

    public static final String SUFFIX = ".csv";

 

 

    public static void writeCsv(OutputStream out, String[] header, ResultSet rs)

            throws IOException, SQLException {

        CsvWriter writer = null;

        try {

            writer = new CsvWriter(out, CsvUtil.DELIMITER, CsvUtil.CHARSET);

            writeCsv(writer, header, rs);

        finally {

            if (writer != null)

                writer.close();

        }

    }

 

    public static void writeCsv(CsvWriter writer, String[] header, ResultSet rs)

            throws IOException, SQLException {

        if (header != null)

            writer.writeRecord(header);

        ResultSetMetaData md = rs.getMetaData();

        int columnCount = md.getColumnCount();

        while (rs.next()) {

            for (int i = 1; i <= columnCount; i++)

                writer.write(rs.getString(i));

            writer.endRecord();

        }

    }

 

    public static void writeCsv(File file, String[] header, ResultSet rs)

            throws IOException, SQLException {

        BufferedOutputStream out = null;

        FileOutputStream fileOutputStream = null;

        try {

            fileOutputStream = new FileOutputStream(file);

            out = new BufferedOutputStream(fileOutputStream);

            writeCsv(out, header, rs);

        finally {

            if (out != null) {

                out.flush();

                out.close();

            }

            if (fileOutputStream != null) {

                fileOutputStream.close();

            }

        }

    }

 

    public static void writeCsv(String csvFilePath, String[] header,

                                ResultSet rs) throws IOException, SQLException {

        writeCsv(new File(csvFilePath), header, rs);

    }

 

    public static void writeCsv(String zipFilePath, String csvName, String[] header, ResultSet rs)

            throws IOException, SQLException {

        FileOutputStream fos = null;

        BufferedOutputStream bos = null;

        ZipOutputStream zos = null;

        try {

            fos = new FileOutputStream(zipFilePath);

            bos = new BufferedOutputStream(fos);

            zos = new ZipOutputStream(bos);

            zos.putNextEntry(new ZipEntry(csvName));

            writeCsv(zos, header, rs);

        finally {

            StreamUtil.flush(zos);

            StreamUtil.close(zos);

            //StreamUtil.flush(bos);

            StreamUtil.close(bos);

            //StreamUtil.flush(fos);

            StreamUtil.close(fos);

        }

    }

 

}

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class StreamUtil {

    public static void flush(Flushable flushable) {

        if (flushable != null) {

            try {

                flushable.flush();

            catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    public static void close(Closeable closeable){

        if(closeable!=null){

            try {

                closeable.close();

            catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

}

 4.下面是下载时的action

1

2

3

4

5

6

7

8

9

10

@RequestMapping(value = "/downloadVehicleRepair", method = RequestMethod.POST)

   public ResponseEntity<byte[]> download() throws IOException {

       String path = RuntimeEnvironmentUtil.getValue(SysConstent.code,SysConstent.path)+"\\VehicleRepairDetail.zip";

       File file = new File(path);

       HttpHeaders headers = new HttpHeaders();

       String fileName = new String("车辆维修清单.zip".getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题

       headers.setContentDispositionFormData("attachment", fileName);

       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

       return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);

   }

 总结:以上只是关键代码。使用时只需要稍加改变就可以运行。

这篇关于大批量到处excel,防止内存溢出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/985841

相关文章

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

java内存泄漏排查过程及解决

《java内存泄漏排查过程及解决》公司某服务内存持续增长,疑似内存泄漏,未触发OOM,排查方法包括检查JVM配置、分析GC执行状态、导出堆内存快照并用IDEAProfiler工具定位大对象及代码... 目录内存泄漏内存问题排查1.查看JVM内存配置2.分析gc是否正常执行3.导出 dump 各种工具分析4.

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空