使用poi匯出excel

2021-09-01 21:33:12 字數 1992 閱讀 4235

使用poi匯出excel,其中涉及到居中、字型加粗、合併單元格

@namespace("/org/examinee")

@results( \"", "inputname",

"inputstream", "buffersize", "4096" })

})public class orgexamineeexportaction extends nationorgexamineebaseaction

/*** 匯出excel

* @return

* @throws exception

*/public string export() throws exception;

hssfworkbook workbook = new hssfworkbook();

hssfsheet sheet = workbook.createsheet("sheet1");

//學校名稱

hssfcellstyle cellstyle = workbook.createcellstyle(); //字型樣式

cellstyle.setalignment(cellstyle.align_center);//水平居中

hssffont font = workbook.createfont();

font.setboldweight(hssffont.boldweight_bold);//加粗

font.setfontheightinpoints((short)15); //字型大小

cellstyle.setfont(font);

//標題

hssfcellstyle cellstyle1 = workbook.createcellstyle(); //字型樣式

cellstyle1.setalignment(cellstyle.align_center);//水平居中

hssffont font1 = workbook.createfont();

font1.setboldweight(hssffont.boldweight_bold);//加粗

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

cellstyle1.setfont(font1);

sheet.addmergedregion(new cellrangeaddress(0,0,0,3));

hssfrow row = sheet.createrow(0);

hssfcell cell = row.createcell(0);

cell.setcellstyle(cellstyle);

cell.setcellvalue(org.getschoolname());//設定第一行的標題名

hssfrow row1 = sheet.createrow(1);

for(int j=0 ; jmap = null ;

for(int i = 0 ;i}}}

try

excelname = rootpath+basepath+"export"+".xls";

workbook.write(new fileoutputstream(excelname));

} catch (filenotfoundexception e) catch (ioexception e)

return "export";

}/**

* */

public inputstream getinputstream() throws exception catch (exception e1)

return fis;

}

public string getdownloadfilename()

public string getplanuuid()

public void setplanuuid(string planuuid)

}

使用POI匯出Excel示例

使用poi匯出excel,需要的jar包 poi bin 3.9 20121203.zip 示例 如下 import org.apache.poi.hssf.usermodel.hssfcell import org.apache.poi.hssf.usermodel.hssfcellstyle i...

使用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...