使用POI匯出Excel示例

2021-06-12 17:42:40 字數 2338 閱讀 3025

使用poi匯出excel,需要的jar包:poi-bin-3.9-20121203.zip

示例**如下:

import org.apache.poi.hssf.usermodel.hssfcell;

import org.apache.poi.hssf.usermodel.hssfcellstyle;

import org.apache.poi.hssf.usermodel.hssffont;

import org.apache.poi.hssf.usermodel.hssfrichtextstring;

import org.apache.poi.hssf.usermodel.hssfrow;

import org.apache.poi.hssf.usermodel.hssfsheet;

import org.apache.poi.hssf.usermodel.hssfworkbook;

import org.apache.poi.hssf.util.hssfcolor;

hssfworkbook workbook=new hssfworkbook();//建立乙個excel檔案

hssfsheet sheet=workbook.createsheet();//建立excel的sheet

// 設定列寬   

sheet.setcolumnwidth(0, 1000);   

sheet.setcolumnwidth(1, 1000);   

sheet.setcolumnwidth(2, 1000);   

sheet.setcolumnwidth(3, 1000);

// sheet樣式   

hssfcellstyle sheetstyle = workbook.createcellstyle();

// 背景色的設定   

sheetstyle.setfillbackgroundcolor(hssfcolor.grey_25_percent.index);   

// 前景色的設定   

sheetstyle.setfillforegroundcolor(hssfcolor.grey_25_percent.index);   

// 填充模式   

sheetstyle.setfillpattern(hssfcellstyle.fine_dots);   

// 設定字型   

hssffont headfont = workbook.createfont();   

headfont.setfontname("黑體");   

headfont.setfontheightinpoints((short) 12);// 字型大小  

// 建立第一行  

hssfrow row0 = sheet.createrow(0);  

// 設定行高  

row0.setheight((short) 900);  

// 建立第一列(第一列)  

hssfcell cell0 = row0.createcell(1);  

cell0.setcellvalue(new hssfrichtextstring("第一列"));  

cell0.setcellstyle(headstyle);  

// 建立第二列(第二列)  

hssfcell cell1 = row0.createcell(2);  

cell1.setcellvalue(new hssfrichtextstring("第二列"));  

cell1.setcellstyle(headstyle); 

// 建立第三列(第三列)  

hssfcell cell2 = row0.createcell(3);  

cell2.setcellvalue(new hssfrichtextstring("第三列"));  

cell2.setcellstyle(headstyle); 

// 建立第四列(第四列)  

hssfcell cell3 = row0.createcell(4);  

cell3.setcellvalue(new hssfrichtextstring("第四列"));  

cell3.setcellstyle(headstyle); 

//建立第二行。。同樣。。

fileoutputstream resultsfile = new fileoutputstream(「f:\test」);

workbook.write(resultsfile);

resultsfile.flush();

resultsfile.close();

使用poi匯出excel

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

使用POI匯出excel檔案

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

使用poi匯出excel檔案

1.匯入依賴 org.apache.poigroupid poiartifactid 4.1.2version dependency 2.獲取匯出資料 list data 資料 3.設定匯出元件 hssfworkbook workbook newhssfworkbook hssfsheet shee...