Search in sources :

Example 1 with POIException

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;
}
Also used : HSSFEventFactory(org.apache.poi.hssf.eventusermodel.HSSFEventFactory) SheetRecordCollectingListener(org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener) MissingRecordAwareHSSFListener(org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener) HSSFRequest(org.apache.poi.hssf.eventusermodel.HSSFRequest) IOException(java.io.IOException) FormatTrackingHSSFListener(org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener) POIException(cn.hutool.poi.exceptions.POIException)

Example 2 with POIException

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) IORuntimeException(cn.hutool.core.io.IORuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) DependencyException(cn.hutool.core.exceptions.DependencyException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) POIException(cn.hutool.poi.exceptions.POIException)

Example 3 with POIException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) POIException(cn.hutool.poi.exceptions.POIException)

Example 4 with POIException

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;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) POIException(cn.hutool.poi.exceptions.POIException) SAXException(org.xml.sax.SAXException) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) POIException(cn.hutool.poi.exceptions.POIException)

Example 5 with POIException

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;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) InputStream(java.io.InputStream) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) IOException(java.io.IOException) IORuntimeException(cn.hutool.core.io.IORuntimeException) POIException(cn.hutool.poi.exceptions.POIException) POIException(cn.hutool.poi.exceptions.POIException)

Aggregations

POIException (cn.hutool.poi.exceptions.POIException)8 IOException (java.io.IOException)7 IORuntimeException (cn.hutool.core.io.IORuntimeException)4 InputStream (java.io.InputStream)3 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)3 SheetRecordCollectingListener (org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener)2 FormatTrackingHSSFListener (org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener)2 HSSFEventFactory (org.apache.poi.hssf.eventusermodel.HSSFEventFactory)2 HSSFRequest (org.apache.poi.hssf.eventusermodel.HSSFRequest)2 MissingRecordAwareHSSFListener (org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener)2 SAXException (org.xml.sax.SAXException)2 DependencyException (cn.hutool.core.exceptions.DependencyException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)1 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1 BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)1 EOFRecord (org.apache.poi.hssf.record.EOFRecord)1 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)1 SSTRecord (org.apache.poi.hssf.record.SSTRecord)1