本文实例为大家分享了java使用poi将多个sheet合并为一个sheet的具体代码,供大家参考,具体内容如下
一、情景描述
最近在项目中客户提到一个新的需求,一开始是在列表查询时导出多个excel表格,后面提到将多个excel表格进行合并,实现一个sheet显示多个sheet内容,图示如下:
一开始:
合并后(不同表格空一行隔开):
二、实现思路
首先,先按照生成五张表的思路来生成创建一个workbook sourceworkbook
,然后再创建一个workbook targetworkbook
,创建一个新的sheet targetsheet
工作表,之后将sourceworkbook
中第一个sheet sheet1
中的内容复制到该表中,再将第二个sheet sheet2
中的内容复制到targetsheet
中,依次操作,复制完sourceworkbook
中全部的五张表,即可实现将多个sheet合并为一个sheet的操作。
三、示例代码
1.poiutil工具类
package com.cdtye.itps.jjxt.model.util; import com.cdtye.itps.jjxt.model.excel.cellrangeaddressexcelvo; import org.apache.poi.ss.usermodel.*; import org.springframework.util.collectionutils; import java.util.list; /** * @author zhongks * @description //todo poi导出excel工具类 * @date 17:16 2021/5/11 * @param * @return **/ public class poiutil { /** * @author zhongks * @description //todo 拷贝sheet(表) * @date 17:16 2021/5/11 * @param [targetsheet, sourcesheet, targetwork, sourcework, startrow, cellrangeaddressexcelvolist] * @return void **/ public static void copysheet(sheet targetsheet, sheet sourcesheet, workbook targetwork, workbook sourcework, int startrow, list cellrangeaddressexcelvolist) { if(targetsheet == null || sourcesheet == null || targetwork == null || sourcework == null){ throw new illegalargumentexception("调用poiutil.copysheet()方法时,targetsheet、sourcesheet、targetwork、sourcework都不能为空,故抛出该异常!"); } //设置单元格默认宽度 targetsheet.setdefaultcolumnwidth(25); //复制源表中的行 for (int i = sourcesheet.getfirstrownum(); i <= sourcesheet.getlastrownum(); i ) { row sourcerow = sourcesheet.getrow(i); row targetrow = targetsheet.createrow(i startrow); //创建新的row if (sourcerow != null) { copyrow(targetrow, sourcerow, targetwork, sourcework); } } //自定义合并单元格样式(若不需要进行单元格合并操作,将cellrangeaddressexcelvolist赋值为null即可) if(!collectionutils.isempty(cellrangeaddressexcelvolist)){ //合并单元格 for(cellrangeaddressexcelvo model:cellrangeaddressexcelvolist){ targetsheet.addmergedregion(new org.apache.poi.ss.util.cellrangeaddress(model.getfirstrow(),model.getlastrow(),model.getfirstcol(),model.getlastcol())); } } } /** * @author zhongks * @description //todo 拷贝row(行) * @date 17:17 2021/5/11 * @param [targetrow, sourcerow, targetwork, sourcework] * @return void **/ public static void copyrow(row targetrow, row sourcerow, workbook targetwork, workbook sourcework) { if(targetrow == null || sourcerow == null || targetwork == null || sourcework == null ){ throw new illegalargumentexception("调用poiutil.copyrow()方法时,targetrow、sourcerow、targetwork、sourcework、targetpatriarch都不能为空,故抛出该异常!"); } //设置行高 targetrow.setheight(sourcerow.getheight()); for (int i = sourcerow.getfirstcellnum(); i < sourcerow.getlastcellnum(); i ) { cell sourcecell = sourcerow.getcell(i); cell targetcell = null; if (sourcecell != null && sourcecell.getstringcellvalue()!="") { if (targetcell == null) { targetcell = targetrow.createcell(i); } //拷贝单元格,包括内容和样式 copycell(targetcell, sourcecell, targetwork, sourcework); } } } /** * @author zhongks * @description //todo 拷贝cell(单元格) * @date 17:18 2021/5/11 * @param [targetcell, sourcecell, targetwork, sourcework] * @return void **/ public static void copycell(cell targetcell, cell sourcecell, workbook targetwork, workbook sourcework) { if(targetcell == null || sourcecell == null || targetwork == null || sourcework == null ){ throw new illegalargumentexception("调用poiutil.copycell()方法时,targetcell、sourcecell、targetwork、sourcework都不能为空,故抛出该异常!"); } cellstyle targetcellstyle=targetwork.createcellstyle(); targetcellstyle.clonestylefrom(sourcecell.getcellstyle());//拷贝样式 //重新添加样式(这里可以根据你的需要重新进行单元格样式添加) /*targetcellstyle.setbordertop(borderstyle.thin);//设置上边框线 targetcellstyle.setborderleft(borderstyle.thin);//设置左边框线 targetcellstyle.setborderbottom(borderstyle.thin);//设置下边框线 targetcellstyle.setborderright(borderstyle.thin);//设置右边框线*/ targetcell.setcellstyle(targetcellstyle); targetcell.setcellvalue(sourcecell.getstringcellvalue()); } }
2.需要合并的单元格位置信息实体
package com.cdtye.itps.jjxt.model.excel; import lombok.allargsconstructor; import lombok.data; import lombok.experimental.accessors; /** * @classname cellrangeaddressexcelvo * @description todo 需要合并的单元格位置信息vo * @author zhongks * @date 2021/5/11 14:09 * @version 1.0 **/ @data @accessors(chain = true) @allargsconstructor public class cellrangeaddressexcelvo { //起始行号 private int firstrow; //终止行号 private int lastrow; //起始列号 private int firstcol; //终止列号 private int lastcol; }
该实体类是为了进行合并单元格操作,用来存储需要合并的单元格位置信息:
service层代码:
* * @author zhongks * @description //todo excel导出 * @date 12:25 2021/5/7 * @param [list, response] * @return void **/ public void export(bureaudayshiftvo bureaudayshiftvo,httpservletresponse response) { try { // 设置下载的excel名称,以当前时间为文件后缀, string datetime = dateutil.formatdatestring(new date(), dateutil.date_format); string filename = "供电安全质量日交班表" datetime ".xlsx"; // 设置响应输出的头类型 response.setheader("content-type", "application/vnd.ms-excel"); response.setheader("content-disposition", "attachment;filename=" filename); // excel信息部分 //供电处重点信息追踪表信息 bureaudayshiftvo.settrackflag(1); map tracksafequalitymap =this.gettracksafequalitymap(bureaudayshiftvo); //日安全质量信息表信息 bureaudayshiftvo.settrackflag(0); map safequalityparamsmap =this.gettracksafequalitymap(bureaudayshiftvo); //天窗兑现统计表 map skylightcashstatisticsmap = this.getskylightcashstatisticsmap(); //其他安全质量信息表 map othersafequalityinfomap = this.getothersafequalityinfomap(bureaudayshiftvo); //安全质量考核表 map safequalityassessmentmap = this.getsafequalityassessmentmap(); //添加表 list
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。