POI自定义单元格类
POI自定義單元格類
在用POI做Excel導(dǎo)出的時(shí)候,單元格的創(chuàng)建是一個(gè)很頭疼的問題,對(duì)于有些表格中雜亂無章的單元格順序,比如:
這種樹形結(jié)構(gòu)并不像橫向的表格有規(guī)律,所以就需要一行一行的插入。這是一項(xiàng)沒有啥技術(shù)含量且枯燥的事情,但是卻又不得不去做。因此我根據(jù)單元格的屬性,自定義了一個(gè)單元格類,稍微簡(jiǎn)化了一下工作量。。
代碼塊
public class ExcelCell {/** sheet **/private HSSFSheet sheet;/** 行 **/private HSSFRow hRow;/** 列 **/private int column;/** 樣式 **/private HSSFCellStyle style;/** 值 **/private String value;/** 區(qū)域中最后一個(gè)單元格的行號(hào) **/private Integer lastRow; /** 區(qū)域中最后一個(gè)單元格的列號(hào) **/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;}}每個(gè)單元格都應(yīng)該具備以下屬性:
- 首行
- 首列
- 末行(用于單元格合并)
- 末列(用于單元格合并)
- 樣式
- 值
通過以上屬性,可以刻畫一個(gè)單元格,我們可以通過List保存所有的單元格,最后通過遍歷List來創(chuàng)建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);}}這樣稍微的簡(jiǎn)化了代碼量。。但是對(duì)于某些單元格不能遍歷的問題,還是需要手動(dòng)的在List里面add。。但若是每個(gè)單元格里面的值都是對(duì)象里的屬性的話,就可以通過遍歷對(duì)象的屬性往List里面塞值啦,具體怎么塞要看表格的類型,比如截圖中的樹形結(jié)構(gòu)表格就可以用樹形的數(shù)據(jù)結(jié)構(gòu)來封裝了,看起來還是挺容易的。。
以下為感想
在POI中,合并單元格后格式會(huì)有一些沒有意識(shí)到的bug。比如:
如果覺得沒啥問題,我們來看看打印預(yù)覽:
有的單元格的邊框顯示不全。這是因?yàn)镻OI對(duì)單元格的定義是:在每個(gè)Excel表格中,每一個(gè)初始單元格(未合并的)都是一個(gè)對(duì)象,對(duì)于每一個(gè)單元格,如果不使用createCell方法,這個(gè)單元格就不存在(對(duì)于打開的.xls或者.xlsx來說,這個(gè)單元格能看到,但是什么都沒有)。既然不存在這個(gè)單元格對(duì)象,也就不具備樣式的屬性了,所以不先定義就合并,結(jié)果就像上圖這樣,單元格邊框顯示不全。我的解決辦法是在合并之前就定義:
/*** * @Description: 對(duì)表格中所有單元格設(shè)置樣式* @param @param sheet* @param @param style* @param @param row 表格行數(shù)* @param @param column 表格列數(shù)* @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。。代碼格式可能有點(diǎn)難看。。但我永遠(yuǎn)堅(jiān)持左大括號(hào)不換行╭(╯^╰)╮
總結(jié)
以上是生活随笔為你收集整理的POI自定义单元格类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 产品策划五:App界面设计风格
- 下一篇: 任正非:到底什么样的人才能当干部