poi匯出excel工具類

2021-08-13 18:12:19 字數 3271 閱讀 7711

poi版本

org.apache.poi

poi3.17

org.apache.poi

poi-ooxml

3.17

/**

* excel匯出工具類

* @author zsc

* @datetime 2023年12月14日 下午8:01:32

* 建立工作簿

* @param sheetnames

* @param headnames

* @param titles

* @param contents

* @return

* @throws exception

*/public static xssfworkbook createexcel(string sheetnames, string headnames,

listtitles, list> contents) throws exception

return workbook; }

public static xssfworkbook createexcel(string sheetname, string headname,

string titles, listcontents) throws exception

/*** 建立工作簿

* @param workbook 匯出工作簿

* @param sheet 匯出工作表

* @param titles 標題列表

* @param contents 資料列表

* @throws exception

*/public static void createexcel(xssfworkbook workbook, xssfsheet sheet, string titles, listcontents) throws exception

/*** 匯出excel

* @param os 輸出流

* @param workbook 匯出工作簿

* @param sheet 匯出工作表

* @param headname 表頭名

* @param titles 標題列表

* @param contents 資料列表

* @throws exception

*/public static void createexcel(xssfworkbook workbook, xssfsheet sheet, string headname,

string titles, listcontents) throws exception

// 建立單元格式樣式集合

styles.set(stylemap(workbook));

// 建立工作表頭

createsheethead(workbook, sheet, titles, headname);

// 填充工作表資料

createsheetdata(sheet, contents, 2);

} finally

styles.remove();

} }/**

* 構建sheet表頭

* @param sheet

* @param heads

*/private static void createsheethead(xssfworkbook workbook, xssfsheet sheet, string heads, string headname)

}} }

/*** 構建sheet資料內容

* @param sheet

* @param contents

* @param index

*/private static void createsheetdata(xssfsheet sheet, listcontents, int index) else if(obj[i] instanceof integer || (obj[i] instanceof long && obj[i].tostring().length() <=10)

|| (stringutil.isnumeric(obj[i].tostring()) && obj[i].tostring().length() <= 10))else

} else }}

} }/**

* 建立單元格表頭樣式

** @param workbook 工作薄

*/private static xssfcellstyle createcellheadstyle(xssfworkbook workbook)

/*** 建立單元格表頭標題樣式

** @param workbook 工作薄

*/private static xssfcellstyle createcelltitlestyle(xssfworkbook workbook)

/*** 建立單元格正文樣式

** @param workbook 工作薄

*/private static xssfcellstyle createcellcontentstyle(xssfworkbook workbook)

/*** 單元格樣式(integer)列表

*/private static xssfcellstyle createcellcontent4integerstyle(xssfworkbook workbook)

/*** 單元格樣式(double)列表

*/private static xssfcellstyle createcellcontent4doublestyle(xssfworkbook workbook)

/*** 單元格樣式列表

*/private static mapstylemap(xssfworkbook workbook)

}

POI匯出Excel工具類 補充

在實際使用中,發現用xssfworkbook建立xlsx檔案,如果資料量比較大,很容易出現占用cpu過高,記憶體溢位的情況。查了相關資料後,才發現官方推薦處理大量資料使用sxssfworkbook 在poi3.8之後才有 下面貼下自己寫的 public static void sxssfoutput...

使用POI匯出Excel最全工具類

1 建立乙個excel 注意在設定單元格的先關資訊時一定要 cell2.setcellstyle style2 這種形式才能讓設定起作用 hssfworkbook workbook new hssfworkbook 建立工作薄物件,這裡也可以設定sheet的name hssfsheet sheet1...

匯出Excel工具類

public class excelutil hssfsheet sheet wb.createsheet sheetname 第二步,在workbook中新增乙個sheet,對應excel檔案中的sheet hssfrow row sheet.createrow 0 第三步,在sheet中新增表頭...