zoukankan      html  css  js  c++  java
  • poi处理excel基本操作时写的工具类

    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 ...
    昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
  • 相关阅读:
    反射、枚举
    WebService、Http请求、Socket请求
    RPC和REST的区别
    命名分组
    golang isPowerOfTwo判断是否是2的幂
    golang 判断平台是32位还是64位
    vue的permission.js详解
    windows 下完全卸载oracle 11的详细过程
    freemarker导出word
    freemarker详细教程从入门到精通(三)模板入门与指令
  • 原文地址:https://www.cnblogs.com/lgqrlchinese/p/13080145.html
Copyright © 2011-2022 走看看