Search in sources :

Example 1 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class XlsxSaxAnalyser method parseXmlSource.

private void parseXmlSource(InputStream inputStream, ContentHandler handler) {
    InputSource inputSource = new InputSource(inputStream);
    try {
        SAXParserFactory saxFactory;
        String xlsxSAXParserFactoryName = xlsxReadContext.xlsxReadWorkbookHolder().getSaxParserFactoryName();
        if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) {
            saxFactory = SAXParserFactory.newInstance();
        } else {
            saxFactory = SAXParserFactory.newInstance(xlsxSAXParserFactoryName, null);
        }
        try {
            saxFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        } catch (Throwable ignore) {
        }
        try {
            saxFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        } catch (Throwable ignore) {
        }
        try {
            saxFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (Throwable ignore) {
        }
        SAXParser saxParser = saxFactory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(handler);
        xmlReader.parse(inputSource);
        inputStream.close();
    } catch (IOException | ParserConfigurationException | SAXException e) {
        throw new ExcelAnalysisException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ExcelAnalysisException("Can not close 'inputStream'!");
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 2 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class ExcelAnalyserImpl method analysis.

@Override
public void analysis(List<ReadSheet> readSheetList, Boolean readAll) {
    try {
        if (!readAll && CollectionUtils.isEmpty(readSheetList)) {
            throw new IllegalArgumentException("Specify at least one read sheet.");
        }
        analysisContext.readWorkbookHolder().setParameterSheetDataList(readSheetList);
        analysisContext.readWorkbookHolder().setReadAll(readAll);
        try {
            excelReadExecutor.execute();
        } catch (ExcelAnalysisStopException e) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Custom stop!");
            }
        }
    } catch (RuntimeException e) {
        finish();
        throw e;
    } catch (Throwable e) {
        finish();
        throw new ExcelAnalysisException(e);
    }
}
Also used : ExcelAnalysisStopException(com.alibaba.excel.exception.ExcelAnalysisStopException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 3 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class XlsListSheetListener method execute.

public void execute() {
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    HSSFListener formatListener = new FormatTrackingHSSFListener(listener);
    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();
    EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
    request.addListenerForAllRecords(workbookBuildingListener);
    try {
        factory.processWorkbookEvents(request, xlsReadContext.xlsReadWorkbookHolder().getPoifsFileSystem());
    } catch (IOException e) {
        throw new ExcelAnalysisException(e);
    }
}
Also used : HSSFEventFactory(org.apache.poi.hssf.eventusermodel.HSSFEventFactory) MissingRecordAwareHSSFListener(org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener) HSSFRequest(org.apache.poi.hssf.eventusermodel.HSSFRequest) MissingRecordAwareHSSFListener(org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener) HSSFListener(org.apache.poi.hssf.eventusermodel.HSSFListener) FormatTrackingHSSFListener(org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener) IOException(java.io.IOException) EventWorkbookBuilder(org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder) FormatTrackingHSSFListener(org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 4 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class XlsSaxAnalyser method execute.

@Override
public void execute() {
    XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder();
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    xlsReadWorkbookHolder.setFormatTrackingHSSFListener(new FormatTrackingHSSFListener(listener));
    EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(xlsReadWorkbookHolder.getFormatTrackingHSSFListener());
    xlsReadWorkbookHolder.setHssfWorkbook(workbookBuildingListener.getStubHSSFWorkbook());
    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();
    request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener());
    try {
        factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem());
    } catch (IOException e) {
        throw new ExcelAnalysisException(e);
    }
}
Also used : HSSFEventFactory(org.apache.poi.hssf.eventusermodel.HSSFEventFactory) XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) MissingRecordAwareHSSFListener(org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener) HSSFRequest(org.apache.poi.hssf.eventusermodel.HSSFRequest) IOException(java.io.IOException) EventWorkbookBuilder(org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder) FormatTrackingHSSFListener(org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 5 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class FileUtils method writeToFile.

/**
 * Write inputStream to file
 *
 * @param file file
 * @param inputStream inputStream
 * @param closeInputStream closeInputStream
 */
public static void writeToFile(File file, InputStream inputStream, boolean closeInputStream) {
    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        int bytesRead;
        byte[] buffer = new byte[WRITE_BUFF_SIZE];
        while ((bytesRead = inputStream.read(buffer, 0, WRITE_BUFF_SIZE)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    } catch (Exception e) {
        throw new ExcelAnalysisException("Can not create temporary file!", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                throw new ExcelAnalysisException("Can not close 'outputStream'!", e);
            }
        }
        if (inputStream != null && closeInputStream) {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ExcelAnalysisException("Can not close 'inputStream'", e);
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException) ExcelCommonException(com.alibaba.excel.exception.ExcelCommonException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Aggregations

ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)12 IOException (java.io.IOException)5 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)3 ConstraintViolation (javax.validation.ConstraintViolation)3 ConstraintViolationException (javax.validation.ConstraintViolationException)3 XlsReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder)2 EventWorkbookBuilder (org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder)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 SyncReadListener (com.alibaba.excel.event.SyncReadListener)1 ExcelAnalysisStopException (com.alibaba.excel.exception.ExcelAnalysisStopException)1 ExcelCommonException (com.alibaba.excel.exception.ExcelCommonException)1 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 ReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder)1 CsvReadSheetHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder)1 CsvReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder)1 XlsReadSheetHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder)1 XlsxReadSheetHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)1