POI設定Excel單元格樣式

2022-08-26 20:12:12 字數 3813 閱讀 1159

poi中可能會用到一些需要設定excel單元格格式的操作小結:

先獲取工作薄物件:

hssfworkbook wb = new hssfworkbook();

hssfsheet sheet = wb.createsheet();

hssfcellstyle cellstyle = wb.createcellstyle();

一、設定背景色:

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

cellstyle.setfillpattern(hssfcellstyle.solid_foreground);

二、設定邊框:

cellstyle.setborderbottom(hssfcellstyle.border_thin); //

下邊框 cellstyle.setborderleft(hssfcellstyle.border_thin);//

左邊框 cellstyle.setbordertop(hssfcellstyle.border_thin);//

上邊框 cellstyle.setborderright(hssfcellstyle.border_thin);//

右邊框

//設定單元格邊框顏色

cellstyle.setbottombordercolor(hssfcolor.red.index);

cellstyle.settopbordercolor(hssfcolor.green.index

);

cellstyle.setleftbordercolor(hssfcolor.blue.index

); 設定單元格邊框樣式

cellstyle.border_double 雙邊線

cellstyle.border_thin 細邊線

cellstyle.border_medium 中等邊線

cellstyle.border_dashed 虛線邊線

cellstyle.border_hair 小圓點虛線邊線

cellstyle.border_thick 粗邊線

三、設定居中:

cellstyle.setalignment(cellstyle.align_center);//

水平居中

cellstyle.setverticalalignment(cellstyle.vertical_center);//

垂直居中

//

設定單元格內容水平對其方式

居中對齊

左對齊

右對齊

//設定單元格內容垂直對其方式

上對齊

中對齊

下對齊

四、設定字型:

hssffont font =wb.createfont();

font.setfontname("黑體");

font.setfontheightinpoints((

short) 13);//

設定字型大小

font.setitalic(

true); //

設定字型為斜體字

font.setcolor(font.color_red); //

將字型設定為「紅色」

font.setunderline(font.u_double);

//新增(font.u_single單條下劃線/font.u_double雙條下劃線)

//是否新增刪除線

hssffont font2 =wb.createfont();

font2.setfontname("宋體");

font2.setboldweight(hssffont.boldweight_bold);

//粗體顯示

font2.setfontheightinpoints((short) 12);

cellstyle.setfont(font);

//選擇需要用到的字型格式

五、設定整列的樣式

sheet.setdefaultcolumnstyle(short column, cellstyle style)

六、設定自動換行:

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

七、合併單元格:

//

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

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

//此方法在poi3.8中已經被廢棄,建議使用下面乙個

注意匯入的jar包:import org.apache.poi.ss.util.cellrangeaddress

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

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

sheet.addmergedregion(region1);

八、設定行的高度

在excel中,每一行的高度是要求一致的,所以設定單元格的高度,其實就是設定行的高度,所以相關的屬性也應該在hssfrow上,它就是hssfrow.height和heightinpoints,這兩個屬性的區別在於heightinpoints的單位是點,而height的單位是1/20個點,所以height的值永遠是heightinpoints的20倍。

row sizerow = sheet.createrow(6);  

sizerow.setheightinpoints(30); //

設定行的高度

//或者

sizerow.setheight((short) (40*20)); // 設定行的高度

九、設定和讀取列寬

setcolumnwidth的第二個引數要乘以256,這是怎麼回事呢?其實,這個引數的單位是1/256個字元寬度,也就是說,這裡是把b列的寬度設定為了100個字元。

//

第乙個引數代表列id(從0開始),第2個引數代表寬度值

sheet.setcolumnwidth(0, 100*256);

//讀取列寬

int col1width = sheet1.getcolumnwidth(1);//

引數列的序號

十、設定預設的列寬和行高

一旦設定了這些屬性,如果某一行或者某一列沒有設定寬度,就會使用預設寬度或高度。

//

設定預設列寬和行高

sheet.setdefaultcolumnwidth(25);

(40*20));

sheet.setdefaultrowheightinpoints(20);

工具方法:

//

設定列寬()

public

static

void setcolumnwidth(hssfsheet sheet, int

colnum)

}

//

設定行的字型和樣式

public

void

setcellstyle(hssfworkbook workbook,hssfcell cell)

參考:poi的api:

poi匯出excel單元格樣式設定

final xssfworkbook wb new xssfworkbook is final sheet datahssfsheet wb.getsheetat 0 獲取到第乙個工作表 final sheet datahssfsheetadd wb.getsheetat 1 獲取到第二個工作表 d...

poi單元格設定

poi中可能會用到一些需要設定excel單元格格式的操作小結 先獲取工作薄物件 hssfworkbook wb new hssfworkbook hssfsheet sheet wb.createsheet hssfcellstyle setborder wb.createcellstyle 一 設...

POI匯出Excel 合併單元格

合併方法 sheet.addmergedregion new cellrangeaddress firstrow,lastrow,firstcol,lastcol 引數分別表示 開始行索引,結束行索引,開始列索引,結束列索引.使用poi大致步驟 1.建立excel模板,並建立輸入流 fileinpu...