|
|
@ -16,8 +16,6 @@ import org.apache.poi.openxml4j.opc.OPCPackage; |
|
|
|
import org.apache.poi.openxml4j.opc.PackageAccess; |
|
|
|
import org.apache.poi.openxml4j.opc.PackageAccess; |
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart; |
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart; |
|
|
|
import org.apache.poi.xssf.eventusermodel.XSSFReader; |
|
|
|
import org.apache.poi.xssf.eventusermodel.XSSFReader; |
|
|
|
import org.apache.poi.xssf.model.CommentsTable; |
|
|
|
|
|
|
|
import org.apache.poi.xssf.model.StylesTable; |
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRelation; |
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRelation; |
|
|
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; |
|
|
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; |
|
|
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; |
|
|
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; |
|
|
@ -27,45 +25,34 @@ import org.xml.sax.InputSource; |
|
|
|
import org.xml.sax.XMLReader; |
|
|
|
import org.xml.sax.XMLReader; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.analysis.ExcelReadExecutor; |
|
|
|
import com.alibaba.excel.analysis.ExcelReadExecutor; |
|
|
|
|
|
|
|
import com.alibaba.excel.analysis.v07.handlers.sax.SharedStringsTableHandler; |
|
|
|
|
|
|
|
import com.alibaba.excel.analysis.v07.handlers.sax.XlsxRowHandler; |
|
|
|
import com.alibaba.excel.cache.ReadCache; |
|
|
|
import com.alibaba.excel.cache.ReadCache; |
|
|
|
import com.alibaba.excel.context.AnalysisContext; |
|
|
|
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
|
|
|
import com.alibaba.excel.exception.ExcelAnalysisException; |
|
|
|
import com.alibaba.excel.exception.ExcelAnalysisException; |
|
|
|
import com.alibaba.excel.read.metadata.ReadSheet; |
|
|
|
import com.alibaba.excel.read.metadata.ReadSheet; |
|
|
|
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; |
|
|
|
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; |
|
|
|
import com.alibaba.excel.util.CollectionUtils; |
|
|
|
import com.alibaba.excel.util.CollectionUtils; |
|
|
|
import com.alibaba.excel.util.FileUtils; |
|
|
|
import com.alibaba.excel.util.FileUtils; |
|
|
|
import com.alibaba.excel.util.SheetUtils; |
|
|
|
import com.alibaba.excel.util.SheetUtils; |
|
|
|
import com.alibaba.excel.util.StringUtils; |
|
|
|
import com.alibaba.excel.util.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* |
|
|
|
|
|
|
|
* @author jipengfei |
|
|
|
* @author jipengfei |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
|
|
|
|
|
|
|
|
private AnalysisContext analysisContext; |
|
|
|
private XlsxReadContext xlsxReadContext; |
|
|
|
private List<ReadSheet> sheetList; |
|
|
|
private List<ReadSheet> sheetList; |
|
|
|
private Map<Integer, InputStream> sheetMap; |
|
|
|
private Map<Integer, InputStream> sheetMap; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception { |
|
|
|
* excel comments |
|
|
|
this.xlsxReadContext = xlsxReadContext; |
|
|
|
* key: sheetNo |
|
|
|
|
|
|
|
* value: CommentsTanle |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private Map<Integer, CommentsTable> commentsTableMap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Current style information |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private StylesTable stylesTable; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public XlsxSaxAnalyser(AnalysisContext analysisContext, InputStream decryptedStream) throws Exception { |
|
|
|
|
|
|
|
this.analysisContext = analysisContext; |
|
|
|
|
|
|
|
// Initialize cache
|
|
|
|
// Initialize cache
|
|
|
|
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); |
|
|
|
XlsxReadWorkbookHolder xlsxReadWorkbookHolder = xlsxReadContext.xlsxReadWorkbookHolder(); |
|
|
|
|
|
|
|
|
|
|
|
OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream); |
|
|
|
OPCPackage pkg = readOpcPackage(xlsxReadWorkbookHolder, decryptedStream); |
|
|
|
readWorkbookHolder.setOpcPackage(pkg); |
|
|
|
xlsxReadWorkbookHolder.setOpcPackage(pkg); |
|
|
|
|
|
|
|
|
|
|
|
ArrayList<PackagePart> packageParts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()); |
|
|
|
ArrayList<PackagePart> packageParts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()); |
|
|
|
|
|
|
|
|
|
|
@ -73,19 +60,18 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
PackagePart sharedStringsTablePackagePart = packageParts.get(0); |
|
|
|
PackagePart sharedStringsTablePackagePart = packageParts.get(0); |
|
|
|
|
|
|
|
|
|
|
|
// Specify default cache
|
|
|
|
// Specify default cache
|
|
|
|
defaultReadCache(readWorkbookHolder, sharedStringsTablePackagePart); |
|
|
|
defaultReadCache(xlsxReadWorkbookHolder, sharedStringsTablePackagePart); |
|
|
|
|
|
|
|
|
|
|
|
// Analysis sharedStringsTable.xml
|
|
|
|
// Analysis sharedStringsTable.xml
|
|
|
|
analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder); |
|
|
|
analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), xlsxReadWorkbookHolder); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
XSSFReader xssfReader = new XSSFReader(pkg); |
|
|
|
XSSFReader xssfReader = new XSSFReader(pkg); |
|
|
|
analysisUse1904WindowDate(xssfReader, readWorkbookHolder); |
|
|
|
analysisUse1904WindowDate(xssfReader, xlsxReadWorkbookHolder); |
|
|
|
|
|
|
|
|
|
|
|
stylesTable = xssfReader.getStylesTable(); |
|
|
|
xlsxReadWorkbookHolder.setStylesTable(xssfReader.getStylesTable()); |
|
|
|
sheetList = new ArrayList<ReadSheet>(); |
|
|
|
sheetList = new ArrayList<ReadSheet>(); |
|
|
|
sheetMap = new HashMap<Integer, InputStream>(); |
|
|
|
sheetMap = new HashMap<Integer, InputStream>(); |
|
|
|
commentsTableMap = new HashMap<Integer, CommentsTable>(); |
|
|
|
|
|
|
|
XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); |
|
|
|
XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); |
|
|
|
int index = 0; |
|
|
|
int index = 0; |
|
|
|
if (!ite.hasNext()) { |
|
|
|
if (!ite.hasNext()) { |
|
|
@ -95,23 +81,19 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
InputStream inputStream = ite.next(); |
|
|
|
InputStream inputStream = ite.next(); |
|
|
|
sheetList.add(new ReadSheet(index, ite.getSheetName())); |
|
|
|
sheetList.add(new ReadSheet(index, ite.getSheetName())); |
|
|
|
sheetMap.put(index, inputStream); |
|
|
|
sheetMap.put(index, inputStream); |
|
|
|
CommentsTable commentsTable = ite.getSheetComments(); |
|
|
|
|
|
|
|
if (null != commentsTable) { |
|
|
|
|
|
|
|
commentsTableMap.put(index, commentsTable); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
index++; |
|
|
|
index++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void defaultReadCache(ReadWorkbookHolder readWorkbookHolder, PackagePart sharedStringsTablePackagePart) { |
|
|
|
private void defaultReadCache(XlsxReadWorkbookHolder xlsxReadWorkbookHolder, PackagePart sharedStringsTablePackagePart) { |
|
|
|
ReadCache readCache = readWorkbookHolder.getReadCacheSelector().readCache(sharedStringsTablePackagePart); |
|
|
|
ReadCache readCache = xlsxReadWorkbookHolder.getReadCacheSelector().readCache(sharedStringsTablePackagePart); |
|
|
|
readWorkbookHolder.setReadCache(readCache); |
|
|
|
xlsxReadWorkbookHolder.setReadCache(readCache); |
|
|
|
readCache.init(analysisContext); |
|
|
|
readCache.init(xlsxReadContext); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void analysisUse1904WindowDate(XSSFReader xssfReader, ReadWorkbookHolder readWorkbookHolder) |
|
|
|
private void analysisUse1904WindowDate(XSSFReader xssfReader, XlsxReadWorkbookHolder xlsxReadWorkbookHolder) |
|
|
|
throws Exception { |
|
|
|
throws Exception { |
|
|
|
if (readWorkbookHolder.globalConfiguration().getUse1904windowing() != null) { |
|
|
|
if (xlsxReadWorkbookHolder.globalConfiguration().getUse1904windowing() != null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
InputStream workbookXml = xssfReader.getWorkbookData(); |
|
|
|
InputStream workbookXml = xssfReader.getWorkbookData(); |
|
|
@ -119,38 +101,38 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
CTWorkbook wb = ctWorkbook.getWorkbook(); |
|
|
|
CTWorkbook wb = ctWorkbook.getWorkbook(); |
|
|
|
CTWorkbookPr prefix = wb.getWorkbookPr(); |
|
|
|
CTWorkbookPr prefix = wb.getWorkbookPr(); |
|
|
|
if (prefix != null && prefix.getDate1904()) { |
|
|
|
if (prefix != null && prefix.getDate1904()) { |
|
|
|
readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE); |
|
|
|
xlsxReadWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE); |
|
|
|
xlsxReadWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void analysisSharedStringsTable(InputStream sharedStringsTableInputStream, |
|
|
|
private void analysisSharedStringsTable(InputStream sharedStringsTableInputStream, |
|
|
|
ReadWorkbookHolder readWorkbookHolder) throws Exception { |
|
|
|
XlsxReadWorkbookHolder xlsxReadWorkbookHolder) throws Exception { |
|
|
|
ContentHandler handler = new SharedStringsTableHandler(readWorkbookHolder.getReadCache()); |
|
|
|
ContentHandler handler = new SharedStringsTableHandler(xlsxReadWorkbookHolder.getReadCache()); |
|
|
|
parseXmlSource(sharedStringsTableInputStream, handler); |
|
|
|
parseXmlSource(sharedStringsTableInputStream, handler); |
|
|
|
readWorkbookHolder.getReadCache().putFinished(); |
|
|
|
xlsxReadWorkbookHolder.getReadCache().putFinished(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private OPCPackage readOpcPackage(ReadWorkbookHolder readWorkbookHolder, InputStream decryptedStream) |
|
|
|
private OPCPackage readOpcPackage(XlsxReadWorkbookHolder xlsxReadWorkbookHolder, InputStream decryptedStream) |
|
|
|
throws Exception { |
|
|
|
throws Exception { |
|
|
|
if (decryptedStream == null && readWorkbookHolder.getFile() != null) { |
|
|
|
if (decryptedStream == null && xlsxReadWorkbookHolder.getFile() != null) { |
|
|
|
return OPCPackage.open(readWorkbookHolder.getFile()); |
|
|
|
return OPCPackage.open(xlsxReadWorkbookHolder.getFile()); |
|
|
|
} |
|
|
|
} |
|
|
|
if (readWorkbookHolder.getMandatoryUseInputStream()) { |
|
|
|
if (xlsxReadWorkbookHolder.getMandatoryUseInputStream()) { |
|
|
|
if (decryptedStream != null) { |
|
|
|
if (decryptedStream != null) { |
|
|
|
return OPCPackage.open(decryptedStream); |
|
|
|
return OPCPackage.open(decryptedStream); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return OPCPackage.open(readWorkbookHolder.getInputStream()); |
|
|
|
return OPCPackage.open(xlsxReadWorkbookHolder.getInputStream()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
File readTempFile = FileUtils.createCacheTmpFile(); |
|
|
|
File readTempFile = FileUtils.createCacheTmpFile(); |
|
|
|
readWorkbookHolder.setTempFile(readTempFile); |
|
|
|
xlsxReadWorkbookHolder.setTempFile(readTempFile); |
|
|
|
File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx"); |
|
|
|
File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx"); |
|
|
|
if (decryptedStream != null) { |
|
|
|
if (decryptedStream != null) { |
|
|
|
FileUtils.writeToFile(tempFile, decryptedStream); |
|
|
|
FileUtils.writeToFile(tempFile, decryptedStream); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
FileUtils.writeToFile(tempFile, readWorkbookHolder.getInputStream()); |
|
|
|
FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream()); |
|
|
|
} |
|
|
|
} |
|
|
|
return OPCPackage.open(tempFile, PackageAccess.READ); |
|
|
|
return OPCPackage.open(tempFile, PackageAccess.READ); |
|
|
|
} |
|
|
|
} |
|
|
@ -164,7 +146,7 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
InputSource inputSource = new InputSource(inputStream); |
|
|
|
InputSource inputSource = new InputSource(inputStream); |
|
|
|
try { |
|
|
|
try { |
|
|
|
SAXParserFactory saxFactory; |
|
|
|
SAXParserFactory saxFactory; |
|
|
|
String xlsxSAXParserFactoryName = analysisContext.readWorkbookHolder().getXlsxSAXParserFactoryName(); |
|
|
|
String xlsxSAXParserFactoryName = xlsxReadContext.xlsxReadWorkbookHolder().getSaxParserFactoryName(); |
|
|
|
if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) { |
|
|
|
if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) { |
|
|
|
saxFactory = SAXParserFactory.newInstance(); |
|
|
|
saxFactory = SAXParserFactory.newInstance(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -194,16 +176,14 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void execute(List<ReadSheet> readSheetList, Boolean readAll) { |
|
|
|
public void execute() { |
|
|
|
for (ReadSheet readSheet : sheetList) { |
|
|
|
for (ReadSheet readSheet : sheetList) { |
|
|
|
readSheet = SheetUtils.match(readSheet, readSheetList, readAll, |
|
|
|
readSheet = SheetUtils.match(readSheet, xlsxReadContext); |
|
|
|
analysisContext.readWorkbookHolder().getGlobalConfiguration()); |
|
|
|
|
|
|
|
if (readSheet != null) { |
|
|
|
if (readSheet != null) { |
|
|
|
analysisContext.currentSheet(readSheet); |
|
|
|
xlsxReadContext.currentSheet(readSheet); |
|
|
|
Integer sheetNo = readSheet.getSheetNo(); |
|
|
|
parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext)); |
|
|
|
parseXmlSource(sheetMap.get(sheetNo), new XlsxRowHandler(analysisContext, stylesTable, commentsTableMap.get(sheetNo))); |
|
|
|
|
|
|
|
// The last sheet is read
|
|
|
|
// The last sheet is read
|
|
|
|
analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); |
|
|
|
xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|