POI导出Excel工具类(补充)


在实际使用中,发现用XSSFWorkbook创建xlsx文件,如果数据量比较大,很容易出现占用cpu过高,内存溢出的情况。查了相关资料后,才发现官方推荐处理大量数据使用SXSSFWorkbook(在POI3.8之后才有)

下面贴下自己写的代码

</pre><pre name="code" class="java">public static void SXSSFOutputHeadersNew(String[] headersInfo,SXSSFWorkbook workbook,Sheet sheet,int rowIndex){
		// 列名样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 11);// 字体大小
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中
        style.setLocked(true);
        
		Row row = sheet.createRow(rowIndex);
		for(int i = 0;i<headersInfo.length;i++){
			Cell nextCell = row.createCell(i);
			nextCell.setCellStyle(style);
			nextCell.setCellValue(headersInfo[i]);
//			sheet.autoSizeColumn(i, true);//列宽自适应
		}
	}
	

	
	public static void SXSSFOutputColumnsNew(String[] headersInfo,List<Map<String,Object>> list,SXSSFWorkbook workbook,Sheet sheet,int rowIndex){
		// 列名样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 11);// 字体大小
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中
        style.setLocked(true);
        
        for(int i=0;i<list.size();i++){
        	Map<String,Object> map = list.get(i);
        	Row nextRow = sheet.createRow(rowIndex+i);
        	for(int j = 0; j<headersInfo.length;j++){
        		Cell nextCell = nextRow.createCell(j);
        		nextCell.setCellStyle(style);
				Object value = map.get(headersInfo[j]);
				nextCell.setCellValue(value==null?"":value.toString());
        	}
        }
	}
	
	public static void SXSSFOutputHeadersMergeNew(String[] headNum,Sheet sheet){
		//动态合并单元格
        for (int i = 0; i < headNum.length; i++) {
            String[] temp = headNum[i].split(",");
            Integer startrow = Integer.parseInt(temp[0]);
            Integer overrow = Integer.parseInt(temp[1]);
            Integer startcol = Integer.parseInt(temp[2]);
            Integer overcol = Integer.parseInt(temp[3]);
            sheet.addMergedRegion(new CellRangeAddress(startrow, overrow,
                    startcol, overcol));
        }
	}
	
	public static void SXSSFOutputColumnsByDtosNew(String[] headersInfo,Object[] objs,SXSSFWorkbook workbook,Sheet sheet,int rowIndex){
		// 列名样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 11);// 字体大小
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 左右居中
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中
        style.setLocked(true);
        
        for(int i=0;i<objs.length;i++){
        	Object obj = objs[i];
        	Row nextRow = sheet.createRow(rowIndex+i);
        	for(int j = 0; j<headersInfo.length;j++){
        		Cell nextCell = nextRow.createCell(j);
        		nextCell.setCellStyle(style);
				Object value = getFiledValueByName(headersInfo[j],obj);
				nextCell.setCellValue(value==null?"":value.toString());
        	}
        }
	}

使用SXSSFWorkbook,要注意他的声明方式
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);//内存中保留 1000条数据,以免内存溢出,其余写入 硬盘

猜你喜欢

转载自blog.csdn.net/m819177045/article/details/52789423