POI export Excel tool class (supplement)


In actual use, it is found that using XSSFWorkbook to create xlsx files, if the amount of data is relatively large, it is easy to occupy too much CPU and memory overflow. After checking the relevant information, I found that the official recommendation to process a large amount of data is to use SXSSFWorkbook (only available after POI3.8)

Paste the code you wrote below

</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());
        	}
        }
	}

When using SXSSFWorkbook, pay attention to his declaration method
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);//内存中保留 1000条数据,以免内存溢出,其余写入 硬盘

Guess you like

Origin blog.csdn.net/m819177045/article/details/52789423