poi maven依赖与 poi新旧版本样式设置更新问题

背景:使用poi 进行导入导出功能

1.maven依赖

<!--文件上传组件-->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
<!--读取excel文件-->
<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>

<!-- servlet插件 -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>

<!-- 基于poi的excel反射插件 -->
<dependency>
    <groupId>com.github.crab2died</groupId>
    <artifactId>Excel4J</artifactId>
    <version>2.1.4-Final2</version>
</dependency>

<!-- lombok依赖 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

<!-- 日志依赖 -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>

2.poi新旧版本样式设置更新问题(变更poi jar包的版本,或多或少都是会有一些冲突的)
旧版本
CellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);       
titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); titleCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN); titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN); titleCellStyle.setBorderTop(CellStyle.BORDER_THIN); titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);

其中,CellStyle.ALIGN_CENTERCellStyle.VERTICAL_CENTERHSSFColor.GREY_25_PERCENT.indexCellStyle.SOLID_FOREGROUNDCellStyle.BORDER_THIN都会报错异常,应改为:

新版本

        CellStyle titleCellStyle = workbook.createCellStyle();
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);//设置图案颜色
        titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置图案样式

        titleCellStyle.setBorderBottom(BorderStyle.THIN);//下边框
        titleCellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        titleCellStyle.setBorderTop(BorderStyle.THIN);//上边框
        titleCellStyle.setBorderRight(BorderStyle.THIN);//右边框

2.读取及操作excel单元格数据,对数据做类型判断时:
Cell.CELL_TYPE_STRINGHSSFCell.CELL_TYPE_NUMERICHSSFCell.CELL_TYPE_BOOLEANHSSFCell.CELL_TYPE_FORMULAHSSFCell.CELL_TYPE_BLANK
都会提示不存在,应该成对应的CellType.STRING,CellType.NUMERIC,CellType.BOOLEAN,CellType.FORMULA,CellType.BLANK;
3.excel写入图片时:

    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

     try {

         byteArrayOut.write(Base64.getDecoder().decode('图片base64的编码'));

         // anchor主要用于设置图片的属性
         XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255, (short) i, rowNum + 1,
                 (short) i + 1, rowNum + 2);
         anchor.setAnchorType(3);
         // 插入图片
         patriarch.createPicture(anchor,
                 workbook.addPicture(byteArrayOut.toByteArray(), ClientAnchor.MOVE_DONT_RESIZE));

     } catch (IOException e) {
         logger.error(e.getMessage(), e);
     }

代码中anchor.setAnchorType(3);
ClientAnchor.MOVE_DONT_RESIZE,报错异常,应改为:
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
ClientAnchor.AnchorType.MOVE_DONT_RESIZE.value;

小结,poi版本问题产生报错汇总表(后续待补充)

poi3.+版本poi4.+版本用途
Cell.CELL_TYPE_STRING CellType.STRING 单元格数据格式判断
HSSFCell.CELL_TYPE_NUMERIC CellType.NUMERIC 单元格数据格式判断
CellStyle.ALIGN_CENTER HorizontalAlignment.CENTER 单元格水平居中
CellStyle.VERTICAL_CENTER VerticalAlignment.CENTER 单元格垂直居中
HSSFColor.GREY_25_PERCENT.index IndexedColors.GREY_25_PERCENT.index 设置图案颜色
CellStyle.SOLID_FOREGROUND FillPatternType.SOLID_FOREGROUND 设置图案样式
CellStyle.BORDER_THIN BorderStyle.THIN 边框
ClientAnchor.MOVE_DONT_RESIZE ClientAnchor.AnchorType.MOVE_DONT_RESIZE.value 单元格插入图片
1.对齐方式

// 居中格式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 
// 修改后,左对齐右对齐修改CENTER
style.setAlignment(HorizontalAlignment.CENTER);
2.边框样式
 
// 最细边框
style.setBorderBottom(CellStyle.BORDER_THIN);
 
// 修改为,其他边框样式详见BorderStyle
style.setBorderBottom(BorderStyle.THIN);
3.获取颜色索引

// 蓝色RGB索引
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
 
// 修改为,其他颜色参考:IndexedColors
style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
4.设置填充样式

 
// SOLID_FOREGROUND纯色使用前景颜色填充
 
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
 
// 修改为,SOLID_FOREGROUND纯色使用前景颜色填充
 
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
5.字体样式

// 字体加粗
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
 
// 修改为
font.setBold(true);
6.单元格数据类型

 
// 字符串类型
Cell.CELL_TYPE_STRING
 
// 修改为
CellType.STRING
 
// 数字类型
Cell.CELL_TYPE_NUMERIC
 
// 修改为
CellType.NUMERIC
//类似的其他类型 详见 CellType
    // 表头样式
    public static XSSFCellStyle getCellStyle(XSSFWorkbook wb) {
        XSSFCellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)12);// 设置字体大小
//        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
        font.setBold(true);//字体加粗
        style.setFillForegroundColor(HSSFColor.LIME.index);// 设置背景色
//        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置图案样式
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置图案样式

//        style.setAlignment(HSSFCellStyle.SOLID_FOREGROUND);// 让单元格居中
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setAlignment(HorizontalAlignment.CENTER);//左右居中
//        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
        style.setWrapText(true);// 设置自动换行
        style.setFont(font);
        return style;
    }
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND)------>
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN)------>
style.setBorderBottom(BorderStyle.THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN)------>
style.setBorderLeft(BorderStyle.THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN)------>
style.setBorderRight(BorderStyle.THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN)------>
style.setBorderTop(BorderStyle.THIN);

titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION)// 水平居中------>
titleStyle.setAlignment(HorizontalAlignment.CENTER)

titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中------>
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);

titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 设置粗体------>
titleFont.setBold(true);

       CellStyle style = wb.createCellStyle();
        CellStyle style2 = wb.createCellStyle();
        //创建表头
        Font font = wb.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short) 11);//设置字体大小
        style.setFont(font);//选择需要用到的字体格式

        style.setFillForegroundColor(HSSFColor.YELLOW.index);// 设置背景色

//        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置图案样式
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置图案样式

//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        style.setAlignment(HorizontalAlignment.CENTER); // 居中

//        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderBottom(BorderStyle.THIN);//下边框

//        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style.setBorderRight(BorderStyle.THIN);//右边框

        style2.setFont(font);//选择需要用到的字体格式

        style2.setFillForegroundColor(HSSFColor.WHITE.index);// 设置背景色

//        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置图案样式
        style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置图案样式

//        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
        style2.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

//        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平向下居中
        style2.setAlignment(HorizontalAlignment.CENTER); // 水平向下居中

//        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style2.setBorderTop(BorderStyle.THIN);//上边框

//        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style2.setBorderBottom(BorderStyle.THIN);//下边框

//        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style2.setBorderLeft(BorderStyle.THIN);//左边框

//        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style2.setBorderRight(BorderStyle.THIN);//右边框
posted @ 2020-11-11 16:40  database-  阅读(4920)  评论(0编辑  收藏  举报