java中生成30万的excel(采用多个excel,每个上面放6万数据,最后打包zip保存)

2024-05-13 13:58

本文主要是介绍java中生成30万的excel(采用多个excel,每个上面放6万数据,最后打包zip保存),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先要说明的是excel03  每个sheet最多放65535行所以,每行不能超过这个数,如果想放的多,可以考虑生成excel2007,

好像excel2007可以放100W多行数据

 

大数据量生成excel我只想到三种方法,当然基本也是网上看到的

1  生成多个excel打包   2  利用xml方式生成   3  用最新的包 

现在讲的是第一种  第二种也可以以后会做下测试   第三种好像不行,一直内存溢出

直接上代码

 

这个是servlet:

package servlets;
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.hssf.util.CellRangeAddress; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook;
import util.DBConnectionManager;
import domain.Person;
public class exportExcel extends HttpServlet {private String fileName;public void destroy() {super.destroy(); // Just puts "destroy" string in log // Put your code here }public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 文件名获取 Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");String f = "Person-" + format.format(date);this.fileName = f;setResponseHeader(response);OutputStream out = null;try {System.out.println("导出excel开始~~~"+System.currentTimeMillis());long startTime = System.currentTimeMillis();out = response.getOutputStream();DBConnectionManager db = new DBConnectionManager();//该部分是用于链接数据库List<Person> list = db.queryDataList(" select * from TEST_EXPORT ");//查询数据集合toExcel(list, request, 50000, f, out);System.out.println("导出excel结束~~~"+System.currentTimeMillis());long endTime = System.currentTimeMillis();System.out.println("导出excel共花费时间~~~"+(endTime-startTime)/1000);} catch (IOException e1) {e1.printStackTrace();} finally {try {out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}}/** 设置响应头 */public void setResponseHeader(HttpServletResponse response) {try {response.setContentType("application/octet-stream;charset=UTF-8");response.setHeader("Content-Disposition", "attachment;filename="+ java.net.URLEncoder.encode(this.fileName, "UTF-8")+ ".zip");response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");} catch (Exception ex) {ex.printStackTrace();}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}public void init() throws ServletException {// Put your code here }@SuppressWarnings("deprecation")public void toExcel(List<Person> list, HttpServletRequest request,int length, String f, OutputStream out) throws IOException {List<String> fileNames = new ArrayList();// 用于存放生成的文件名称s File zip = new File(request.getRealPath("/excel") + f + ".zip");// 压缩文件 // 生成excel for (int j = 0, n = list.size() / length + 1; j < n; j++) {Workbook book = new HSSFWorkbook();Sheet sheet = book.createSheet("person");double d = 0;// 用来统计 String file = request.getRealPath("/excel") + "/" + f + "-" + j + ".xls";fileNames.add(file);FileOutputStream o = null;try {o = new FileOutputStream(file);// sheet.addMergedRegion(new // CellRangeAddress(list.size()+1,0,list.size()+5,6)); Row row = sheet.createRow(0);row.createCell(0).setCellValue("ID");row.createCell(1).setCellValue("NAME");row.createCell(2).setCellValue("REMARK");int m = 1;for (int i = 1, min = (list.size() - j * length + 1) > (length + 1) ? (length + 1): (list.size() - j * length + 1); i < min; i++) {m++;Person user = list.get(length * (j) + i - 1);row = sheet.createRow(i);row.createCell(0).setCellValue(user.getId());row.createCell(1).setCellValue(user.getName());row.createCell(2).setCellValue(user.getRemark());}
//    CellStyle cellStyle2 = book.createCellStyle();
//
//    cellStyle2.setAlignment(CellStyle.ALIGN_CENTER);
//
//    row = sheet.createRow(m);
//
//    Cell cell0 = row.createCell(0);
//
//    cell0.setCellValue("Total");
//
//    cell0.setCellStyle(cellStyle2);
//
//    Cell cell4 = row.createCell(4);
//
//    cell4.setCellValue(d);
//
//    cell4.setCellStyle(cellStyle2);
//
//    sheet.addMergedRegion(new CellRangeAddress(m, m, 0, 3));} catch (Exception e) {e.printStackTrace();}try {book.write(o);} catch (Exception ex) {ex.printStackTrace();} finally {if (o != null){o.flush();o.close();}}}File srcfile[] = new File[fileNames.size()];for (int i = 0, n = fileNames.size(); i < n; i++) {srcfile[i] = new File(fileNames.get(i));}util.FileZip.ZipFiles(srcfile, zip);FileInputStream inStream = new FileInputStream(zip);byte[] buf = new byte[4096];int readLength;while (((readLength = inStream.read(buf)) != -1)) {out.write(buf, 0, readLength);}inStream.close();}
}


 

实体类

package domain;
public class Person {private String id;private String name;private String remark;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}}


 

这个是打包的工具类

package util;
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; public class FileZip {/** *  * @param srcfile 文件名数组 * @param zipfile 压缩后文件 */ public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) { byte[] buf = new byte[1024]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipfile)); for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); out.putNextEntry(new ZipEntry(srcfile[i].getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); } catch (IOException e) { e.printStackTrace(); } } }


 

jsp我就传了,

 

另外这个也是我从别的地方转载的,修改了一些地方,也有很多不完善的地方,如果在项目中最好再做优化

这篇关于java中生成30万的excel(采用多个excel,每个上面放6万数据,最后打包zip保存)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in