首页 > 其他分享 >百万数据导出

百万数据导出

时间:2023-08-14 20:55:32浏览次数:28  
标签:spring writeDataRows boot totalCount com 导出 org 数据 百万

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xiaoteng</groupId>
    <artifactId>excelexportdemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 集成mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <!-- 集成mysql连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--分页插件-->
        <!-- pagehelper 插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>
        <!--导出-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>
    </dependencies>
</project>

配置application.yml

 

 

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/shiyan?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
server:
  port: 9000
#  mybaits
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.xiaoteng.demo.entity

启动类

package com.xiaoteng.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.xiaoteng.demo.mapper")
public class AppRun {
    public static void main(String[] args) {
        SpringApplication.run(AppRun.class,args);
    }
}

对应导出方法

@RestController
@RequestMapping("/test")
public class EexcelPortCtroller {

    @Resource
    private ExcelPortService excelPortService;
    @Resource
    private ExcelPortMapper excelPortMapper;


    @GetMapping("/hello2")
    public void test2(HttpServletResponse response) throws Exception {
        // 导出开始时间戳
        long start = System.currentTimeMillis();
// 文件名称 String fileName = URLEncoder.encode(String.format("%s-(%s).xlsx", "百万数据导出", UUID.randomUUID().toString()), StandardCharsets.UTF_8.toString()); ExcelWriter writer = new ExcelWriterBuilder() .autoCloseStream(true) .excelType(ExcelTypeEnum.XLSX) .file(response.getOutputStream()) .head(Product.class) .build(); // xlsx文件上上限是104W行左右,这里如果超过104W需要分Sheet int lastBatchMaxId = 0; //相当于页数 int writeDataRows = 100000; //每次查10W条 Integer sheetDataRows = 1000000; //每个sheet写100W条数据 Integer totalCount = excelPortMapper.count(); // 一个sheet得循环次数 int oneSheetWriteCount = totalCount / writeDataRows; //计算最后一个sheet需要写入的次数 Integer lastSheetWriteCount = totalCount % sheetDataRows == 0 ? oneSheetWriteCount : (totalCount % sheetDataRows % writeDataRows == 0 ? (totalCount / sheetDataRows / writeDataRows) : (totalCount / sheetDataRows / writeDataRows + 1)); for (int i = 0; i < 2; i++) { WriteSheet writeSheet = new WriteSheet(); writeSheet.setSheetName("测试Sheet" + i); // Sheet sheet = new Sheet(i, 0); // sheet.setSheetName("测试Sheet1" + i); for (int j = 0; j <(i!=2-1?oneSheetWriteCount:lastSheetWriteCount) ; j++) {
// 分页插件,没有就用limit分页 PageHelper.startPage(j + 1 + oneSheetWriteCount * i, writeDataRows); // List<Product> list = excelPortService.findAllById(lastBatchMaxId, writeDataRows); List<Product> list = excelPortService.findAll(); writer.write(list, writeSheet); } } response.setContentType("application/force-download"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); response.setCharacterEncoding("utf-8"); writer.finish(); // log.info("导出数据耗时:{} ms,start:{},end:{}", System.currentTimeMillis() - start); long time = System.currentTimeMillis() - start; System.out.println("导出数据耗时:" + time/1000); } }

 

标签:spring,writeDataRows,boot,totalCount,com,导出,org,数据,百万
From: https://www.cnblogs.com/cjxiaozhuang/p/17629699.html

相关文章

  • sql server sqlbulkcopy 批量数据插入数据库
     SqlBulkCopyOptions介绍 SqlBulkCopy位于位于命名空间System.Data.SqlClient下,主要功能是把其他数据源(数据行DataRow,数据表DataTable,数据读取器IDataReader等)的数据有效批量的导入到SQLServer表中的功能。类似与MicrosoftSQLServer包中名为bcp的命令行应用程序。但......
  • 让 GPT-4 来修复 Golang “数据竞争”问题 - 每天5分钟玩转 GPT 编程系列(6)
    目录1.Golang中的“数据竞争”2.GoPool中的数据竞争问题3.让GPT-4来修复数据竞争问题3.1和GPT-4的第一轮沟通3.2和GPT-4的第二轮沟通3.3提交代码4.总结1.Golang中的“数据竞争”我在上个月发过一篇《跟着GPT-4从0到1学习Golang并发机制(三)》,文中有一节专......
  • 机器学习数据集:UCI Maching Learning Reposirity 473
    ......
  • C# 获取网络API接口中的数据(1)
    控制台案例:usingSystem;usingSystem.Net.Http;usingSystem.Threading.Tasks;usingNewtonsoft.Json;usingNewtonsoft.Json.Linq;usingSystem.Data;usingSystem.Xml.Linq;usingSystem.Net;namespaceConsoleApp{classProgram{staticvoidM......
  • 数据库查询
    MySQL数据库结构库创建删除查询选择使用表创建删除修改基本查询列增加删除修改查询行增加删除修改查询数据库是“按照数据结构来组织、存储和管理数据的仓库”PHPMyAdmin/linux命令id......
  • redis查询 第1个数据库,而不是默认的第0个数据库
    redis查询第1个数据库,而不是默认的第0个数据库 spring.redis.database=1默认:spring.redis.database=0car-test:0>getcar:info:detail:id001NULL##切换数据库car-test:0>select1OKcar-test:1>getcar:info:detail:id001{"id":444186}car-test:1>redis有......
  • 线性表【数据结构学习-青岛大学王卓老师】
    https://www.bilibili.com/video/BV1qz4y1p767/线性表线性表的初始化(顺序表)StatusInitList(SqList&L){L.elem=(ElemType*)malloc(sizeof(ElemType)*MAXSIZE);if(!L.elem)exit(OVERFLOW);L.length=0;returnOK;}线性表的销毁voi......
  • 抓包、反编译和数据获取的过程及工具网址
    抓包、反编译和数据获取是网络安全工程师在进行应用程序分析和漏洞挖掘时常用的技术。以下是这个过程的详细解释以及所涉及的工具和它们的优缺点。抓包:抓包是指截获并记录网络通信数据流的过程。抓包工具能够在网络上截取传输的数据包,分析其中的信息,用于网络分析、网络故障排除、......
  • R语言二手车汽车销售数据可视化探索:预处理、平滑密度图、地理空间可视化(带自测题)|附代
    全文链接:http://tecdat.cn/?p=27546原文出处:拓端数据部落公众号最近我们被客户要求撰写关于二手车汽车销售数据的研究报告,包括一些图形和统计输出。本文用爬虫采集了汽车销售数据,后来对其进行了扩展,创建这个数据集,其中包括境内的所有二手车辆或者经销商车辆条目数据。这些数据......
  • 马尔可夫转换模型研究交通伤亡人数事故时间序列预测|附代码数据
    原文链接:http://tecdat.cn/?p=12227最近我们被客户要求撰写关于马尔可夫转换模型的研究报告,包括一些图形和统计输出。本文描述了R语言中马尔克夫转换模型的分析过程首先,对模拟数据集进行详细建模。接下来,将马尔可夫转换模型拟合到具有离散响应变量的真实数据集。用于验证对这些......