1 回答
TA贡献1783条经验 获得超4个赞
请不要坚持使用过时的软件版本。这样做不是我们在软件开发方面取得进步的方式。
但当然,XSSF and SAX (Event API)版本也存在3.15。
但在这个旧版本中SharedStringsTable已经没有办法了getItemAt。它只有getEntryAtwhich 返回 a org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst。所以类中的代码SAXExcelSheetHandler必须更改为:
...
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
...
...
public void endElement(String uri, String localName, String name)
throws SAXException {
// Process the last contents as required.
// Do now, as characters() may be called more than once
if(nextIsString) {
int idx = Integer.parseInt(lastContents);
//lastContents = sst.getItemAt(idx).getString();
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
nextIsString = false;
}
// v => contents of a cell
// Output after we've seen the string contents
if(name.equals("v")) {
System.out.println(lastContents);
}
}
...
也还没有一个SAXHelper. 所以类的代码POISaxXaafV2必须像这样改变:
...
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
//import org.apache.poi.ooxml.util.SAXHelper;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
...
...
OPCPackage opcPackage = OPCPackage.open(file);
XSSFReader xssfReader = new XSSFReader(opcPackage);
SharedStringsTable sharedStringsTable = xssfReader.getSharedStringsTable();
//XMLReader xmlParser = SAXHelper.newXMLReader();
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
XMLReader xmlParser = parser.getXMLReader();
ContentHandler contentHandler = new SAXExcelSheetHandler(sharedStringsTable);
xmlParser.setContentHandler(contentHandler);
...
添加回答
举报