日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 操作POI 之复制sheet页

發布時間:2023/12/4 java 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 操作POI 之复制sheet页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

來點干貨直接上代碼,就不細說了

package com.qs.web.tools.core.excel;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

import java.util.TreeSet;


import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.ss.util.CellRangeAddress;


public ? class ExcelCopySheetUtil { ?

??

? ? public ExcelCopySheetUtil() { ?

? ? } ?

??

? ? public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet) { ?

? ? ? ? copySheets(newSheet, sheet, true); ?

? ? } ?

??

? ? public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, ?

? ? ? ? ? ? boolean copyStyle) { ?

? ? ? ? int maxColumnNum = 0; ?

? ? ? ? Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() ?

? ? ? ? ? ? ? ? : null; ?

? ? ? ? for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { ?

? ? ? ? ? ? HSSFRow srcRow = sheet.getRow(i); ?

? ? ? ? ? ? HSSFRow destRow = newSheet.createRow(i); ?

? ? ? ? ? ? if (srcRow != null) { ?

? ? ? ? ? ? ? ? ExcelCopySheetUtil.copyRow(sheet, newSheet, srcRow, destRow, ?

? ? ? ? ? ? ? ? ? ? ? ? styleMap); ?

? ? ? ? ? ? ? ? if (srcRow.getLastCellNum() > maxColumnNum) { ?

? ? ? ? ? ? ? ? ? ? maxColumnNum = srcRow.getLastCellNum(); ?

? ? ? ? ? ? ? ? } ?

? ? ? ? ? ? } ?

? ? ? ? } ?

? ? ? ? for (int i = 0; i <= maxColumnNum; i++) { ? ?//設置列寬 ?

? ? ? ? ? ? newSheet.setColumnWidth(i, sheet.getColumnWidth(i)); ?

? ? ? ? } ?

? ? } ?

??

? ? /**?

? ? ?* 復制并合并單元格?

? ? ?* @param ?newSheet?

? ? ?* @param ?sheet?

? ? ?* @param ?copyStyle?

? ? ?*/ ?

? ? public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, ?

? ? ? ? ? ? HSSFRow srcRow, HSSFRow destRow, ?

? ? ? ? ? ? Map<Integer, HSSFCellStyle> styleMap) { ?

? ? ? ? Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>(); ?

? ? ? ? destRow.setHeight(srcRow.getHeight()); ?

? ? ? ? int deltaRows = destRow.getRowNum() - srcRow.getRowNum(); //如果copy到另一個sheet的起始行數不同 ?

? ? ? ? for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) { ?

? ? ? ? ? ? HSSFCell oldCell = srcRow.getCell(j); // old cell ?

? ? ? ? ? ? HSSFCell newCell = destRow.getCell(j); // new cell ?

? ? ? ? ? ? if (oldCell != null) { ?

? ? ? ? ? ? ? ? if (newCell == null) { ?

? ? ? ? ? ? ? ? ? ? newCell = destRow.createCell(j); ?

? ? ? ? ? ? ? ? } ?

? ? ? ? ? ? ? ? copyCell(oldCell, newCell, styleMap); ?

? ? ? ? ? ? ? ? CellRangeAddress mergedRegion = getMergedRegion(srcSheet, ?

? ? ? ? ? ? ? ? ? ? ? ? srcRow.getRowNum(), (short) oldCell.getColumnIndex()); ?

? ? ? ? ? ? ? ? if (mergedRegion != null) { ?

? ? ? ? ? ? ? ? ? ? CellRangeAddress newMergedRegion = new CellRangeAddress( ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? mergedRegion.getFirstRow() + deltaRows, ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? mergedRegion.getLastRow() + deltaRows, mergedRegion ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .getFirstColumn(), mergedRegion ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .getLastColumn()); ?

? ? ? ? ? ? ? ? ? ? CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper( ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? newMergedRegion); ?

? ? ? ? ? ? ? ? ? ? if (isNewMergedRegion(wrapper, mergedRegions)) { ?

? ? ? ? ? ? ? ? ? ? ? ? mergedRegions.add(wrapper); ?

? ? ? ? ? ? ? ? ? ? ? ? destSheet.addMergedRegion(wrapper.range); ?

? ? ? ? ? ? ? ? ? ? } ?

? ? ? ? ? ? ? ? } ?

? ? ? ? ? ? } ?

? ? ? ? } ?

? ? }?

??

? ? /**?

? ? ?* 把原來的Sheet中cell(列)的樣式和數據類型復制到新的sheet的cell(列)中 ? ? ?* ?

? ? ?* @param ?oldCell?

? ? ?* @param ?newCell?

? ? ?* @param styleMap?

? ? ?*/ ?

? ? public static void copyCell(HSSFCell oldCell, HSSFCell newCell, ?

? ? ? ? ? ? Map<Integer, HSSFCellStyle> styleMap) { ?

? ? ? ? if (styleMap != null) { ?

? ? ? ? ? ? if (oldCell.getSheet().getWorkbook() == newCell.getSheet() ?

? ? ? ? ? ? ? ? ? ? .getWorkbook()) { ?

? ? ? ? ? ? ? ? newCell.setCellStyle(oldCell.getCellStyle()); ?

? ? ? ? ? ? } else { ?

? ? ? ? ? ? ? ? int stHashCode = oldCell.getCellStyle().hashCode(); ?

? ? ? ? ? ? ? ? HSSFCellStyle newCellStyle = styleMap.get(stHashCode); ?

? ? ? ? ? ? ? ? if (newCellStyle == null) { ?

? ? ? ? ? ? ? ? ? ? newCellStyle = newCell.getSheet().getWorkbook() ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? .createCellStyle(); ?

? ? ? ? ? ? ? ? ? ? newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); ?

? ? ? ? ? ? ? ? ? ? styleMap.put(stHashCode, newCellStyle); ?

? ? ? ? ? ? ? ? } ?

? ? ? ? ? ? ? ? newCellStyle.setLocked(false);

? ? ? ? ? ? ? ? newCell.setCellStyle(newCellStyle); ?

? ? ? ? ? ? } ?

? ? ? ? } ?

? ? ? ? switch (oldCell.getCellType()) { ?

? ? ? ? case HSSFCell.CELL_TYPE_STRING: ?

? ? ? ? ? ? newCell.setCellValue(oldCell.getStringCellValue()); ?

? ? ? ? ? ? break; ?

? ? ? ? case HSSFCell.CELL_TYPE_NUMERIC: ?

? ? ? ? ? ? newCell.setCellValue(oldCell.getNumericCellValue()); ?

? ? ? ? ? ? break; ?

? ? ? ? case HSSFCell.CELL_TYPE_BLANK: ?

? ? ? ? ? ? newCell.setCellType(HSSFCell.CELL_TYPE_BLANK); ?

? ? ? ? ? ? break; ?

? ? ? ? case HSSFCell.CELL_TYPE_BOOLEAN: ?

? ? ? ? ? ? newCell.setCellValue(oldCell.getBooleanCellValue()); ?

? ? ? ? ? ? break; ?

? ? ? ? case HSSFCell.CELL_TYPE_ERROR: ?

? ? ? ? ? ? newCell.setCellErrorValue(oldCell.getErrorCellValue()); ?

? ? ? ? ? ? break; ?

? ? ? ? case HSSFCell.CELL_TYPE_FORMULA: ?

? ? ? ? ? ? newCell.setCellFormula(oldCell.getCellFormula()); ?

? ? ? ? ? ? break; ?

? ? ? ? default: ?

? ? ? ? ? ? break; ?

? ? ? ? } ?

??

? ? } ?

??

? ? // 獲取merge對象 ?

? ? public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, ?

? ? ? ? ? ? short cellNum) { ?

? ? ? ? for (int i = 0; i < sheet.getNumMergedRegions(); i++) { ?

? ? ? ? ? ? CellRangeAddress merged = sheet.getMergedRegion(i); ?

? ? ? ? ? ? if (merged.isInRange(rowNum, cellNum)) { ?

? ? ? ? ? ? ? ? return merged; ?

? ? ? ? ? ? } ?

? ? ? ? } ?

? ? ? ? return null; ?

? ? } ?

??

? ? private static boolean isNewMergedRegion( ?

? ? ? ? ? ? CellRangeAddressWrapper newMergedRegion, ?

? ? ? ? ? ? Set<CellRangeAddressWrapper> mergedRegions) { ?

? ? ? ? boolean bool = mergedRegions.contains(newMergedRegion); ?

? ? ? ? return !bool; ?

? ? } ?

??

} ?


轉載于:https://my.oschina.net/zhangdayue/blog/419354

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java 操作POI 之复制sheet页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。