1. 程式人生 > >POI匯出Excel工具類(補充)

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條資料,以免記憶體溢位,其餘寫入 硬碟