`

解决大批量数据导出Excel产生内存溢出的方案(二)

    博客分类:
  • java
阅读更多

客户要求一次导出20w行数据,这就要求导出的文件必须带sheet,上面的代码也不支持sheet,下面的就可以了:

Java代码 复制代码
  1. @SuppressWarnings("unchecked")   
  2. public class XlsMergeUtil {   
  3.   private static Logger logger = LoggerFactory.getLogger(XlsMergeUtil.class);   
  4.   
  5.   /**  
  6.    * 将多个Xls文件合并为一个,适用于只有一个sheet,并且格式相同的文档  
  7.    * @param inputs 输入的Xls文件,第一个XLS文件必须给出足够sheet空间  
  8.    * 例如,总共200000行数据,第一个文件至少3个空白sheet  
  9.    * @param out 输出文件  
  10.    */  
  11.   public static void merge(InputStream[] inputs, OutputStream out) {   
  12.     if (inputs == null || inputs.length <= 1) {   
  13.       throw new IllegalArgumentException("没有传入输入流数组,或只有一个输入流.");   
  14.     }   
  15.   
  16.     List<Record> rootRecords = getRecords(inputs[0]);   
  17.     Workbook workbook = Workbook.createWorkbook(rootRecords);   
  18.     List<Sheet> sheets = getSheets(workbook, rootRecords);   
  19.     if(sheets == null || sheets.size() == 0) {   
  20.       throw new IllegalArgumentException("第一篇文档的格式错误,必须有至少一个sheet");   
  21.     }   
  22.     //以第一篇文档的第一个sheet为根,以后的数据都追加在这个sheet后面   
  23.     Sheet rootSheet = sheets.get(0);    
  24.     int rootRows = getRowsOfSheet(rootSheet); //记录第一篇文档的行数,以后的行数在此基础上增加   
  25.     rootSheet.setLoc(rootSheet.getDimsLoc());   
  26.     Map<Integer, Integer> map = new HashMap(10000);   
  27.     int sheetIndex = 0;   
  28.        
  29.     for (int i = 1; i < inputs.length; i++) { //从第二篇开始遍历   
  30.       List<Record> records = getRecords(inputs[i]);   
  31.       //达到最大行数限制,换一个sheet   
  32.       if(getRows(records) + rootRows >= RowRecord.MAX_ROW_NUMBER) {   
  33.         if((++sheetIndex) > (sheets.size() - 1)) {   
  34.           logger.warn("第一个文档给出的sheets小于需要的数量,部分数据未能合并.");   
  35.           break;   
  36.         }   
  37.         rootSheet = sheets.get(sheetIndex);   
  38.         rootRows = getRowsOfSheet(rootSheet);   
  39.         rootSheet.setLoc(rootSheet.getDimsLoc());   
  40.         logger.debug("切换Sheet{}", sheetIndex);   
  41.       }   
  42.       int rowsOfCurXls = 0;   
  43.       //遍历当前文档的每一个record   
  44.       for (Iterator itr = records.iterator(); itr.hasNext();) {   
  45.         Record record = (Record) itr.next();   
  46.         if (record.getSid() == RowRecord.sid) { //如果是RowRecord   
  47.           RowRecord rowRecord = (RowRecord) record;   
  48.           //调整行号   
  49.           rowRecord.setRowNumber(rootRows + rowRecord.getRowNumber());   
  50.           rootSheet.addRow(rowRecord); //追加Row   
  51.           rowsOfCurXls++; //记录当前文档的行数   
  52.         }   
  53.         //SST记录,SST保存xls文件中唯一的String,各个String都是对应着SST记录的索引   
  54.         else if (record.getSid() == SSTRecord.sid) {   
  55.           SSTRecord sstRecord = (SSTRecord) record;   
  56.           for (int j = 0; j < sstRecord.getNumUniqueStrings(); j++) {   
  57.             int index = workbook.addSSTString(sstRecord.getString(j));   
  58.             //记录原来的索引和现在的索引的对应关系   
  59.             map.put(Integer.valueOf(j), Integer.valueOf(index));   
  60.           }   
  61.         } else if (record.getSid() == LabelSSTRecord.sid) {   
  62.           LabelSSTRecord label = (LabelSSTRecord) record;   
  63.           //调整SST索引的对应关系   
  64.           label.setSSTIndex(map.get(Integer.valueOf(label.getSSTIndex())));   
  65.         }   
  66.         //追加ValueCell   
  67.         if (record instanceof CellValueRecordInterface) {   
  68.           CellValueRecordInterface cell = (CellValueRecordInterface) record;   
  69.           int cellRow = cell.getRow() + rootRows;   
  70.           cell.setRow(cellRow);   
  71.           rootSheet.addValueRecord(cellRow, cell);   
  72.         }   
  73.       }   
  74.       rootRows += rowsOfCurXls;   
  75.     }   
  76.        
  77.     byte[] data = getBytes(workbook, sheets.toArray(new Sheet[0]));   
  78.     write(out, data);   
  79.   }   
  80.   
  81.   static void write(OutputStream out, byte[] data) {   
  82.     POIFSFileSystem fs = new POIFSFileSystem();   
  83.     // Write out the Workbook stream   
  84.     try {   
  85.       fs.createDocument(new ByteArrayInputStream(data), "Workbook");   
  86.       fs.writeFilesystem(out);   
  87.       out.flush();   
  88.     } catch (IOException e) {   
  89.       e.printStackTrace();   
  90.     } finally {   
  91.       try {   
  92.         out.close();   
  93.       } catch (IOException e) {   
  94.         e.printStackTrace();   
  95.       }   
  96.     }   
  97.   }   
  98.   
  99.   static List<Sheet> getSheets(Workbook workbook, List records) {   
  100.     int recOffset = workbook.getNumRecords();   
  101.     int sheetNum = 0;   
  102.   
  103.     // convert all LabelRecord records to LabelSSTRecord   
  104.     convertLabelRecords(records, recOffset, workbook);   
  105.     List<Sheet> sheets = new ArrayList();   
  106.     while (recOffset < records.size()) {   
  107.       Sheet sh = Sheet.createSheet(records, sheetNum++, recOffset);   
  108.   
  109.       recOffset = sh.getEofLoc() + 1;   
  110.       if (recOffset == 1) {   
  111.         break;   
  112.       }   
  113.       sheets.add(sh);   
  114.     }   
  115.     return sheets;   
  116.   }   
  117.   
  118.   static int getRows(List<Record> records) {   
  119.     int row = 0;   
  120.     for (Iterator itr = records.iterator(); itr.hasNext();) {   
  121.       Record record = (Record) itr.next();   
  122.       if (record.getSid() == RowRecord.sid) {   
  123.         row++;   
  124.       }   
  125.     }   
  126.     return row;   
  127.   }   
  128.      
  129.   static int getRowsOfSheet(Sheet sheet) {   
  130.     int rows = 0;   
  131.     sheet.setLoc(0);   
  132.     while(sheet.getNextRow() != null) {   
  133.       rows++;   
  134.     }   
  135.     return rows;   
  136.   }   
  137.   
  138.   @SuppressWarnings("deprecation")   
  139.   static List<Record> getRecords(InputStream input) {   
  140.     try {   
  141.       POIFSFileSystem poifs = new POIFSFileSystem(input);   
  142.       InputStream stream = poifs.getRoot().createDocumentInputStream("Workbook");   
  143.       return org.apache.poi.hssf.record.RecordFactory.createRecords(stream);   
  144.     } catch (IOException e) {   
  145.       logger.error("IO异常:{}", e.getMessage());   
  146.       e.printStackTrace();   
  147.     }   
  148.     return Collections.EMPTY_LIST;   
  149.   }   
  150.   
  151.   static void convertLabelRecords(List records, int offset, Workbook workbook) {   
  152.   
  153.     for (int k = offset; k < records.size(); k++) {   
  154.       Record rec = (Record) records.get(k);   
  155.   
  156.       if (rec.getSid() == LabelRecord.sid) {   
  157.         LabelRecord oldrec = (LabelRecord) rec;   
  158.   
  159.         records.remove(k);   
  160.         LabelSSTRecord newrec = new LabelSSTRecord();   
  161.         int stringid = workbook.addSSTString(new UnicodeString(oldrec.getValue()));   
  162.   
  163.         newrec.setRow(oldrec.getRow());   
  164.         newrec.setColumn(oldrec.getColumn());   
  165.         newrec.setXFIndex(oldrec.getXFIndex());   
  166.         newrec.setSSTIndex(stringid);   
  167.         records.add(k, newrec);   
  168.       }   
  169.     }   
  170.   }   
  171.   
  172.   public static byte[] getBytes(Workbook workbook, Sheet[] sheets) {   
  173.     // HSSFSheet[] sheets = getSheets();   
  174.     int nSheets = sheets.length;   
  175.   
  176.     // before getting the workbook size we must tell the sheets that   
  177.     // serialization is about to occur.   
  178.     for (int i = 0; i < nSheets; i++) {   
  179.       sheets[i].preSerialize();   
  180.     }   
  181.   
  182.     int totalsize = workbook.getSize();   
  183.     // pre-calculate all the sheet sizes and set BOF indexes   
  184.     int[] estimatedSheetSizes = new int[nSheets];   
  185.     for (int k = 0; k < nSheets; k++) {   
  186.       workbook.setSheetBof(k, totalsize);   
  187.       int sheetSize = sheets[k].getSize();   
  188.       estimatedSheetSizes[k] = sheetSize;   
  189.       totalsize += sheetSize;   
  190.     }   
  191.     logger.debug("分配内存{}bytes", totalsize);   
  192.     byte[] retval = new byte[totalsize];   
  193.     int pos = workbook.serialize(0, retval);   
  194.   
  195.     for (int k = 0; k < nSheets; k++) {   
  196.       int serializedSize = sheets[k].serialize(pos, retval);   
  197.       if (serializedSize != estimatedSheetSizes[k]) {   
  198.         // Wrong offset values have been passed in the call to setSheetBof() above.   
  199.         // For books with more than one sheet, this discrepancy would cause excel   
  200.         // to report errors and loose data while reading the workbook   
  201.         throw new IllegalStateException("Actual serialized sheet size (" + serializedSize   
  202.             + ") differs from pre-calculated size (" + estimatedSheetSizes[k] + ") for sheet (" + k   
  203.             + ")");   
  204.         // TODO - add similar sanity check to ensure that Sheet.serializeIndexRecord() does not   
  205.         // write mis-aligned offsets either   
  206.       }   
  207.       pos += serializedSize;   
  208.     }   
  209.     return retval;   
  210.   }   
  211.   
  212.   public static void main(String[] args) throws Exception {   
  213.     final String PATH = "E:\\projects\\java\\ws_0\\export\\data\\";   
  214.     InputStream[] inputs = new InputStream[25];   
  215.     inputs[0] = new java.io.FileInputStream(PATH + "07_10.xls");   
  216.     for(int i = 1; i < 25 ; i++) {   
  217.       inputs[i] = new java.io.FileInputStream(PATH + "07_01.xls");   
  218.     }   
  219.     OutputStream out = new FileOutputStream(PATH + "xx.xls");   
  220.     long t1 = System.currentTimeMillis();   
  221.     merge(inputs, out);   
  222.     System.out.println(System.currentTimeMillis() - t1);   
  223.   }   
  224.   
  225. }  
分享到:
评论
1 楼 lenves 2011-10-24  
哥们你这用的是3.0的包吧,我这3W多条数据都内存溢出了

相关推荐

Global site tag (gtag.js) - Google Analytics