POI自定义单元格类
POI自定義單元格類
在用POI做Excel導出的時候,單元格的創建是一個很頭疼的問題,對于有些表格中雜亂無章的單元格順序,比如:
這種樹形結構并不像橫向的表格有規律,所以就需要一行一行的插入。這是一項沒有啥技術含量且枯燥的事情,但是卻又不得不去做。因此我根據單元格的屬性,自定義了一個單元格類,稍微簡化了一下工作量。。
代碼塊
public class ExcelCell {/** sheet **/private HSSFSheet sheet;/** 行 **/private HSSFRow hRow;/** 列 **/private int column;/** 樣式 **/private HSSFCellStyle style;/** 值 **/private String value;/** 區域中最后一個單元格的行號 **/private Integer lastRow; /** 區域中最后一個單元格的列號 **/private Integer lastCol;public ExcelCell() {}public ExcelCell(HSSFSheet sheet, HSSFRow hRow, int column,HSSFCellStyle style, String value,Integer lastRow, Integer lastCol) {super();this.sheet = sheet;this.hRow = hRow;this.column = column;this.style = style;this.value = value;this.lastRow = lastRow;this.lastCol = lastCol;}public HSSFSheet getSheet() {return sheet;}public void setSheet(HSSFSheet sheet) {this.sheet = sheet;}public HSSFRow gethRow() {return hRow;}public void sethRow(HSSFRow hRow) {this.hRow = hRow;}public int getColumn() {return column;}public void setColumn(int column) {this.column = column;}public HSSFCellStyle getStyle() {return style;}public void setStyle(HSSFCellStyle style) {this.style = style;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}public Integer getLastRow() {return lastRow;}public void setLastRow(Integer lastRow) {this.lastRow = lastRow;}public Integer getLastCol() {return lastCol;}public void setLastCol(Integer lastCol) {this.lastCol = lastCol;}}每個單元格都應該具備以下屬性:
- 首行
- 首列
- 末行(用于單元格合并)
- 末列(用于單元格合并)
- 樣式
- 值
通過以上屬性,可以刻畫一個單元格,我們可以通過List保存所有的單元格,最后通過遍歷List來創建Excel表格
public static void batchCreateCell(HSSFSheet sheet, ExcelCell ec) {HSSFCell hCell = ec.gethRow().createCell(ec.getColumn());hCell.setCellValue(ec.getValue());hCell.setCellStyle(ec.getStyle());if (ec.getLastRow() != null && ec.getLastCol() != null) {CellRangeAddress cad = new CellRangeAddress(ec.gethRow().getRowNum(), ec.getLastRow().intValue(), ec.getColumn(), ec.getLastCol().intValue());sheet.addMergedRegion(cad);}}這樣稍微的簡化了代碼量。。但是對于某些單元格不能遍歷的問題,還是需要手動的在List里面add。。但若是每個單元格里面的值都是對象里的屬性的話,就可以通過遍歷對象的屬性往List里面塞值啦,具體怎么塞要看表格的類型,比如截圖中的樹形結構表格就可以用樹形的數據結構來封裝了,看起來還是挺容易的。。
以下為感想
在POI中,合并單元格后格式會有一些沒有意識到的bug。比如:
如果覺得沒啥問題,我們來看看打印預覽:
有的單元格的邊框顯示不全。這是因為POI對單元格的定義是:在每個Excel表格中,每一個初始單元格(未合并的)都是一個對象,對于每一個單元格,如果不使用createCell方法,這個單元格就不存在(對于打開的.xls或者.xlsx來說,這個單元格能看到,但是什么都沒有)。既然不存在這個單元格對象,也就不具備樣式的屬性了,所以不先定義就合并,結果就像上圖這樣,單元格邊框顯示不全。我的解決辦法是在合并之前就定義:
/*** * @Description: 對表格中所有單元格設置樣式* @param @param sheet* @param @param style* @param @param row 表格行數* @param @param column 表格列數* @return void * @throws* @author HJC* @date 2017-10-17*/public static void createRowAndColumn(HSSFSheet sheet, HSSFCellStyle style, int row, int column) {for (int i = 0; i < row; i++) {sheet.createRow((int) i);for (int j = 0; j < column; j++) {sheet.getRow(i).createCell(j);sheet.getRow(i).getCell(j).setCellStyle(style);}}//return sheet; }第一次用markdown。。代碼格式可能有點難看。。但我永遠堅持左大括號不換行╭(╯^╰)╮
總結
以上是生活随笔為你收集整理的POI自定义单元格类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 产品策划五:App界面设计风格
- 下一篇: 快收藏了!优秀的Linux工程师必备的8