POI自定義單元格類

2021-08-09 12:08:48 字數 2409 閱讀 7683

在用poi做excel匯出的時候,單元格的建立是乙個很頭疼的問題,對於有些**中雜亂無章的單元格順序,比如:

這種樹形結構並不像橫向的**有規律,所以就需要一行一行的插入。這是一項沒有啥技術含量且枯燥的事情,但是卻又不得不去做。因此我根據單元格的屬性,自定義了乙個單元格類,稍微簡化了一下工作量。。

public

class

excelcell

public

excelcell(hssfsheet sheet, hssfrow hrow, int column,

hssfcellstyle style, string value,

integer lastrow, integer lastcol)

public hssfsheet getsheet()

public

void

setsheet(hssfsheet sheet)

public hssfrow gethrow()

public

void

sethrow(hssfrow hrow)

public

intgetcolumn()

public

void

setcolumn(int column)

public hssfcellstyle getstyle()

public

void

setstyle(hssfcellstyle style)

public string getvalue()

public

void

setvalue(string value)

public integer getlastrow()

public

void

setlastrow(integer lastrow)

public integer getlastcol()

public

void

setlastcol(integer lastcol)

}

每個單元格都應該具備以下屬性:

通過以上屬性,可以刻畫乙個單元格,我們可以通過list儲存所有的單元格,最後通過遍歷list來建立excel**

public static void batchcreatecell(hssfsheet sheet, excelcell ec) 

}

這樣稍微的簡化了**量。。但是對於某些單元格不能遍歷的問題,還是需要手動的在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)

}//return sheet;

}

第一次用markdown。。**格式可能有點難看。。但我永遠堅持左大括號不換行╭(╯^╰)╮

自定義單元格

自定義單元格有兩種方法 1 通重載入xib的方式 如果單元格的樣式都一樣的話,這種方法很便捷 注 這種方式只能用於單元格樣式一樣的情況 2 全部手寫 通重載入xib的方式自定義單元格 設定重用id cztableviewcell.h檔案 import class czgoods inte ce cz...

jQuery MiniUI自定義單元格

監聽處理 drawcell 事件 使用 drawcell 事件,可以自定義單元格內容 樣式 行樣式等。grid.on drawcell function e 給年齡,增加 歲 字串 if field age 給帳號列,增加背景色 if field loginname 超過1萬工資,紅色標識 if f...

poi合併單元格

注 我這裡合併的是列的相同單元格,效果如下 poi合併單元格的方法是addmergedregion 合併單元格 param sheet 要合併單元格的excel 的sheet param cellline 要合併的列 param startrow 要合併列的開始行 param endrow 要合併列...