1.匯入依賴
>
>
org.apache.poigroupid
>
>
poiartifactid
>
>
4.1.2version
>
dependency
>
2.獲取匯出資料
list data = 資料;
3.設定匯出元件
hssfworkbook workbook =
newhssfworkbook()
;hssfsheet sheet = workbook.
createsheet
("sheet名"
);
4.初始化sheet
hssfrow row = sheet.
createrow(0
);//設定列寬,setcolumnwidth的第二個引數要乘以256,這個引數的單位是1/256個字元寬度
sheet.
setcolumnwidth(0
,5*256);
sheet.
setcolumnwidth(1
,15*256);
sheet.
setcolumnwidth(2
,10*256);
//設定為居中加粗
hssfcellstyle style = workbook.
createcellstyle()
;hssffont font = workbook.
createfont()
;font.
setbold
(true);
style.
setalignment
(horizontalalignment.center)
;style.
setfont
(font)
;hssfcell cell = row.
createcell(0
);cell.
setcellvalue
("序號");
cell.
setcellstyle
(style)
;cell = row.
createcell(1
);cell.
setcellvalue
("任務id");
cell.
setcellstyle
(style)
;cell = row.
createcell(2
);cell.
setcellvalue
("標題");
cell.
setcellstyle
(style)
;int rownum =1;
for(object item : data)
5.匯出
string filename =
"表名.xls"
;response.
setcontenttype()
;response.
setheader
("content-disposition"
,"attachment;filename="
+ urlencoder.
encode
(filename,
"utf-8"))
;outputstream outputstream = response.
getoutputstream()
;workbook.
write
(outputstream)
;outputstream.
flush()
;outputstream.
close()
;
使用POI匯出excel檔案
fileoutputstream fileout new fileoutputstream workbook wb new hssfworkbook 建議使用介面來宣告變數 面向介面思想,可以方便使用介面的不同實現 wb.write fileout 寫入檔案 fileout.close sheet ...
POI匯出Excel檔案
workbook workbook exportservice.exportfile request,student outputstream out null try catch ioexception e finally catch exception e 實現類 設定各列寬度 short st...
使用poi匯出excel
使用poi匯出excel,其中涉及到居中 字型加粗 合併單元格 namespace org examinee results inputname inputstream buffersize 4096 public class orgexamineeexportaction extends nati...