poi導出excel解決內存溢出


解決方法

寫excel時使用 SXSSFWorkbook workbook = new SXSSFWorkbook(xssfWorkbook , 1000);只在內存中留1000行,不會占用過多的內存。下面只貼了部分代碼。

public static void createExcelByTrade(List<String[]> mergeCellConfigList ,  Map<String, String[]> cellConfMap , List dataList ,String tempPath , String fileName  ) {
        try {
          int  excelRowNum  = 0;      
          short fontSize = 12;         
          // 創建新的Excel 工作簿
          XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
          SXSSFWorkbook workbook = new SXSSFWorkbook(xssfWorkbook , 1000);
                          
          Sheet sheet = workbook.createSheet(fileName);         
          // 設置合並表頭         
          setMergeHeaderCellByTrade(   workbook,   sheet,    mergeCellConfigList ,  excelRowNum,   fontSize );
          excelRowNum ++ ;
          excelRowNum ++ ;
         
          //設置表頭
          setHeaderCell(  workbook,   sheet,  cellConfMap ,  excelRowNum,   fontSize );
          excelRowNum ++ ;     //查詢數據庫中所有的數據  
          setCellData(   workbook,   sheet, cellConfMap,   dataList,   excelRowNum,   fontSize);                        
          // 新建一輸出文件流
          FileOutputStream fOut = new FileOutputStream(tempPath);
          // 把相應的Excel 工作簿存盤
          workbook.write(fOut);
          //清空緩沖區數據
          fOut.flush();
          // 操作結束,關閉文件
          fOut.close();
          System.out.println("文件生成...");
        } catch (Exception e) {
            e.printStackTrace();
          System.out.println("已運行 xlCreate() : " + e);
        }
      }

導出excel時進行合並單元格及樣式設置。package com.sfit.fiss.otcdownload;

import java.io.FileOutputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.RegionUtil; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFDataFormat; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.bstek.dorado.common.UserConfig; import com.sfit.fiss.util.ExcelUtil; import com.sfit.framework.sp.SpCondition; public class ExcelExportUtil { public static void main(String[] args) throws SQLException {  String tempFilePath = "D:/Eclipse/WorkSpaceGanyMede/otcreport/temp/report/"; List<String[]> dataList = new  ArrayList<String[]>();
//導出數據 exportPositionInfoData(dataList , tempFilePath) ;
} /** * 導出數據 * @param dataList */ static String exportPositionInfoData( List dataList, String tempFilePath) { List<String[]> mergeCellConfigList = new ArrayList<String[]>(); mergeCellConfigList.add(new String[] {"主體信息" , "2" , "HeaderStyle_2" }); mergeCellConfigList.add(new String[] {"對手方信息" , "2" , "HeaderStyle_2" }); mergeCellConfigList.add(new String[] {"物信息" , "11" , "HeaderStyle_2" }); mergeCellConfigList.add(new String[] {"倉信息" , "8" , "HeaderStyle_2" }); //設置表頭 Map<String, String[]> cellConfMap = new LinkedHashMap<String, String[]>(); cellConfMap.put("MAINBODYNAME", new String[] { "ASSS", "HeaderStyle_2", "DataStyle_TEXT_LC", "30" }); cellConfMap.put("NOCID", new String[] { "ASD", "HeaderStyle_2", "DataStyle_TEXT_LC", "30" }); cellConfMap.put("ANALOGUENAME", new String[] { "ASDASD", "HeaderStyle_2", "DataStyle_TEXT_LC", "30" }); cellConfMap.put("ANALOGUENOCID", new String[] { "ASDASDSD", "HeaderStyle_2", "DataStyle_TEXT_LC", "35" }); cellConfMap.put("TRANSCONFIRNUMBER", new String[] { "ASDASDAS", "HeaderStyle_2", "DataStyle_TEXT_LC", "20" }); cellConfMap.put("TRANSCONFIRTIME", new String[] { "DFGDFG", "HeaderStyle_2", "DataStyle_TEXT_LC", "15" }); SpCondition sc = new SpCondition(); String fileName = "數據"; String tempPath = tempFilePath +"OTCPositionData.xlsx"; createExcel( mergeCellConfigList , cellConfMap , dataList , tempPath, fileName); return tempPath; } @SuppressWarnings("deprecation") public static void createExcel(List<String[]> mergeCellConfigList , Map<String, String[]> cellConfMap , List dataList ,String tempPath , String fileName ) { try { int excelRowNum = 0; short fontSize = 12; // 創建新的Excel 工作簿 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); SXSSFWorkbook workbook = new SXSSFWorkbook(xssfWorkbook, 1000); Sheet sheet = workbook.createSheet(fileName); // 設置合並表頭 setMergeHeaderCell( workbook, sheet, mergeCellConfigList , excelRowNum, fontSize ); excelRowNum ++ ; //設置表頭 setHeaderCell( workbook, sheet, cellConfMap , excelRowNum, fontSize ); excelRowNum ++ ; //設置cell自動寬度 //setAutoSizeColumn(sheet, cellConfMap.size()); //查詢數據庫中所有的數據 setCellData( workbook, sheet, cellConfMap, dataList, excelRowNum, fontSize); // 新建一輸出文件流 FileOutputStream fOut = new FileOutputStream(tempPath); // 把相應的Excel 工作簿存盤 workbook.write(fOut); //清空緩沖區數據 fOut.flush(); // 操作結束,關閉文件 fOut.close(); System.out.println("文件生成..."); } catch (Exception e) { e.printStackTrace(); System.out.println("已運行 xlCreate() : " + e); } } //向單元格 填充數據 private static void setCellData( SXSSFWorkbook workbook, Sheet sheet, Map<String, String[]> cellConfMap, List list, int excelRowNum, short fontSize) throws SQLException, Exception { Row row = null ; Cell cell = null ; //生成 樣式 Map<String , XSSFCellStyle > styleMap = new HashMap<String, XSSFCellStyle>(); if(cellConfMap!=null && !cellConfMap.isEmpty()) { for(String keyStr: cellConfMap.keySet() ) { styleMap.put(keyStr, ExcelExportUtil.createCellStyle(workbook, cellConfMap.get(keyStr)[2], fontSize)); } } if(list != null && !list.isEmpty()) { ResultSet rs = (ResultSet) list.get(0); if(cellConfMap != null && !cellConfMap.isEmpty()) { while(rs.next()){ row = sheet.createRow(excelRowNum); int cellNum = 0; for (String keyStr: cellConfMap.keySet()) { cell = row.createCell(cellNum); cell.setCellStyle(styleMap.get(keyStr)); cell.setCellValue(rs.getString(keyStr)); cellNum++; } excelRowNum ++ ; } } } } /** * 設置普通表頭信息 * @param workbook * @param sheet * @param cellConfMap * @param excelRowNum * @param fontSize * @throws Exception */ private static void setHeaderCell( SXSSFWorkbook workbook, Sheet sheet, Map<String, String[]> cellConfMap ,int excelRowNum, short fontSize ) throws Exception { Row row; Cell cell; XSSFCellStyle style; row = sheet.createRow(excelRowNum); if(cellConfMap != null && !cellConfMap.isEmpty() ) { int cellNum = 0; for(String keyStr: cellConfMap.keySet()) { cell = row.createCell(cellNum); cell.setCellValue(cellConfMap.get(keyStr)[0]); // 表頭列名 // 設置類表寬度 sheet.setColumnWidth(cellNum, 256 * Integer.parseInt((cellConfMap.get(keyStr)[3] != null && !"".equals(cellConfMap.get(keyStr)[3])) ? cellConfMap.get(keyStr)[3] : "10")); // 更具樣式類型設置表頭樣式HeaderStyle String headerStyleTypeStr = cellConfMap.get(keyStr)[1]; if (headerStyleTypeStr == null && "".equals(headerStyleTypeStr)) { headerStyleTypeStr = "HeaderStyle"; } style = createCellStyle(workbook, headerStyleTypeStr, fontSize); cell.setCellStyle(style); cellNum++; } } } /** * 生成成交的合並表頭信息 * @param workbook * @param sheet * @param mergeCellConfigList * @param excelRowNum * @param fontSize * @throws Exception */ private static void setMergeHeaderCellByTrade( SXSSFWorkbook workbook, Sheet sheet, List<String[]> mergeCellConfigList ,int excelRowNum, short fontSize ) throws Exception { Row row = null ; int startCellIndex = 0; int endCellIndex = 0; Cell cell = null; XSSFCellStyle style = null; if (mergeCellConfigList != null && !mergeCellConfigList.isEmpty()) { row = sheet.createRow(excelRowNum); for (String[] mergeParamItem : mergeCellConfigList) { if (endCellIndex != 0) { startCellIndex = (endCellIndex + 1); } endCellIndex = (startCellIndex + Integer.parseInt(mergeParamItem[1]) - 1); if(mergeParamItem[0] != null && "交易信息".equals(mergeParamItem[0])){ sheet.addMergedRegion(new CellRangeAddress(excelRowNum, excelRowNum, startCellIndex, endCellIndex)); }else { sheet.addMergedRegion(new CellRangeAddress(excelRowNum, excelRowNum +1, startCellIndex, endCellIndex)); } endCellIndex = (startCellIndex + Integer.parseInt(mergeParamItem[1]) - 1); cell = row.createCell(startCellIndex ); style = ExcelExportUtil.createCellStyle(workbook, "HeaderStyle", fontSize); cell.setCellStyle(style); cell.setCellValue(mergeParamItem[0]); } } ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 0, 1)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 2, 3)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 4, 5)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 6, 7)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 8, 9)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 10, 10)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 11, 34)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 35, 36)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 37, 41)); excelRowNum=excelRowNum+1; row = sheet.createRow(excelRowNum); cell = row.createCell(11 ); style = ExcelExportUtil.createCellStyle(workbook, "HeaderStyle_2", fontSize); cell.setCellStyle(style); cell.setCellValue("交易編碼"); cell = row.createCell(12); style = ExcelExportUtil.createCellStyle(workbook, "HeaderStyle_2", fontSize); cell.setCellStyle(style); cell.setCellValue("時間信息"); cell = row.createCell(17); style = ExcelExportUtil.createCellStyle(workbook, "HeaderStyle_2", fontSize); cell.setCellStyle(style); cell.setCellValue("產品信息及標的物信息"); cell = row.createCell(29); style = ExcelExportUtil.createCellStyle(workbook, "HeaderStyle_2", fontSize); cell.setCellStyle(style); cell.setCellValue("價格及價值信息"); sheet.addMergedRegion(new CellRangeAddress(excelRowNum, excelRowNum, 12, 16)); sheet.addMergedRegion(new CellRangeAddress(excelRowNum, excelRowNum, 17, 28)); sheet.addMergedRegion(new CellRangeAddress(excelRowNum, excelRowNum, 29, 34)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 0, 1)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 2, 3)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 4, 5)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 6, 7)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 8, 9)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 10, 10)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 11, 11)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 12, 16)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 17, 28)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 29, 34)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 35, 36)); ExcelExportUtil.setMergedCellStyle(workbook, sheet, new CellRangeAddress(excelRowNum, excelRowNum, 37, 41)); } /** * 設置表頭信息 * @param workbook * @param sheet * @param mergeCellConfigList * @param excelRowNum * @param fontSize * @throws Exception */ private static void setMergeHeaderCell( SXSSFWorkbook workbook, Sheet sheet, List<String[]> mergeCellConfigList ,int excelRowNum, short fontSize ) throws Exception { Row row = null ; int startCellIndex = 0; int endCellIndex = 0; Cell cell = null; if (mergeCellConfigList != null && !mergeCellConfigList.isEmpty()) { row = sheet.createRow(excelRowNum); for (String[] mergeParamItem : mergeCellConfigList) { if (endCellIndex != 0) { startCellIndex = (endCellIndex + 1); } endCellIndex = (startCellIndex + Integer.parseInt(mergeParamItem[1]) - 1); sheet.addMergedRegion(new CellRangeAddress(excelRowNum, excelRowNum, startCellIndex, endCellIndex)); cell = row.createCell(startCellIndex); cell.setCellStyle(ExcelExportUtil.createCellStyle(workbook, mergeParamItem[2], fontSize)); cell.setCellValue(mergeParamItem[0]); } } } private static void setAutoSizeColumn(Sheet sheet , int cellNum) { //設置列寬 if(sheet!= null ) { for(int index = 0 ; index<=cellNum ; index++ ) { sheet.autoSizeColumn(index, true); //sheet.setColumnWidth(index,sheet.getColumnWidth(index)); } } } /* 更具單元格樣式類型 生成相應的單元格樣式 */ public static XSSFCellStyle createCellStyle(SXSSFWorkbook workbook, String styleType, short fontSize) throws Exception { // 設置數據類型 XSSFDataFormat dataFormat = (XSSFDataFormat) workbook.createDataFormat(); XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle(); if (styleType != null && "HeaderStyle".equals(styleType)) { // 表頭數據格式 HeaderStyle /* * // 背景色 style.setFillForegroundColor((short) 11); * style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); * style.setFillBackgroundColor((short) 11); */ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ /*style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex());*/ } else if (styleType != null && "HeaderStyle_1".equals(styleType)) { // 表頭數據格式 HeaderStyle1 // 背景色 /* * style.setFillForegroundColor((short) 8); * style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); * style.setFillBackgroundColor((short) 8); */ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗體顯示 font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "HeaderStyle_2".equals(styleType)) { // 表頭數據格式 HeaderStyle2 // 背景色 /* style.setFillForegroundColor((short) 9); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 9);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗體顯示 font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "HeaderStyle_3".equals(styleType)) { // 表頭數據格式 HeaderStyle3 // 背景色 /* * style.setFillForegroundColor((short) 10); * style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); * style.setFillBackgroundColor((short) 10); */ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗體顯示 font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "DataStyle_TEXT".equals(styleType)) { // 數據單元格樣式 文本 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "DataStyle_TEXT_LC".equals(styleType)) { // 數據單元格樣式 文本 水平靠左 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_LEFT);// 水平靠左 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "DataStyle_TEXT_CC".equals(styleType)) { // 數據單元格樣式 文本 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "DataStyle_TEXT_RC".equals(styleType)) { // 數據單元格樣式 文本 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);// 水平靠右 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "DataStyle_TEXT_LC_LINE".equals(styleType)) { // 數據單元格樣式 文本 水平靠左 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_LEFT);// 水平靠左 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(false);// 設置不自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); } else if (styleType != null && "DataStyle_NUMBER".equals(styleType)) { // 數據單元格樣式 數字格式 整數 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("#,#0")); } else if (styleType != null && "DataStyle_NUMBER_F2".equals(styleType)) { // 數據單元格樣式 數字格式 兩位小數 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("#,#0.00")); } else if (styleType != null && "DataStyle_NUMBER_F3".equals(styleType)) { // 數據單元格樣式 數字格式 三位小數 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("#,#0.000")); } else if (styleType != null && "DataStyle_NUMBER_F4".equals(styleType)) { // // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_RIGHT);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("#,#0.0000")); } else if (styleType != null && "DataStyle_DATE".equals(styleType)) { // 數據單元格樣式 日期格式 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("yyyy-MM-dd")); } else if (styleType != null && "DataStyle_TIME".equals(styleType)) { // 數據單元格樣式 時間格式 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("hh:mm:ss")); } else if (styleType != null && "DataStyle_DATETIME".equals(styleType)) { // 數據單元格樣式 日期時間格式 // 背景色 /*style.setFillForegroundColor((short) 11); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setFillBackgroundColor((short) 11);*/ // 居中顯示 style.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 水平居中 style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 垂直居中 // 設置字體 XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName("宋體"); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints(fontSize); style.setFont(font); // 設置自動換行 style.setWrapText(true);// 設置自動換行 /* 設置邊框 */ style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setDataFormat(dataFormat.getFormat("yyyy-MM-dd hh:mm:ss")); } else { /* 默認樣式 */ } return style; } public static Sheet setMergedCellStyle(SXSSFWorkbook wb , Sheet sheet ,CellRangeAddress cra ) { RegionUtil.setBorderBottom(XSSFCellStyle.BORDER_THIN, cra, sheet , wb); // 下邊框 RegionUtil.setBorderLeft(XSSFCellStyle.BORDER_THIN, cra, sheet,wb); // 左邊框 RegionUtil.setBorderRight(XSSFCellStyle.BORDER_THIN, cra, sheet,wb); // 有邊框 RegionUtil.setBorderTop(XSSFCellStyle.BORDER_THIN, cra, sheet,wb); // 上邊框 return sheet; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM