|
|
|
@ -3,12 +3,10 @@ package com.alibaba.excel.analysis.v03;
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.util.StringUtils; |
|
|
|
|
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; |
|
|
|
|
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; |
|
|
|
|
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; |
|
|
|
@ -21,11 +19,6 @@ import org.apache.poi.hssf.record.BoundSheetRecord;
|
|
|
|
|
import org.apache.poi.hssf.record.Record; |
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
|
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
|
|
|
|
import org.apache.poi.ss.usermodel.Comment; |
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet; |
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
|
import org.apache.poi.ss.usermodel.WorkbookFactory; |
|
|
|
|
import org.apache.poi.ss.util.CellAddress; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
@ -40,7 +33,8 @@ import com.alibaba.excel.analysis.v03.handlers.NoteRecordHandler;
|
|
|
|
|
import com.alibaba.excel.analysis.v03.handlers.NumberRecordHandler; |
|
|
|
|
import com.alibaba.excel.analysis.v03.handlers.RkRecordHandler; |
|
|
|
|
import com.alibaba.excel.analysis.v03.handlers.SstRecordHandler; |
|
|
|
|
import com.alibaba.excel.context.AnalysisContext; |
|
|
|
|
import com.alibaba.excel.analysis.v03.handlers.TextObjectRecordHandler; |
|
|
|
|
import com.alibaba.excel.context.XlsReadContext; |
|
|
|
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
|
|
|
|
import com.alibaba.excel.exception.ExcelAnalysisException; |
|
|
|
|
import com.alibaba.excel.metadata.CellData; |
|
|
|
@ -72,6 +66,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
|
|
|
|
|
private List<ReadSheet> readSheetList; |
|
|
|
|
private int lastRowNumber; |
|
|
|
|
private int lastColumnNumber; |
|
|
|
|
private int ii = 0; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* For parsing Formulas |
|
|
|
|
*/ |
|
|
|
@ -81,19 +77,12 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
|
|
|
|
|
private List<ReadSheet> sheets; |
|
|
|
|
private HSSFWorkbook stubWorkbook; |
|
|
|
|
private List<XlsRecordHandler> recordHandlers = new ArrayList<XlsRecordHandler>(); |
|
|
|
|
private AnalysisContext analysisContext; |
|
|
|
|
private Workbook poiWorkbook; |
|
|
|
|
private Map<Integer, String> rowComments; |
|
|
|
|
private XlsReadContext analysisContext; |
|
|
|
|
|
|
|
|
|
public XlsSaxAnalyser(AnalysisContext context, POIFSFileSystem poifsFileSystem) { |
|
|
|
|
public XlsSaxAnalyser(XlsReadContext context, POIFSFileSystem poifsFileSystem) { |
|
|
|
|
this.analysisContext = context; |
|
|
|
|
this.records = new LinkedHashMap<Integer, CellData>(); |
|
|
|
|
this.poifsFileSystem = poifsFileSystem; |
|
|
|
|
try { |
|
|
|
|
this.poiWorkbook = WorkbookFactory.create(poifsFileSystem); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
analysisContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -154,7 +143,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
|
|
|
|
|
thisRow = handler.getRow(); |
|
|
|
|
thisColumn = handler.getColumn(); |
|
|
|
|
cellData = handler.getCellData(); |
|
|
|
|
handleComments(thisRow, thisColumn); |
|
|
|
|
if (cellData != null) { |
|
|
|
|
cellData.checkEmpty(); |
|
|
|
|
if (CellDataTypeEnum.EMPTY != cellData.getType()) { |
|
|
|
@ -186,26 +174,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
|
|
|
|
|
processLastCellOfRow(record); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void handleComments(int row, int col) { |
|
|
|
|
if (null == this.poiWorkbook || null == analysisContext.readSheetHolder() || row < 0 || col < 0) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Sheet currentSheet = poiWorkbook.getSheetAt(analysisContext.readSheetHolder().getSheetNo()); |
|
|
|
|
Map<CellAddress, ? extends Comment> cellComments = currentSheet.getCellComments(); |
|
|
|
|
if (CollectionUtils.isEmpty(cellComments)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Comment comment = cellComments.get(new CellAddress(row, col)); |
|
|
|
|
if (null == comment) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
String commentsStr = comment.getString().toString(); |
|
|
|
|
if (!StringUtils.isEmpty(commentsStr)) { |
|
|
|
|
rowComments = rowComments == null ? new HashMap<Integer, String>(8) : rowComments; |
|
|
|
|
rowComments.put(col, commentsStr); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean ignoreRecord(Record record) { |
|
|
|
|
return analysisContext.readWorkbookHolder().getIgnoreRecord03() && record.getSid() != BoundSheetRecord.sid |
|
|
|
|
&& record.getSid() != BOFRecord.sid; |
|
|
|
@ -224,17 +192,14 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
|
|
|
|
|
} |
|
|
|
|
analysisContext.readRowHolder( |
|
|
|
|
new ReadRowHolder(lastRowNumber, analysisContext.readSheetHolder().getGlobalConfiguration())); |
|
|
|
|
if (!CollectionUtils.isEmpty(rowComments)) { |
|
|
|
|
analysisContext.readRowHolder().setRowComments(rowComments); |
|
|
|
|
} |
|
|
|
|
analysisContext.readSheetHolder().notifyEndOneRow(new EachRowAnalysisFinishEvent(records), analysisContext); |
|
|
|
|
records = new HashMap<Integer, CellData>(); |
|
|
|
|
this.records = new LinkedHashMap<Integer, CellData>(); |
|
|
|
|
lastColumnNumber = -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void buildXlsRecordHandlers() { |
|
|
|
|
if (CollectionUtils.isEmpty(recordHandlers)) { |
|
|
|
|
recordHandlers.add(new BlankOrErrorRecordHandler()); |
|
|
|
|
recordHandlers.add(new BlankOrErrorRecordHandler(analysisContext)); |
|
|
|
|
// The table has been counted and there are no duplicate statistics
|
|
|
|
|
if (sheets == null) { |
|
|
|
|
sheets = new ArrayList<ReadSheet>(); |
|
|
|
@ -242,14 +207,15 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
|
|
|
|
|
} else { |
|
|
|
|
recordHandlers.add(new BofRecordHandler(analysisContext, sheets, true, true)); |
|
|
|
|
} |
|
|
|
|
recordHandlers.add(new FormulaRecordHandler(stubWorkbook, formatListener)); |
|
|
|
|
recordHandlers.add(new LabelRecordHandler()); |
|
|
|
|
recordHandlers.add(new NoteRecordHandler()); |
|
|
|
|
recordHandlers.add(new FormulaRecordHandler(analysisContext, stubWorkbook, formatListener)); |
|
|
|
|
recordHandlers.add(new LabelRecordHandler(analysisContext)); |
|
|
|
|
recordHandlers.add(new NoteRecordHandler(analysisContext)); |
|
|
|
|
recordHandlers.add(new NumberRecordHandler(analysisContext, formatListener)); |
|
|
|
|
recordHandlers.add(new RkRecordHandler()); |
|
|
|
|
recordHandlers.add(new SstRecordHandler()); |
|
|
|
|
recordHandlers.add(new MissingCellDummyRecordHandler()); |
|
|
|
|
recordHandlers.add(new RkRecordHandler(analysisContext)); |
|
|
|
|
recordHandlers.add(new SstRecordHandler(analysisContext)); |
|
|
|
|
recordHandlers.add(new MissingCellDummyRecordHandler(analysisContext)); |
|
|
|
|
recordHandlers.add(new IndexRecordHandler(analysisContext)); |
|
|
|
|
recordHandlers.add(new TextObjectRecordHandler(analysisContext)); |
|
|
|
|
Collections.sort(recordHandlers); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|