java使用poi实现大数据量导出为EXCEL
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,总体的实现思想为:每次查询出2w数据,并写入到临时文件然后把这些文件写入到一个EXCEL里边,或者把这些文件压缩为zip文件,然后把Zip文件提供给下载(这里使用zip打包是因为,在Linux上也能进
千家信息网最后更新 2025年12月02日java使用poi实现大数据量导出为EXCEL
总体的实现思想为:
每次查询出2w数据,并写入到临时文件
然后把这些文件写入到一个EXCEL里边,或者把这些文件压缩为zip文件,然后把Zip文件提供给下载(这里使用zip打包是因为,在Linux上也能进行Zip打包)。
//这个zip打包工具类package net.szh.zip; import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Zip; import org.apache.tools.ant.types.FileSet; public class ZipCompressorByAnt { private File zipFile; public ZipCompressorByAnt(String pathName) { zipFile = new File(pathName); } public void compress(String srcPathName) { File srcdir = new File(srcPathName); if (!srcdir.exists()) throw new RuntimeException(srcPathName + "不存在!"); Project prj = new Project(); Zip zip = new Zip(); zip.setProject(prj); zip.setDestFile(zipFile); FileSet fileSet = new FileSet(); fileSet.setProject(prj); fileSet.setDir(srcdir); //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java"); //fileSet.setExcludes(...); 排除哪些文件或文件夹 zip.addFileset(fileSet); zip.execute(); } }业务处理 import java.util.Map; import com.eos.common.transaction.ITransactionManager; import com.eos.common.transaction.TransactionManagerFactory; import com.eos.system.annotation.Bizlet; import com.pns.framework.dao.Dao; import com.pns.framework.execl.Excel; import com.pns.framework.filepath.ExportExeclUtil; import com.pns.framework.filepath.FilePath; import commonj.sdo.DataObject; @Bizlet("") public class Aeanalysis { private ExportExeclUtil util= new ExportExeclUtil(); /** * 变电站 * */ @Bizlet("") public String export4boassets(Map map,String nameSqlid) { String tempFileName ; String fileName ; ITransactionManager manager = TransactionManagerFactory.getTransactionManager(); DataObject[] objects={}; try { /** *在这儿得到总共的条数,再除每个文件的条数,得到需要多少个文件,求余如果不为零, *则需要循环的次数即为文件数,如果不为零则为文件数+1;文件写完之后进行压缩 *,把压缩文件的地址返回到流的jsp中。 */ tempFileName = FilePath.getTempFilePath("02_02_01.xls");//零时文件 例:xxx.xls fileName = FilePath.getTemplatePath("aeanalysis/uic01_001.xls");//模板文件 例: pamanagement/uic02_001_004.xls manager.begin(); objects = Dao.query(nameSqlid, map); manager.commit(); //变电站标识 电网变电站标识 变电站名称 电压等级 变电站地址 管理单位 运行状态 变电站标识 变电站业务系统ID 变电站名称 电压等级 变电站地址 管理单位 运行状态 备注 String[] fieldArrStr = new String[] {"SUBS_ID", "PMS_SUBS_ID", "SUBS_NAME", "VOLT_CODE","SUBS_ADDR","ORG_NAME","RUN_STATUS","PMS_ID","PMS_GLOBEID", "PMS_SUBS_NAME","PMS_VOLT_CODE","PMS_SUBS_ADDR","PMS_ORG_NAME","PMS_RUN_STATUS", "REMARK"}; util.exportExcelToTemp(fileName, 2, objects, fieldArrStr, tempFileName); } catch (RuntimeException e) { manager.rollback(); tempFileName="ex"; } return tempFileName; } /** * 生成只有数据的execl到临时文件夹下 * * @param args * @throws IOException */ public boolean exportExcelToTemp(String temFile,int beginRowIndex,Object[] objects,String[] fieldArrStr,String tempPath) { boolean flag; FileOutputStream fos = null; try { this.getWorkBook(temFile); this.getSheet(); this.setDataRow(beginRowIndex, objects, fieldArrStr); fos = new FileOutputStream(tempPath); workBook.write(fos); flag=true; } catch (IOException e) { // TODO 自动生成 catch 块 flag=false; } finally { try { if(fos != null){ fos.close(); } } catch (IOException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } return flag; } } jsp文件
<%@page pageEncoding="UTF-8"%><%@page contentType="text/plain; charset=utf-8" %><%@page import="org.apache.commons.fileupload.*" %><%@page import="java.net.URLEncoder"%><%@page import="java.io.BufferedOutputStream"%><%@page import="java.io.FileInputStream"%><%@page import="java.io.IOException"%><%@page import="java.util.Date"%><%@page import="java.text.SimpleDateFormat"%><%String path =request.getParameter("path"); String filename=request.getParameter("filename"); //String time=request.getParameter("time"); response.reset();//可以加也可以不加 response.setContentType("application/x-download"); filename = URLEncoder.encode(filename,"UTF-8"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 String dd=df.format(new Date()); //System.out.println(dd);// new Date()为获取当前系统时间 response.addHeader("Content-Disposition", "p_w_upload; filename=" +dd+"_"+filename); BufferedOutputStream bos = null; FileInputStream fis = null; try { fis = new FileInputStream(path); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[1024]; while(fis.read(buffer) != -1){ bos.write(buffer); } response.flushBuffer(); out.clear(); }catch(IOException e) { e.printStackTrace(); }finally { fis.close(); bos.close(); }%>
文件
变电站
变电
地址
文件夹
标识
生成
数据
业务
件数
单位
名称
状态
电压
等级
自动生成
管理
运行
只有
备注
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
成都人工智能软件开发定制
数据库安全的一般方法
软件开发者路线图 pdf
电子软件开发业务
软件开发预算适合什么预算制度
信息化网络安全课
网络安全政治经济
中国国家统计数据库
深圳华强北软件开发
软件开发可以用哪几种语言
软件开发岗位晋级表
黑龙江互联网养老软件开发哪儿好
企业投标数据库如何建立
中国根服务器被取消
计算机3级网络技术应用题
使用向导创建数据库
web 网络安全评估
玖玖盾网络安全全景
达梦数据库删除实例
软件开发有哪些网址
网络技术是怎样影响人们思考的
数据库怎么防止重复提交
网络安全年主题班会
学校落实网络安全工作责任制百度
vpn服务器安装
byte 在数据库
手机按键精灵 新建数据库
小程序数据库收费
三维互联网科技有限公司
广电网络安全机房