Search in sources :

Example 6 with POIException

use of cn.hutool.poi.exceptions.POIException in project hutool by looly.

the class Excel03SaxReader method read.

/**
 * 读取
 *
 * @param fs  {@link POIFSFileSystem}
 * @param idOrRidOrSheetName sheet id或者rid编号或sheet名称,从0开始,rid必须加rId前缀,例如rId0,如果为-1处理所有编号的sheet
 * @return this
 * @throws POIException IO异常包装
 */
public Excel03SaxReader read(POIFSFileSystem fs, String idOrRidOrSheetName) throws POIException {
    this.rid = getSheetIndex(idOrRidOrSheetName);
    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);
    } finally {
        IoUtil.close(fs);
    }
    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 7 with POIException

use of cn.hutool.poi.exceptions.POIException in project hutool by looly.

the class Excel03SaxReader method processRecord.

/**
 * HSSFListener 监听方法,处理 Record
 *
 * @param record 记录
 */
@Override
public void processRecord(Record record) {
    if (this.rid > -1 && this.curRid > this.rid) {
        // 指定Sheet之后的数据不再处理
        return;
    }
    if (record instanceof BoundSheetRecord) {
        // Sheet边界记录,此Record中可以获得Sheet名
        final BoundSheetRecord boundSheetRecord = (BoundSheetRecord) record;
        boundSheetRecords.add(boundSheetRecord);
        final String currentSheetName = boundSheetRecord.getSheetname();
        if (null != this.sheetName && StrUtil.equals(this.sheetName, currentSheetName)) {
            this.rid = this.boundSheetRecords.size() - 1;
        }
    } else if (record instanceof SSTRecord) {
        // 静态字符串表
        sstRecord = (SSTRecord) record;
    } else if (record instanceof BOFRecord) {
        BOFRecord bofRecord = (BOFRecord) record;
        if (bofRecord.getType() == BOFRecord.TYPE_WORKSHEET) {
            // 如果有需要,则建立子工作薄
            if (workbookBuildingListener != null && stubWorkbook == null) {
                stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
            }
            curRid++;
        }
    } else if (record instanceof EOFRecord) {
        if (this.rid < 0 && null != this.sheetName) {
            throw new POIException("Sheet [{}] not exist!", this.sheetName);
        }
        processLastCellSheet();
    } else if (isProcessCurrentSheet()) {
        if (record instanceof MissingCellDummyRecord) {
            // 空值的操作
            MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
            addToRowCellList(mc);
        } else if (record instanceof LastCellOfRowDummyRecord) {
            // 行结束
            processLastCell((LastCellOfRowDummyRecord) record);
        } else {
            // 处理单元格值
            processCellValue(record);
        }
    }
}
Also used : EOFRecord(org.apache.poi.hssf.record.EOFRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) POIException(cn.hutool.poi.exceptions.POIException)

Example 8 with POIException

use of cn.hutool.poi.exceptions.POIException in project hutool by looly.

the class Word07Writer method addPicture.

/**
 * 增加图片,单独成段落,增加后图片流关闭
 *
 * @param in       图片流
 * @param picType  图片类型,见Document.PICTURE_TYPE_XXX
 * @param fileName 文件名
 * @param width    宽度
 * @param height   高度
 * @param align    图片的对齐方式
 * @return this
 * @since 5.2.4
 */
public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height, ParagraphAlignment align) {
    final XWPFParagraph paragraph = doc.createParagraph();
    paragraph.setAlignment(align);
    final XWPFRun run = paragraph.createRun();
    try {
        run.addPicture(in, picType.getValue(), fileName, Units.toEMU(width), Units.toEMU(height));
    } catch (InvalidFormatException e) {
        throw new POIException(e);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(in);
    }
    return this;
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) 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