use of cn.hutool.poi.exceptions.POIException in project hutool by looly.
the class Excel03SaxReader method read.
/**
* 读取
*
* @param fs {@link POIFSFileSystem}
* @param sheetIndex sheet序号
* @return this
* @throws POIException IO异常包装
*/
public Excel03SaxReader read(POIFSFileSystem fs, int sheetIndex) throws POIException {
this.sheetIndex = sheetIndex;
formatListener = new FormatTrackingHSSFListener(new MissingRecordAwareHSSFListener(this));
final HSSFRequest request = new HSSFRequest();
if (isOutputFormulaValues) {
request.addListenerForAllRecords(formatListener);
} else {
workbookBuildingListener = new SheetRecordCollectingListener(formatListener);
request.addListenerForAllRecords(workbookBuildingListener);
}
final HSSFEventFactory factory = new HSSFEventFactory();
try {
factory.processWorkbookEvents(request, fs);
} catch (IOException e) {
throw new POIException(e);
}
return this;
}
use of cn.hutool.poi.exceptions.POIException in project hutool by looly.
the class ExcelSaxUtil method readFrom.
/**
* 从Excel的XML文档中读取内容,并使用{@link ContentHandler}处理
*
* @param xmlDocStream Excel的XML文档流
* @param handler 文档内容处理接口,实现此接口用于回调处理数据
* @throws DependencyException 依赖异常
* @throws POIException POI异常,包装了SAXException
* @throws IORuntimeException IO异常,如流关闭或异常等
* @since 5.1.4
*/
public static void readFrom(InputStream xmlDocStream, ContentHandler handler) throws DependencyException, POIException, IORuntimeException {
XMLReader xmlReader;
try {
// xmlReader = XMLReaderFactory.createXMLReader();
// noinspection deprecation
xmlReader = SAXHelper.newXMLReader();
} catch (SAXException | ParserConfigurationException e) {
if (e.getMessage().contains("org.apache.xerces.parsers.SAXParser")) {
throw new DependencyException(e, "You need to add 'xerces:xercesImpl' to your project and version >= 2.11.0");
} else {
throw new POIException(e);
}
}
xmlReader.setContentHandler(handler);
try {
xmlReader.parse(new InputSource(xmlDocStream));
} catch (IOException e) {
throw new IORuntimeException(e);
} catch (SAXException e) {
throw new POIException(e);
}
}
use of cn.hutool.poi.exceptions.POIException in project hutool by looly.
the class SheetRidReader method read.
/**
* 读取Wordkbook的XML中sheet标签中sheetId和rid的对应关系
*
* @param xssfReader XSSF读取器
* @return this
*/
public SheetRidReader read(XSSFReader xssfReader) {
InputStream workbookData = null;
try {
workbookData = xssfReader.getWorkbookData();
ExcelSaxUtil.readFrom(workbookData, this);
} catch (InvalidFormatException e) {
throw new POIException(e);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(workbookData);
}
return this;
}
use of cn.hutool.poi.exceptions.POIException in project hutool by looly.
the class Excel07SaxReader method read.
/**
* 开始读取Excel,Sheet编号从0开始计数
*
* @param opcPackage {@link OPCPackage},Excel包
* @param sheetIndex Excel中的sheet编号,如果为-1处理所有编号的sheet
* @return this
* @throws POIException POI异常
*/
public Excel07SaxReader read(OPCPackage opcPackage, int sheetIndex) throws POIException {
InputStream sheetInputStream = null;
try {
final XSSFReader xssfReader = new XSSFReader(opcPackage);
// 获取共享样式表
stylesTable = xssfReader.getStylesTable();
// 获取共享字符串表
this.sharedStringsTable = xssfReader.getSharedStringsTable();
if (sheetIndex > -1) {
this.sheetIndex = sheetIndex;
// 根据 rId# 或 rSheet# 查找sheet
sheetInputStream = xssfReader.getSheet(RID_PREFIX + (sheetIndex + 1));
parse(sheetInputStream);
} else {
this.sheetIndex = -1;
// 遍历所有sheet
final Iterator<InputStream> sheetInputStreams = xssfReader.getSheetsData();
while (sheetInputStreams.hasNext()) {
// 重新读取一个sheet时行归零
curRow = 0;
this.sheetIndex++;
sheetInputStream = sheetInputStreams.next();
parse(sheetInputStream);
}
}
} catch (Exception e) {
throw new POIException(e);
} finally {
IoUtil.close(sheetInputStream);
}
return this;
}
use of cn.hutool.poi.exceptions.POIException in project hutool by looly.
the class Excel07SaxReader method readSheets.
// ------------------------------------------------------------------------------ Read end
// --------------------------------------------------------------------------------------- Private method start
/**
* 开始读取Excel,Sheet编号从0开始计数
*
* @param xssfReader {@link XSSFReader},Excel读取器
* @param idOrRidOrSheetName Excel中的sheet id或者rid编号或sheet名,从0开始,rid必须加rId前缀,例如rId0,如果为-1处理所有编号的sheet
* @return this
* @throws POIException POI异常
* @since 5.4.4
*/
private Excel07SaxReader readSheets(XSSFReader xssfReader, String idOrRidOrSheetName) throws POIException {
this.handler.sheetIndex = getSheetIndex(xssfReader, idOrRidOrSheetName);
InputStream sheetInputStream = null;
try {
if (this.handler.sheetIndex > -1) {
// 根据 rId# 或 rSheet# 查找sheet
sheetInputStream = xssfReader.getSheet(RID_PREFIX + (this.handler.sheetIndex + 1));
ExcelSaxUtil.readFrom(sheetInputStream, this.handler);
this.handler.rowHandler.doAfterAllAnalysed();
} else {
this.handler.sheetIndex = -1;
// 遍历所有sheet
final Iterator<InputStream> sheetInputStreams = xssfReader.getSheetsData();
while (sheetInputStreams.hasNext()) {
// 重新读取一个sheet时行归零
this.handler.index = 0;
this.handler.sheetIndex++;
sheetInputStream = sheetInputStreams.next();
ExcelSaxUtil.readFrom(sheetInputStream, this.handler);
this.handler.rowHandler.doAfterAllAnalysed();
}
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new POIException(e);
} finally {
IoUtil.close(sheetInputStream);
}
return this;
}
Aggregations