使用POI匯出Excel最全工具類

2021-10-23 06:14:45 字數 4181 閱讀 2127

1:建立乙個excel**(注意在設定單元格的先關資訊時一定要:cell2.setcellstyle(style2)這種形式才能讓設定起作用)

hssfworkbook workbook=new hssfworkbook();//建立工作薄物件,這裡也可以設定sheet的name

hssfsheet sheet1 = workbook.createsheet();//建立工作表sheet物件

2:設定列寬

sheet1.setcolumnwidth(0, 256*width+184);//設定第n列的寬度:0:第一列。width:要設定的寬度

3:設定行高

hssfrow row2 = sheet1.createrow(0);//設定第一行,從零開始

hssfcell cell2 = row2.createcell(0);//第一行第一列

row1.setheightinpoints(35);//行高

4:呼叫背景顏色板

hssfpalette palette = workbook.getcustompalette();//拿到palette顏色板,這個是針對於乙個sheet頁中使用一種顏色(下邊還有另外設定背景色的方法)

4.1:不確定夠單元格顏色的用上邊的顏色板和這個組合進行rgb顏色值如(ccffff)轉換成16進製制顏色碼(204,255,255)的組合(style2:引數在下邊設定樣式)

palette.setcoloratindex(hssfcolor.lime.index, (byte) 204, (byte) 255, (byte) 255);

style2.setfillforegroundcolor(hssfcolor.lime.index);//設定背景顏色

4.2:確定行背景色的(style2:引數在下邊設定樣式)

style2.setfillforegroundcolor(hssfcolor.pink.index);//設定單元格背景色:確定單元格顏色的用這個

4.3:設定單元格背景色(下邊附帶有49中顏色和**)

style2.setfillpattern(hssfcellstyle.solid_foreground);//填充單元格

style2.setfillforegroundcolor(indexedcolors.lemon_chiffon.getindex());//設定背景顏色(第二種設定背景色的方法)

上邊的1-49對應下邊1-49行**(已載入原文連線位址,尊重原創)

5:設定樣式

hssfcellstyle style2 = workbook.createcellstyle();

style2.setwraptext(true);//指定當單元格內容顯示不下時自動換行

style2.setalignment(hssfcellstyle.align_center);//水平居中

style2.setverticalalignment(hssfcellstyle.vertical_center);//垂直居中

style2.setbordertop(hssfcellstyle.border_thin);//上邊框

style2.settopbordercolor(hssfcolor.black.index);//上邊框顏色

style2.setborderright(hssfcellstyle.border_thin);//右邊框

style2.setrightbordercolor(hssfcolor.black.index);//右邊框顏色

style2.setwraptext(true);//自動換行

style2.setindention((short)5);//縮排

style2.setrotation((short)60);//文字旋轉,這裡的取值是從-90到90,而不是0-180度。

cell2.setcellstyle(style2);

如果是左側對齊就是 hssfcellstyle.align_fill;

如果是居中對齊就是 hssfcellstyle.align_center;

如果是右側對齊就是 hssfcellstyle.align_right;

如果是跨列舉中就是 hssfcellstyle.align_center_selection;

如果是兩端對齊就是 hssfcellstyle.align_justify;

如果是填充就是 hssfcellstyle.align_fill;

如果是靠上就是 hssfcellstyle.vertical_top;

如果是居中就是 hssfcellstyle.vertical_center;

如果是靠下就是 hssfcellstyle.vertical_bottom;

如果是兩端對齊就是 hssfcellstyle.vertical_justify;

6:設定邊框

style2.setbordertop(hssfcellstyle.border_dotted);//上邊框

style2.setborderbottom(hssfcellstyle.border_thick);//下邊框

style2.setborderleft(hssfcellstyle.border_double);//左邊框

style2.setborderright(hssfcellstyle.border_slanted_dash_dot);//右邊框

style2.settopbordercolor(hssfcolor.red.index);//上邊框顏色

style2.setbottombordercolor(hssfcolor.blue.index);//下邊框顏色

style2.setleftbordercolor(hssfcolor.green.index);//左邊框顏色

style2.setrightbordercolor(hssfcolor.pink.index);//右邊框顏色

7:設定字型

hssffont font = workbook.createfont();

font.setfontname("華文行楷");//設定字型名稱

font.setfontheightinpoints((short)28);//設定字型大小

font.setcolor(hssfcolor.red.index);//設定字型顏色

font.setunderline(fontformatting.u_single);//設定下劃線

font.settypeoffset(fontformatting.ss_super);//設定上標下標

font.setstrikeout(true);//設定刪除線

style2.setfont(font);

cell2.setcellstyle(style2);

8:合併列、行

cellrangeaddress region1=new cellrangeaddress(0, 1, 0, 0);//合併行

sheet1.addmergedregion(region1);

region1=new cellrangeaddress(0, 1, 1, 44);//合併列

sheet1.addmergedregion(region1);

cellrangeaddress(firstrow, lastrow, firstcol, lastcol),引數的說明:

firstrow:區域中第乙個單元格的行號。

lastrow 區域中最後乙個單元格的行號。

firstcol 區域中第乙個單元格的列號。

lastcol 區域中最後乙個單元格的列號

9:固定某一行某一列

sheet1.createfreezepane(1, 4);//設定此sheet頁前四行第一列不隨滾動條的滾動而滾動

本部落格僅供交流和學習,對於使用在其他用途及產生的後果,由使用者承擔,最終解釋權歸本人所有,請尊重原創,如需使用請標明鏈結位址

使用poi匯出excel

使用poi匯出excel,其中涉及到居中 字型加粗 合併單元格 namespace org examinee results inputname inputstream buffersize 4096 public class orgexamineeexportaction extends nati...

使用POI匯出Excel示例

使用poi匯出excel,需要的jar包 poi bin 3.9 20121203.zip 示例 如下 import org.apache.poi.hssf.usermodel.hssfcell import org.apache.poi.hssf.usermodel.hssfcellstyle i...

使用POI匯出excel檔案

fileoutputstream fileout new fileoutputstream workbook wb new hssfworkbook 建議使用介面來宣告變數 面向介面思想,可以方便使用介面的不同實現 wb.write fileout 寫入檔案 fileout.close sheet ...