poi匯出excel 之API整理

2021-08-03 23:04:47 字數 2161 閱讀 4319

hssfworkbook wb = new hssfworkbook();

hssfsheet sheet = wb.createsheet();

hssfcellstyle setborder = wb.createcellstyle();

一、設定背景色:

setborder.setfillforegroundcolor((short) 13);// 設定背景色

setborder.setfillpattern(hssfcellstyle.solid_foreground);

二、設定邊框:

setborder.setborderbottom(hssfcellstyle.border_thin); //下邊框

setborder.setborderleft(hssfcellstyle.border_thin);//左邊框

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

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

三、設定居中:

setborder.setalignment(hssfcellstyle.align_center); // 居中

四、設定字型:

hssffont font = wb.createfont();

font.setfontname("黑體");

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

hssffont font2 = wb.createfont();

font2.setfontname("仿宋_gb2312");

font2.setboldweight(hssffont.boldweight_bold);//粗體顯示

font2.setfontheightinpoints((short) 12);

setborder.setfont(font);//選擇需要用到的字型格式

五、設定列寬:

sheet.setcolumnwidth(0, 3766); //第乙個引數代表列id(從0開始),第2個引數代表寬度值

六、設定自動換行:

setborder.setwraptext(true);//設定自動換行

七、合併單元格:

region region1 = new region(0, (short) 0, 0, (short) 6);

//引數1:行號 引數2:起始列號 引數3:行號 引數4:終止列號

sheet.addmergedregion(region1);

或者用cellrangeaddress region1 = new cellrangeaddress(rownumber, rownumber, (short) 0, (short) 11);

但應注意兩個構造方法的引數不是一樣的,具體使用哪個取決於poi的不同版本。

sheet.addmergedregion(region1);

目前用過的就這麼多,後續有新的會繼續新增。

八、加邊框

hssfcellstyle cellstyle= wookbook.createcellstyle();

cellstyle.setalignment(hssfcellstyle.align_center);

cellstyle.setborderbottom(hssfcellstyle.borderborder_medium);

cellstyle.setbottombordercolor(hssfcolor.black.index);

cellstyle.setborderleft(hssfcellstyle.border_medium);

cellstyle.setleftbordercolor(hssfcolor.black.index);

cellstyle.setborderright(hssfcellstyle.border_medium);

cellstyle.setrightbordercolor(hssfcolor.black.index);

cellstyle.setbordertop(hssfcellstyle.border_medium);

cellstyle.settopbordercolor(hssfcolor.black.index);

POI匯出Excel檔案

workbook workbook exportservice.exportfile request,student outputstream out null try catch ioexception e finally catch exception e 實現類 設定各列寬度 short st...

利用poi匯出excel

最近接了乙個任務,要求把資料庫內容提取成excel,乙個excel動態建立多個sheet,乙個sheet裡面顯示一條資訊,然後再根據此條資訊的結果,再去子表獲取結果,如一條學生資訊,對應顯示該學生的所有詳細資訊。如圖所示 工程中匯入 public static void writeexcelnew ...

POI匯出Excel筆記

匯出excel是個常用的問題,然而這一次摸索之後並不知道下一次是什麼時候再用,記錄一下,方便自己 基本的練習 工作簿物件,所有的sheet都在在這個物件上操作,建立樣式 建立sheet hssfworkbook workbook new hssfworkbook 乙個 頁面,可以用default構造...