码迷,mamicode.com
首页 > 其他好文 > 详细

poi处理excel基本操作时写的工具类

时间:2020-06-09 20:21:13      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:one   stc   类型   public   tor   ast   addm   etl   异常   

1.复制单元格

 1 ...
 2     public static void copyCell(Workbook wb, Cell srcCell, Cell distCell, boolean copyValueFlag) {
 3         //新建单元格样式
 4         CellStyle newStyle = wb.createCellStyle();
 5         //获取来源的单元格样式
 6         CellStyle srcStyle = srcCell.getCellStyle();
 7         //将来源的单元格样式克隆到新的单元格样式
 8         newStyle.cloneStyleFrom(srcStyle);
 9         //样式
10         newStyle.cloneStyleFrom(newStyle);
11         distCell.setCellStyle(newStyle);
12         //克隆单元格的评论
13         if (srcCell.getCellComment() != null) {
14             distCell.setCellComment(srcCell.getCellComment());
15         }
16         // 不同数据类型处理
17         CellType srcCellType = srcCell.getCellTypeEnum();
18         distCell.setCellType(srcCellType);
19         if (copyValueFlag) {
20             if (srcCellType == CellType.NUMERIC) {
21                 if (DateUtil.isCellDateFormatted(srcCell)) {
22                     distCell.setCellValue(srcCell.getDateCellValue());
23                 } else {
24                     distCell.setCellValue(srcCell.getNumericCellValue());
25                 }
26             } else if (srcCellType == CellType.STRING) {
27                 distCell.setCellValue(srcCell.getRichStringCellValue());
28             } else if (srcCellType == CellType.BLANK) {
29 
30             } else if (srcCellType == CellType.BOOLEAN) {
31                 distCell.setCellValue(srcCell.getBooleanCellValue());
32             } else if (srcCellType == CellType.ERROR) {
33                 distCell.setCellErrorValue(srcCell.getErrorCellValue());
34             } else if (srcCellType == CellType.FORMULA) {
35                 distCell.setCellFormula(srcCell.getCellFormula());
36             } else {
37             }
38         }
39     }
40 ...

2.行复制

 1 ...
 2     /**
 3      * 行复制功能
 4      *
 5      * @param wb 要复制到的Workbook
 6      * @param fromRow 复制到的行
 7      * @param toRow 来源的行
 8      * @param copyValueFlag true的时候赋值里面的值
 9      * @return [返回类型说明]
10      * @author 龙谷情
11      * @date 2020/6/9 19:42
12      * @exception/throws [异常类型] [异常说明]
13      * @since [v1.0]
14      */
15     public static void copyRow(Workbook wb, Row fromRow, Row toRow, boolean copyValueFlag) {
16         toRow.setHeight(fromRow.getHeight());
17         for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) {
18             Cell tmpCell = (Cell) cellIt.next();
19             Cell newCell = toRow.createCell(tmpCell.getColumnIndex());
20             //调用单元格赋值方法
21             copyCell(wb, tmpCell, newCell, copyValueFlag);
22         }
23         Sheet worksheet = fromRow.getSheet();
24         //获取合并单元格的数量
25         int mergedRegionsNum = worksheet.getNumMergedRegions();
26         for (int i = 0; i < mergedRegionsNum; i++) {
27             CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
28             if (cellRangeAddress.getFirstRow() == fromRow.getRowNum()) {
29                 CellRangeAddress newCellRangeAddress = new CellRangeAddress(toRow.getRowNum(), (toRow.getRowNum() +
30                         (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), cellRangeAddress
31                         .getFirstColumn(), cellRangeAddress.getLastColumn());
32                 worksheet.addMergedRegionUnsafe(newCellRangeAddress);
33             }
34         }
35     }
36 ...

 

poi处理excel基本操作时写的工具类

标签:one   stc   类型   public   tor   ast   addm   etl   异常   

原文地址:https://www.cnblogs.com/lgqrlchinese/p/13080145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!