使用POI匯出Excel自適應列寬的實現

2021-09-02 21:55:57 字數 1230 閱讀 3166

使用poi匯出excel時,涉及到如何自適應列寬的問題。方法如下

// 4:**內容

for (int i = 0; i < tablebody.size(); i++) else

int length = createcell.getstringcellvalue().getbytes().length * 256 + 200;

//這裡把寬度最大限制到15000

if (length>15000)

maxwidth.put(j,math.max(length,maxwidth.get(j)));

j++;

createcell.setcellstyle(style1);

}}

// 列寬自適應

for (int i = 0; i < tablehead.size(); i++)

// 5:設定頭

response.setheader("content-disposition",

"attachment; filename=" + new string(headname.getbytes("gb2312"), "iso8859-1") + ".xls");

// 6:設定頭型別

// 7:寫出

outputstream toclient = response.getoutputstream();

workbook.write(toclient);

toclient.flush();

toclient.close();

}注意:以上**有15000的限制,原因是excel限制最大寬度為

15000。所以,在這裡我們除了這個寬度處理外,還需要設定單元格自動換行

style.setwraptext(true);

使用poi匯出excel

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

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