/**
* 大数据量的情况下使用此方法可减少oom
* @param headList 表头
* @param dataList 数据
* @param sheetName 工作薄名称(第一页)
*/
public static SXSSFWorkbook exportSXSSFWb(
List<String> headList, List<List<String>> dataList, String sheetName) {
SXSSFWorkbook sxssfWorkbook = null;
//这样表示SXSSFWorkbook只会保留1000条数据在内存中,其它的数据都会写到磁盘里,这样的话占用的内存就会很少
sxssfWorkbook = new SXSSFWorkbook(1000);
//获取第一个Sheet页
SXSSFSheet sheet = sxssfWorkbook.createSheet(sheetName);
// 表头样式 字体
CellStyle headStyle = sxssfWorkbook.createCellStyle();
headStyle.setAlignment(HorizontalAlignment.LEFT);
Font headFont = sxssfWorkbook.createFont();
headFont.setFontName("Arial");
headFont.setFontHeightInPoints((short) 11);
headFont.setBold(true);
headStyle.setFont(headFont);
//普通数据样式 字体
CellStyle style = sxssfWorkbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
Font font = sxssfWorkbook.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 11);
style.setFont(font);
// 产生表格标题行
SXSSFRow row = sheet.createRow(0);
int i = 0;
for (String head : headList) {
SXSSFCell cell = row.createCell(i);
cell.setCellStyle(headStyle);
cell.setCellValue(head);
i++;
}
//创建内容
int j = 1;
for (List<String> list : dataList) {
row = sheet.createRow(j);
i = 0;
for (String value : list) {
//将内容按顺序赋给对应的列对象
SXSSFCell cell1 = row.createCell(i);
cell1.setCellValue(value);
cell1.setCellStyle(style);
i++;
}
j++;
}
return sxssfWorkbook;