Java使用POI將多個Sheet合并為一個Sheet
本文實例為大家分享了Java使用POI將多個Sheet合并為一個Sheet的具體代碼,供大家參考,具體內容如下
一、情景描述
最近在項目中客戶提到一個新的需求,一開始是在列表查詢時導出多個Excel表格,后面提到將多個Excel表格進行合并,實現(xiàn)一個sheet顯示多個sheet內容,圖示如下:
一開始:
合并后(不同表格空一行隔開):
二、實現(xiàn)思路
首先,先按照生成五張表的思路來生成創(chuàng)建一個Workbook sourceWorkbook
,然后再創(chuàng)建一個Workbook targetWorkbook
,創(chuàng)建一個新的Sheet targetSheet
工作表,之后將sourceWorkbook
中第一個Sheet sheet1
中的內容復制到該表中,再將第二個Sheet sheet2
中的內容復制到targetSheet
中,依次操作,復制完sourceWorkbook
中全部的五張表,即可實現(xiàn)將多個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<CellRangeAddressExcelVo> 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); ?//創(chuàng)建新的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());//拷貝樣式 ? ? ? ? //重新添加樣式(這里可以根據(jù)你的需要重新進行單元格樣式添加) ? ? ? ? /*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<String, Object> trackSafeQualityMap =this.getTrackSafeQualityMap(bureauDayShiftVo); ? ? ? ? ? ? //日安全質量信息表信息 ? ? ? ? ? ? bureauDayShiftVo.setTrackFlag(0); ? ? ? ? ? ? Map<String, Object> safeQualityParamsMap =this.getTrackSafeQualityMap(bureauDayShiftVo); ? ? ? ? ? ? //天窗兌現(xiàn)統(tǒng)計表 ? ? ? ? ? ? Map<String, Object> skylightCashStatisticsMap = this.getSkylightCashStatisticsMap(); ? ? ? ? ? ? //其他安全質量信息表 ? ? ? ? ? ? Map<String, Object> otherSafeQualityInfoMap = this.getOtherSafeQualityInfoMap(bureauDayShiftVo); ? ? ? ? ? ? //安全質量考核表 ? ? ? ? ? ? Map<String, Object> safeQualityAssessmentMap = this.getSafeQualityAssessmentMap(); ? ? ? ? ? ? //添加表 ? ? ? ? ? ? List<Map<String, Object>> sheetsList = new ArrayList<>(); ? ? ? ? ? ? sheetsList.add(trackSafeQualityMap); ? ? ? ? ? ? sheetsList.add(safeQualityParamsMap); ? ? ? ? ? ? sheetsList.add(skylightCashStatisticsMap); ? ? ? ? ? ? sheetsList.add(otherSafeQualityInfoMap); ? ? ? ? ? ? sheetsList.add(safeQualityAssessmentMap); ? ? ? ? ? ? List<Map<String, Object>> sourceSheetsList = new ArrayList<>(); ? ? ? ? ? ? //創(chuàng)建excel文件的方法 ? ? ? ? ? ? Workbook sourceWorkbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF); ? ? ? ? ? ? Workbook targetWorkbook = ExcelExportUtil.exportExcel(sourceSheetsList, ExcelType.HSSF); ? ? ? ? ? ? Workbook workbook = this.mergeWorkSheet(targetWorkbook, sourceWorkbook); ? ? ? ? ? ? //通過response輸出流直接輸入給客戶端 ? ? ? ? ? ? ServletOutputStream outputStream = response.getOutputStream(); ? ? ? ? ? ? workbook.write(outputStream); ? ? ? ? ? ? outputStream.flush(); ? ? ? ? ? ? outputStream.close(); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ? /** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 返回重點追蹤以及非重點追蹤excel信息 ? ? ?* @Date 9:31 2021/5/8 ? ? ?* @Param [bureauDayShiftVo] ? ? ?* @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> ? ? ?**/ ? ? public Map<String, Object> getTrackSafeQualityMap(BureauDayShiftVo bureauDayShiftVo){ ? ? ? ? List<BureauDayShiftExcelVo> exportList = new LinkedList<>(); ? ? ? ? List<Map<String, Object>> allTrackSafeQualityList = this.getAllTrackSafeQualityList(bureauDayShiftVo); ? ? ? ? //封裝數(shù)據(jù) ? ? ? ? allTrackSafeQualityList.forEach(map -> { ? ? ? ? ? ? String basicInformation="單位:"+map.get("unitDeptName")+"\n"+ ? ? ? ? ? ? ? ? ? ? "線別:"+map.get("lineName")+"\n"+ ? ? ? ? ? ? ? ? ? ? "所亭:"+map.get("bdsSubstationName")+"\n"+ ? ? ? ? ? ? ? ? ? ? "開關號:"+map.get("switchNo")+"\n"+ ? ? ? ? ? ? ? ? ? ? "故障地點:"+map.get("faultPlace")+"\n"+ ? ? ? ? ? ? ? ? ? ? "發(fā)生時間:"+DateUtil.formatDateString(map.get("stopDate"), DateUtil.DATE_FORMAT)+"\n"+ ? ? ? ? ? ? ? ? ? ? "停時(分鐘):"+map.get("stopMinute")+"\n"+ ? ? ? ? ? ? ? ? ? ? "天氣:"+map.get("weatherInfo")+"\n"+ ? ? ? ? ? ? ? ? ? ? "專業(yè)分類:"+map.get("faultMajorName")+"\n"; ? ? ? ? ? ? String segmentAnalysis="單位:"+map.get("unitDeptName")+"\n"+ ? ? ? ? ? ? ? ? ? ? "單位:詳見分析報告"+"\n"; ? ? ? ? ? ? String isTrack=""; ? ? ? ? ? ? if(bureauDayShiftVo.getTrackFlag()==0){ ? ? ? ? ? ? ? ? isTrack="否"; ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? isTrack="是"; ? ? ? ? ? ? } ? ? ? ? ? ? String review="科室:"+map.get("trackUnitDeptName")+"\n"+ ? ? ? ? ? ? ? ? ? ? "問題類別:"+map.get("faultCategoryConfigName")+"\n"+ ? ? ? ? ? ? ? ? ? ? "定責考核:"+map.get("dutyType")+"\n"+ ? ? ? ? ? ? ? ? ? ? "審核結果:"+map.get("switchNo")+"\n"+ ? ? ? ? ? ? ? ? ? ? "重點追蹤:"+isTrack+"\n"; ? ? ? ? ? ? BureauDayShiftExcelVo bureauDayShiftExcelVo =new BureauDayShiftExcelVo( ? ? ? ? ? ? ? ? ? ? DateUtil.formatDateString(map.get("inputDate"), DateUtil.DATE_FORMAT), ? ? ? ? ? ? ? ? ? ? basicInformation, ? ? ? ? ? ? ? ? ? ? (String)map.get("faultDescription"), ? ? ? ? ? ? ? ? ? ? (String)map.get("reportType"), ? ? ? ? ? ? ? ? ? ? segmentAnalysis, ? ? ? ? ? ? ? ? ? ? review, ? ? ? ? ? ? ? ? ? ? map.get("safeQualityState").toString(), ? ? ? ? ? ? ? ? ? ? String.valueOf(bureauDayShiftVo.getTrackFlag())); ? ? ? ? ? ? exportList.add(bureauDayShiftExcelVo); ? ? ? ? }); ? ? ? ? ExportParams exportParams = new ExportParams(); ? ? ? ? //設置邊框樣式 // ? ? ? ?exportParams.setStyle(ExcelStyleType.BORDER.getClazz()); ? ? ? ? // 設置sheet的名稱 ? ? ? ? if(bureauDayShiftVo.getTrackFlag()==0){ ? ? ? ? ? ? exportParams.setSheetName("日安全質量信息"); ? ? ? ? }else{ ? ? ? ? ? ? exportParams.setSheetName("供電處重點追蹤信息"); ? ? ? ? } ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? // title的參數(shù)為ExportParams類型,目前僅僅在ExportParams中設置了sheetName ? ? ? ? map.put("title", exportParams); ? ? ? ? // 模版導出對應得實體類型,即包含了List的對象 ? ? ? ? map.put("entity", BureauDayShiftExcelVo.class); ? ? ? ? // sheet中要填充得數(shù)據(jù) ? ? ? ? map.put("data", exportList); ? ? ? ? return map; ? ? } ? ? /** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 返回天窗兌現(xiàn)統(tǒng)計excel信息 ? ? ?* @Date 10:59 2021/5/8 ? ? ?* @Param [] ? ? ?* @return java.util.Map<java.lang.String,java.lang.Object> ? ? ?**/ ? ? public Map<String, Object> getSkylightCashStatisticsMap(){ ? ? ? ? List<BureauSkylightCashStatisticsExcelVo> exportList = new LinkedList<>(); ? ? ? ? //ToDo 得到天窗兌現(xiàn)統(tǒng)計列表數(shù)據(jù)并進行封裝 ? ? ? ? //示例數(shù)據(jù) ? ? ? ? BureauSkylightCashStatisticsCommonExcelVo applicationExcelVo=new BureauSkylightCashStatisticsCommonExcelVo("申請供電類","申請非供電類"); ? ? ? ? BureauSkylightCashStatisticsCommonExcelVo applicationTimeExcelVo=new BureauSkylightCashStatisticsCommonExcelVo("申請時間供電類","申請時間非供電類"); ? ? ? ? BureauSkylightCashStatisticsCommonExcelVo getTimeExcelVo=new BureauSkylightCashStatisticsCommonExcelVo("給點時間供電類","給點時間非供電類"); ? ? ? ? BureauSkylightCashStatisticsCommonExcelVo workTimeExcelVo=new BureauSkylightCashStatisticsCommonExcelVo(null,null); ? ? ? ? BureauSkylightCashStatisticsExcelVo bureauSkylightCashStatisticsExcelVo =new BureauSkylightCashStatisticsExcelVo("懷化供電段","高鐵","滬昆高速線", ? ? ? ? ? ? ? ? applicationExcelVo,"取消","10","10",applicationTimeExcelVo,getTimeExcelVo,workTimeExcelVo,"天窗取消原因"); ? ? ? ? exportList.add(bureauSkylightCashStatisticsExcelVo); ? ? ? ? exportList.add(bureauSkylightCashStatisticsExcelVo); ? ? ? ? exportList.add(bureauSkylightCashStatisticsExcelVo); ? ? ? ? //供電處重點追蹤信息表 ? ? ? ? ExportParams exportParams = new ExportParams(); ? ? ? ? //設置邊框樣式 // ? ? ? ?exportParams.setStyle(ExcelStyleType.BORDER.getClazz()); ? ? ? ? // 設置sheet的名稱 ? ? ? ? exportParams.setSheetName("天窗兌現(xiàn)統(tǒng)計"); ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? // title的參數(shù)為ExportParams類型,目前僅僅在ExportParams中設置了sheetName ? ? ? ? map.put("title", exportParams); ? ? ? ? // 模版導出對應得實體類型,即包含了List的對象 ? ? ? ? map.put("entity", BureauSkylightCashStatisticsExcelVo.class); ? ? ? ? // sheet中要填充得數(shù)據(jù) ? ? ? ? map.put("data", exportList); ? ? ? ? return map; ? ? } ? ?? ? ? /** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 返回其他安全信息excel信息 ? ? ?* @Date 11:01 2021/5/8 ? ? ?* @Param [] ? ? ?* @return java.util.Map<java.lang.String,java.lang.Object> ? ? ?**/ ? ? public Map<String, Object> getOtherSafeQualityInfoMap(BureauDayShiftVo bureauDayShiftVo){ ? ? ? ? List<BureauOtherSafeQualityInfoExcelVo> exportList = new LinkedList<>(); ? ? ? ? //ToDo 得到其他安全信息列表數(shù)據(jù)并進行封裝 ? ? ? ? BureauSafeQualityOtherInfoVo bureauSafeQualityOtherInfoVo=new BureauSafeQualityOtherInfoVo(); ? ? ? ? bureauSafeQualityOtherInfoVo.setStartDate(bureauDayShiftVo.getStartDate()); ? ? ? ? bureauSafeQualityOtherInfoVo.setEndDate(bureauDayShiftVo.getEndDate()); ? ? ? ? List<Map<String, Object>> list = bureauSafeQualityOtherInfoService.findList(bureauSafeQualityOtherInfoVo); ? ? ? ? list.forEach(map->{ ? ? ? ? ? ? BureauOtherSafeQualityInfoExcelVo otherSafeQualityInfoExcelVo=new BureauOtherSafeQualityInfoExcelVo( ? ? ? ? ? ? ? ? ? ? DateUtil.formatDateString(map.get("createDatetime"), DateUtil.DATE_FORMAT), ? ? ? ? ? ? ? ? ? ? (String)map.get("description"), ? ? ? ? ? ? ? ? ? ? (String)map.get("inputStaffName"), ? ? ? ? ? ? ? ? ? ? DateUtil.formatDateString(map.get("createDatetime"), DateUtil.DATE_FORMAT), ? ? ? ? ? ? ? ? ? ? (String)map.get("modifyStaffName"), ? ? ? ? ? ? ? ? ? ? DateUtil.formatDateString(map.get("updateDatetime"), DateUtil.DATE_FORMAT) ? ? ? ? ? ? ); ? ? ? ? ? ? exportList.add(otherSafeQualityInfoExcelVo); ? ? ? ? }); ? ? ? ? //供電處重點追蹤信息表 ? ? ? ? ExportParams exportParams = new ExportParams(); ? ? ? ? //設置邊框樣式 // ? ? ? ?exportParams.setStyle(ExcelStyleType.BORDER.getClazz()); ? ? ? ? // 設置sheet的名稱 ? ? ? ? exportParams.setSheetName("其他安全信息"); ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? // title的參數(shù)為ExportParams類型,目前僅僅在ExportParams中設置了sheetName ? ? ? ? map.put("title", exportParams); ? ? ? ? // 模版導出對應得實體類型,即包含了List的對象 ? ? ? ? map.put("entity", BureauOtherSafeQualityInfoExcelVo.class); ? ? ? ? // sheet中要填充得數(shù)據(jù) ? ? ? ? map.put("data", exportList); ? ? ? ? return map; ? ? } ? ? /** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 返回安全質量考核excel信息 ? ? ?* @Date 11:04 2021/5/8 ? ? ?* @Param [] ? ? ?* @return java.util.Map<java.lang.String,java.lang.Object> ? ? ?**/ ? ? public Map<String, Object> getSafeQualityAssessmentMap(){ ? ? ? ? List<BureauSafeQualityAssessmentExcelVo> exportList = new LinkedList<>(); ? ? ? ? //ToDo 得到安全質量考核列表數(shù)據(jù)并進行封裝 ? ? ? ? //供電處重點追蹤信息表 ? ? ? ? ExportParams exportParams = new ExportParams(); ? ? ? ? //設置邊框樣式 // ? ? ? ?exportParams.setStyle(ExcelStyleType.BORDER.getClazz()); ? ? ? ? // 設置sheet的名稱 ? ? ? ? exportParams.setSheetName("安全質量考核"); ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? // title的參數(shù)為ExportParams類型,目前僅僅在ExportParams中設置了sheetName ? ? ? ? map.put("title", exportParams); ? ? ? ? // 模版導出對應得實體類型,即包含了List的對象 ? ? ? ? map.put("entity", BureauSafeQualityAssessmentExcelVo.class); ? ? ? ? // sheet中要填充得數(shù)據(jù) ? ? ? ? map.put("data", exportList); ? ? ? ? return map; ? ? } ? ? /** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 合并sheet ? ? ?* @Date 10:39 2021/5/11 ? ? ?* @Param [targetWorkbook, sourceWorkbook] ? ? ?* @return org.apache.poi.ss.usermodel.Workbook ? ? ?**/ ? ? public static Workbook mergeWorkSheet(Workbook targetWorkbook, Workbook sourceWorkbook){ ? ? ? ? try{ ? ? ? ? ? ? //第一個sheet ? ? ? ? ? ? Sheet firstSourceSheet=sourceWorkbook.getSheetAt(0); ? ? ? ? ? ? //獲得第一個sheet總行數(shù) ? ? ? ? ? ? int firstSourceSheetLen=firstSourceSheet.getPhysicalNumberOfRows(); ? ? ? ? ? ? //獲取第幾個工作表 ? ? ? ? ? ? Sheet secondSourceSheet= sourceWorkbook.getSheetAt(1); ? ? ? ? ? ? int secondSourceSheetLen=secondSourceSheet.getPhysicalNumberOfRows(); ? ? ? ? ? ? Sheet thirdSourceSheet=sourceWorkbook.getSheetAt(2); ? ? ? ? ? ? int thirdSourceSheetLen=thirdSourceSheet.getPhysicalNumberOfRows(); ? ? ? ? ? ? Sheet fourSourceSheet=sourceWorkbook.getSheetAt(3); ? ? ? ? ? ? int fourSourceSheetLen=fourSourceSheet.getPhysicalNumberOfRows(); ? ? ? ? ? ? Sheet fiveSourceSheet=sourceWorkbook.getSheetAt(4); ? ? ? ? ? ? //表合并后新表名稱 ? ? ? ? ? ? Sheet targetSheet = targetWorkbook.createSheet("安全質量信息日交班表"); ? ? ? ? ? ? //表合并(根據(jù)startRow來控制各個表之間的距離,這里為空一行) ? ? ? ? ? ? POIUtil.copySheet(targetSheet, firstSourceSheet, targetWorkbook, sourceWorkbook,0,null); ? ? ? ? ? ? POIUtil.copySheet(targetSheet, secondSourceSheet, targetWorkbook, sourceWorkbook,firstSourceSheetLen+1,null); ? ? ? ? ? ? int thirdSourceSheetColLen=thirdSourceSheet.getRow(0).getPhysicalNumberOfCells(); ? ? ? ? ? ? //得到需要合并單元格的坐標列表,row與col都從0開始計算 ? ? ? ? ? ? List<CellRangeAddressExcelVo> cellRangeAddressExcelVoList = getCellRangeAddressExcelVoList(firstSourceSheetLen+secondSourceSheetLen+2, thirdSourceSheetColLen); ? ? ? ? ? ? //第三張表需要進行合并單元格操作 ? ? ? ? ? ? POIUtil.copySheet(targetSheet, thirdSourceSheet, targetWorkbook, sourceWorkbook,firstSourceSheetLen+secondSourceSheetLen+2,cellRangeAddressExcelVoList); ? ? ? ? ? ? POIUtil.copySheet(targetSheet, fourSourceSheet, targetWorkbook, sourceWorkbook,firstSourceSheetLen+secondSourceSheetLen+thirdSourceSheetLen+3,null); ? ? ? ? ? ? POIUtil.copySheet(targetSheet, fiveSourceSheet, targetWorkbook, sourceWorkbook,firstSourceSheetLen+secondSourceSheetLen+thirdSourceSheetLen+fourSourceSheetLen+4,null); ? ? ? ? ? ? return targetWorkbook; ? ? ? ? }catch (Exception e){ ? ? ? ? ? ? log.error("Workbook合并出錯",e); ? ? ? ? ? ? return null; ? ? ? ? } ? ? } ? ? /** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 根據(jù)表格場景自定義需要返回合并的單元格位置坐標(注意:row與col都從0開始計算) ? ? ?* @Date 14:23 2021/5/11 ? ? ?* @Param [row, col] ? ? ?* @return java.util.List<com.cdtye.itps.jjxt.model.excel.CellRangeAddressExcelVo> ? ? ?**/ ? ? public static List<CellRangeAddressExcelVo> getCellRangeAddressExcelVoList(int row,int col){ ? ? ? ? //合并單元格坐標位置 ? ? ? ? List<CellRangeAddressExcelVo> list=new LinkedList<>(); ? ? ? ? for(int i=0;i<15;i++){ ? ? ? ? ? ? if(i<7){ ? ? ? ? ? ? ? ? CellRangeAddressExcelVo cellRangeAddressExcelVo=new CellRangeAddressExcelVo(row,row+1,i,i); ? ? ? ? ? ? ? ? list.add(cellRangeAddressExcelVo); ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? CellRangeAddressExcelVo cellRangeAddressExcelVo=new CellRangeAddressExcelVo(row,row,i,i+1); ? ? ? ? ? ? ? ? list.add(cellRangeAddressExcelVo); ? ? ? ? ? ? ? ? i++; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return list; ? ? }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java實現(xiàn)轉跳不同系統(tǒng)使用枚舉加switch的方式示例
今天小編就為大家分享一篇關于Java實現(xiàn)轉跳不同系統(tǒng)使用枚舉加switch的方式示例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12new出來的對象中無法使用@autowired進行對象bean注入問題
這篇文章主要介紹了基于new出來的對象中無法使用@autowired進行對象bean注入問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02spring?@value無法取值多個properties文件的解決
這篇文章主要介紹了spring?@value無法取值多個properties文件的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03