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

相关文章

Java五子棋之坐标校正

上篇针对了Java项目中的解构思维,在这篇内容中我们不妨从整体项目中拆解拿出一个非常重要的五子棋逻辑实现:坐标校正,我们如何使漫无目的鼠标点击变得有序化和可控化呢? 目录 一、从鼠标监听到获取坐标 1.MouseListener和MouseAdapter 2.mousePressed方法 二、坐标校正的具体实现方法 1.关于fillOval方法 2.坐标获取 3.坐标转换 4.坐

Spring Cloud:构建分布式系统的利器

引言 在当今的云计算和微服务架构时代,构建高效、可靠的分布式系统成为软件开发的重要任务。Spring Cloud 提供了一套完整的解决方案,帮助开发者快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器等)。本文将探讨 Spring Cloud 的定义、核心组件、应用场景以及未来的发展趋势。 什么是 Spring Cloud Spring Cloud 是一个基于 Spring

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

java8的新特性之一(Java Lambda表达式)

1:Java8的新特性 Lambda 表达式: 允许以更简洁的方式表示匿名函数(或称为闭包)。可以将Lambda表达式作为参数传递给方法或赋值给函数式接口类型的变量。 Stream API: 提供了一种处理集合数据的流式处理方式,支持函数式编程风格。 允许以声明性方式处理数据集合(如List、Set等)。提供了一系列操作,如map、filter、reduce等,以支持复杂的查询和转

Mac excel 同时冻结首行和首列

1. 选择B2窗格 2. 选择视图 3. 选择冻结窗格 最后首行和首列的分割线加粗了就表示成功了

Java面试八股之怎么通过Java程序判断JVM是32位还是64位

怎么通过Java程序判断JVM是32位还是64位 可以通过Java程序内部检查系统属性来判断当前运行的JVM是32位还是64位。以下是一个简单的方法: public class JvmBitCheck {public static void main(String[] args) {String arch = System.getProperty("os.arch");String dataM

详细分析Springmvc中的@ModelAttribute基本知识(附Demo)

目录 前言1. 注解用法1.1 方法参数1.2 方法1.3 类 2. 注解场景2.1 表单参数2.2 AJAX请求2.3 文件上传 3. 实战4. 总结 前言 将请求参数绑定到模型对象上,或者在请求处理之前添加模型属性 可以在方法参数、方法或者类上使用 一般适用这几种场景: 表单处理:通过 @ModelAttribute 将表单数据绑定到模型对象上预处理逻辑:在请求处理之前

eclipse运行springboot项目,找不到主类

解决办法尝试了很多种,下载sts压缩包行不通。最后解决办法如图: help--->Eclipse Marketplace--->Popular--->找到Spring Tools 3---->Installed。

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Java面试题:通过实例说明内连接、左外连接和右外连接的区别

在 SQL 中,连接(JOIN)用于在多个表之间组合行。最常用的连接类型是内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。它们的主要区别在于它们如何处理表之间的匹配和不匹配行。下面是每种连接的详细说明和示例。 表示例 假设有两个表:Customers 和 Orders。 Customers CustomerIDCus