-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcb86ed
commit 791f0ca
Showing
20 changed files
with
354 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.alibaba.excel.analysis.v03; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; | ||
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; | ||
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; | ||
import org.apache.poi.hssf.eventusermodel.HSSFListener; | ||
import org.apache.poi.hssf.eventusermodel.HSSFRequest; | ||
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener; | ||
import org.apache.poi.hssf.record.BoundSheetRecord; | ||
import org.apache.poi.hssf.record.Record; | ||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | ||
|
||
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler; | ||
import com.alibaba.excel.context.AnalysisContext; | ||
import com.alibaba.excel.exception.ExcelAnalysisException; | ||
import com.alibaba.excel.read.metadata.ReadSheet; | ||
|
||
/** | ||
* In some cases, you need to know the number of sheets in advance and only read the file once in advance. | ||
* | ||
* @author Jiaju Zhuang | ||
*/ | ||
public class XlsListSheetListener implements HSSFListener { | ||
private POIFSFileSystem poifsFileSystem; | ||
private List<ReadSheet> sheetList; | ||
private XlsRecordHandler bofRecordHandler; | ||
|
||
public XlsListSheetListener(AnalysisContext analysisContext, POIFSFileSystem poifsFileSystem) { | ||
this.poifsFileSystem = poifsFileSystem; | ||
sheetList = new ArrayList<ReadSheet>(); | ||
bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false); | ||
bofRecordHandler.init(); | ||
} | ||
|
||
@Override | ||
public void processRecord(Record record) { | ||
bofRecordHandler.processRecord(record); | ||
} | ||
|
||
public List<ReadSheet> getSheetList() { | ||
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); | ||
HSSFListener formatListener = new FormatTrackingHSSFListener(listener); | ||
HSSFEventFactory factory = new HSSFEventFactory(); | ||
HSSFRequest request = new HSSFRequest(); | ||
EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener = | ||
new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener); | ||
request.addListenerForAllRecords(workbookBuildingListener); | ||
|
||
try { | ||
factory.processWorkbookEvents(request, poifsFileSystem); | ||
} catch (IOException e) { | ||
throw new ExcelAnalysisException(e); | ||
} | ||
return sheetList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.