使用poi向excel中插入內容遇到的問題總結

2021-07-22 03:58:48 字數 3967 閱讀 2681

1、如何插入?

在poi中有hssfpatriarch物件,該物件為畫圖的頂級管理器,乙個sheet只可以建立乙個。它的createpicture(anchor,pictureindex)方法可以在excel中插入一張。

具體**我們可以封裝成乙個方法:

public

static

void

setpicture(workbook wb,hssfpatriarch patriarch,string path, int irowstart, int icolstart, int irowstop, int icolstop)

}

2、如何畫線?

同樣的,畫線也是由hssfpatriarch物件建立的,具體封裝**如下

public

static

void

drawline(workbook wb,hssfpatriarch patriarch,int irowstart, int icolstart, int irowstop, int icolstop)

3、 如何畫矩形?

hssfsheet sheet = workbook.createsheet("test");// 建立工作表(sheet)

hssfpatriarch patriarch=sheet.createdrawingpatriarch();

hssfclientanchor anchor = new hssfclientanchor(255,122, 255, 122, (short)1, 0,(short)4, 3);

hssf******shape rec = patriarch.create******shape(anchor);

rec.setshapetype(hssf******shape.object_type_rectangle);

rec.setlinestyle(hssfshape.linestyle_dashgel);//設定邊框樣式

rec.setfillcolor(255, 0, 0);//設定填充色

rec.setlinewidth(25400);//設定邊框寬度

rec.setlinestylecolor(0, 0, 255);//設定邊框顏色

4、如何設定單元格換行?

cellstyle cellstyle = wb.createcellstyle();

cellstyle.setwraptext(true);//true表示自動換行

5、如何進行資料格式化?

資料格式化要借助hssfdataformat這個類,當使用excel內嵌的(或者說預定義)的格式時,直接用hssfdataformat.getbuiltinformat靜態方法即可。當使用自己定義的格式時,必須先呼叫hssfdataformat format= hssfworkbook.createdataformat()。

hssfsheet sheet = workbook.createsheet("test");// 建立工作表(sheet)

hssfrow row=sheet.createrow(0);

//設定日期格式--使用excel內嵌的格式

hssfcell cell=row.createcell(0);

cell.setcellvalue(new date());

hssfcellstyle style=workbook.createcellstyle();

style.setdataformat(hssfdataformat.getbuiltinformat("m/d/yy h:mm"));

cell.setcellstyle(style);

//設定保留2位小數--使用excel內嵌的格式

cell=row.createcell(1);

cell.setcellvalue(12.3456789);

style=workbook.createcellstyle();

style.setdataformat(hssfdataformat.getbuiltinformat("0.00"));

cell.setcellstyle(style);

//設定貨幣格式--使用自定義的格式

cell=row.createcell(2);

cell.setcellvalue(12345.6789);

style=workbook.createcellstyle();

style.setdataformat(workbook.createdataformat().getformat("¥#,##0"));

cell.setcellstyle(style);

//設定百分比格式--使用自定義的格式

cell=row.createcell(3);

cell.setcellvalue(0.123456789);

style=workbook.createcellstyle();

style.setdataformat(workbook.createdataformat().getformat("0.00%"));

cell.setcellstyle(style);

//設定中文大寫格式--使用自定義的格式

cell=row.createcell(4);

cell.setcellvalue(12345);

style=workbook.createcellstyle();

style.setdataformat(workbook.createdataformat().getformat("[dbnum2][$-804]0"));

cell.setcellstyle(style);

//設定科學計數法格式--使用自定義的格式

cell=row.createcell(5);

cell.setcellvalue(12345);

style=workbook.createcellstyle();

style.setdataformat(workbook.createdataformat().getformat("0.00e+00"));

cell.setcellstyle(style);

6、如何使用公式?

cell.setcelltype(hssfcell.cell_type_formula);

cell.setcellformula("sum(a1,c1)");

7、單元格設定邊框

注意單元格合併後,設定邊框,並不會為其他的(未合併前)單元格設定邊框,所以其他邊框得自己手動補上

curstyle.setbordertop(hssfcellstyle.border_thin); // 實線右邊框

curstyle.setborderright(hssfcellstyle.border_thin); // 實線右邊框

curstyle.setborderbottom(hssfcellstyle.border_thin); // 實線右邊框

curstyle.setborderleft(hssfcellstyle.border_thin); // 實線右邊框

bytearrayoutputstream bos = new bytearrayoutputstream();

wb.write(bos);

downloadutil.download(bos, response, " ***.xls");

public

static

void

download(bytearrayoutputstream bytearrayoutputstream, httpservletresponse response, string returnname) throws ioexception

10、提供網上乙個牛人介紹poi部落格位址,介紹的很詳細:

mac 關於向 bash profile插入內容

可直接在終端輸入 cd 然後需要輸入密碼,則輸入 pwd 得知 而.bash profile為該目錄下的乙個隱藏檔案 可使用文字編輯器vi 直接新增 在終端機上輸入 vi bash profile 插入內容 alias mysql usr local mysql bin mysql alias my...

使用POI操作Excel

apache的jakata專案poi http poi.apache.org 用來操作excel,並能滿足大部分需要.poi下面有幾個子專案,其中hssf xssf專案 http poi.apache.org spreadsheet index.html 用來實現excel讀寫的.public st...

使用POI讀寫Excel

使用poi來讀寫exccel很方便,但是一開始用的時候有點蒙,這裡我將我學習時寫的例子給大家分享一下,希望對你學習有點幫助。順便說一下,jxl也可以操作excel,如果有興趣,可以參考 讀excel 其中讀excel比較簡單,先通過輸入流建立工作檔案,獲取工作簿,通過工作簿獲取行,通過行獲取單元格,...