本文主要是介绍【POI】Java-POI复制Sheet(.xlsx),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码只作笔记用,理解用法即可,不保证无误,可能需要微调
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.3</version></dependency>
public class XSSFUtils {/*** 复制指定的sheet,并获取一个新的sheet*/public XSSFSheet copyAndGetNewSheet(XSSFWorkbook workbook, XSSFSheet fromSheet, String sheetName) {XSSFSheet newSheet = workbook.createSheet(sheetName);XSSFUtils.copySheet(workbook, fromSheet, newSheet);// 复制列宽copyColWidth(fromSheet, newSheet);return newSheet;}/*** 复制行*/private static void copySheet(XSSFWorkbook workbook, XSSFSheet fromSheet, XSSFSheet toSheet) {// 复制所有的合并单元格copyMergedCells(fromSheet, toSheet);// 设置sheet页缩放toSheet.setZoom(70);// 复制所有行for (Iterator it = fromSheet.rowIterator(); it.hasNext();) {XSSFRow fromRow = (XSSFRow) it.next();XSSFRow toRow = toSheet.createRow(fromRow.getRowNum());copyRow(fromRow, toRow);}// 复制图片copyPictures(fromSheet, toSheet);}/*** 复制行*/private static void copyRow(XSSFRow fromRow, XSSFRow toRow) {// 复制行高和行样式toRow.setHeight(fromRow.getHeight());toRow.setRowStyle(fromRow.getRowStyle());// 复制该行所有单元格for (Iterator it = fromRow.cellIterator(); it.hasNext();) {XSSFCell fromCell = (XSSFCell) it.next();XSSFCell toCell = toRow.createCell(fromCell.getColumnIndex());copyCell(fromCell, toCell);}}/*** 复制单元格*/private static void copyCell(XSSFCell fromCell, XSSFCell toCell) {// 复制单元格样式toCell.setCellStyle((XSSFCellStyle) fromCell.getCellStyle().clone());// 复制列宽的方法在XSSFSheet里,所以本方法不做列宽的复制// 按照单元格类型复制单元格内容toCell.setCellType(fromCell.getCellTypeEnum());switch (fromCell.getCellTypeEnum()) {case NUMERIC :if (DateUtil.isCellDateFormatted(fromCell)) {toCell.setCellValue(fromCell.getDateCellValue());} else {toCell.setCellValue(fromCell.getNumericCellValue());}break;case STRING :toCell.setCellValue(fromCell.getStringCellValue());break;case BOOLEAN :toCell.setCellValue(fromCell.getBooleanCellValue());break;case ERROR :toCell.setCellValue(fromCell.getErrorCellValue());break;case FORMULA :toCell.setCellFormula(fromCell.getCellFormula());default:break;}}/*** 复制合并单元格*/private static void copyMergedCells(XSSFSheet fromSheet, XSSFSheet toSheet) {for (int i = 0; i < fromSheet.getNumMergedRegions(); i++) {toSheet.addMergedRegion(fromSheet.getMergedRegion(i));}}/*** 复制图片*/private static void copyPictures(XSSFSheet fromSheet, XSSFSheet toSheet) {List<POIXMLDocumentPart> partList = fromSheet.getRelations();for (POIXMLDocumentPart part : partList) {if (part instanceof XSSFDrawing) {XSSFDrawing drawing = (XSSFDrawing) part;List<XSSFShape> shapes = drawing.getShapes();for (XSSFShape shape : shapes) {XSSFPicture picture = (XSSFPicture) shape;XSSFClientAnchor anchor = picture.getPreferredSize();XSSFDrawing toDrawing = toSheet.createDrawingPatriarch();toDrawing.createPicture(anchor, 0);}}}}/*** 复制列宽*/private void copyColWidth(XSSFSheet fromSheet, XSSFSheet toSheet) {// 注意row0 的getLastCellNum();可能会不准确int lastColIndex = fromSheet.getRow(0).getLastCellNum();for (int i = 0; i <= lastColIndex; i++) {toSheet.setColumnWidth(i, fromSheet.getColumnWidth(0));}}
}
这篇关于【POI】Java-POI复制Sheet(.xlsx)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!