原文地址:https://www.cnblogs.com/wong-/p/13959433.html
背景:使用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_CENTER,CellStyle.VERTICAL_CENTER,HSSFColor.GREY_25_PERCENT.index,CellStyle.SOLID_FOREGROUND,CellStyle.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_STRING,HSSFCell.CELL_TYPE_NUMERIC,HSSFCell.CELL_TYPE_BOOLEAN,HSSFCell.CELL_TYPE_FORMULA,HSSFCell.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);//右邊框

