Apache POI Java Excel Performance for Large Spreadsheets

匿名 (未验证) 提交于 2019-12-03 01:32:01

问题:

I have a spreadsheet I'm trying to read with POI (I have both xls and xlsx formats), but in this case, the problem is with the xls file. My spreadsheet has about 10,000 rows and 75 columns, and reading it in can take several minutes (though Excel opens in a few seconds). I'm using the event based reading, rather than reading the whole file into memory. The meat of my code is below. It's a bit messy right now, but it's really just a long switch statement that was mostly copied from the POI examples.

Is it typical for POI performance using the event model to be so slow? Is there anything I an do to speed this up? I think several minutes will be unacceptable for my application.

    POIFSFileSystem poifs = new POIFSFileSystem(fis);     InputStream din = poifs.createDocumentInputStream("Workbook");     try     {         HSSFRequest req = new HSSFRequest();         listener = new FormatTrackingHSSFListener(new HSSFListener() {             @Override             public void processRecord(Record rec)             {                 thisString = null;                 int sid = rec.getSid();                 switch (sid)                 {                     case SSTRecord.sid:                         strTable = (SSTRecord) rec;                         break;                     case LabelSSTRecord.sid:                         LabelSSTRecord labelSstRec = (LabelSSTRecord) rec;                         thisString = strTable.getString(labelSstRec                                 .getSSTIndex()).getString();                         row = labelSstRec.getRow();                         col = labelSstRec.getColumn();                         break;                     case RKRecord.sid:                         RKRecord rrk = (RKRecord) rec;                         thisString = "";                         row = rrk.getRow();                         col = rrk.getColumn();                         break;                     case LabelRecord.sid:                         LabelRecord lrec = (LabelRecord) rec;                         thisString = lrec.getValue();                         row = lrec.getRow();                         col = lrec.getColumn();                         break;                     case BlankRecord.sid:                         BlankRecord blrec = (BlankRecord) rec;                         thisString = "";                         row = blrec.getRow();                         col = blrec.getColumn();                         break;                     case BoolErrRecord.sid:                         BoolErrRecord berec = (BoolErrRecord) rec;                         row = berec.getRow();                         col = berec.getColumn();                         byte errVal = berec.getErrorValue();                         thisString = errVal == 0 ? Boolean.toString(berec                                 .getBooleanValue()) : ErrorConstants                                 .getText(errVal);                         break;                     case FormulaRecord.sid:                         FormulaRecord frec = (FormulaRecord) rec;                         switch (frec.getCachedResultType())                         {                             case Cell.CELL_TYPE_NUMERIC:                                 double num = frec.getValue();                                 if (Double.isNaN(num))                                 {                                     // Formula result is a string                                     // This is stored in the next record                                     outputNextStringRecord = true;                                 }                                 else                                 {                                     thisString = formatNumericValue(frec, num);                                 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!