Search in sources :

Example 1 with ExcelGenerateException

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

the class WriteContextImpl method finish.

@Override
public void finish(boolean onException) {
    if (finished) {
        return;
    }
    finished = true;
    WriteHandlerUtils.afterWorkbookDispose(writeWorkbookHolder.getWorkbookWriteHandlerContext());
    if (writeWorkbookHolder == null) {
        return;
    }
    Throwable throwable = null;
    boolean isOutputStreamEncrypt = false;
    // Determine if you need to write excel
    boolean writeExcel = !onException;
    if (writeWorkbookHolder.getWriteExcelOnException()) {
        writeExcel = Boolean.TRUE;
    }
    // No data is written if an exception is thrown
    if (writeExcel) {
        try {
            isOutputStreamEncrypt = doOutputStreamEncrypt07();
        } catch (Throwable t) {
            throwable = t;
        }
    }
    if (!isOutputStreamEncrypt) {
        try {
            if (writeExcel) {
                writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
            }
            writeWorkbookHolder.getWorkbook().close();
        } catch (Throwable t) {
            throwable = t;
        }
    }
    try {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        if (workbook instanceof SXSSFWorkbook) {
            ((SXSSFWorkbook) workbook).dispose();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
            writeWorkbookHolder.getOutputStream().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    if (writeExcel && !isOutputStreamEncrypt) {
        try {
            doFileEncrypt07();
        } catch (Throwable t) {
            throwable = t;
        }
    }
    try {
        if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
            writeWorkbookHolder.getTempTemplateInputStream().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    clearEncrypt03();
    removeThreadLocalCache();
    if (throwable != null) {
        throw new ExcelGenerateException("Can not close IO.", throwable);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Finished write.");
    }
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) WriteWorkbook(com.alibaba.excel.write.metadata.WriteWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 2 with ExcelGenerateException

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

the class ExcelWriteFillExecutor method createCell.

private void createCell(AnalysisCell analysisCell, FillConfig fillConfig, CellWriteHandlerContext cellWriteHandlerContext, RowWriteHandlerContext rowWriteHandlerContext) {
    Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet();
    if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) {
        Row row = cachedSheet.getRow(analysisCell.getRowIndex());
        cellWriteHandlerContext.setRow(row);
        Cell cell = row.getCell(analysisCell.getColumnIndex());
        cellWriteHandlerContext.setCell(cell);
        rowWriteHandlerContext.setRow(row);
        rowWriteHandlerContext.setRowIndex(analysisCell.getRowIndex());
        return;
    }
    Sheet sheet = writeContext.writeSheetHolder().getSheet();
    Map<AnalysisCell, Integer> collectionLastIndexMap = collectionLastIndexCache.computeIfAbsent(currentUniqueDataFlag, key -> MapUtils.newHashMap());
    boolean isOriginalCell = false;
    Integer lastRowIndex;
    Integer lastColumnIndex;
    switch(fillConfig.getDirection()) {
        case VERTICAL:
            lastRowIndex = collectionLastIndexMap.get(analysisCell);
            if (lastRowIndex == null) {
                lastRowIndex = analysisCell.getRowIndex();
                collectionLastIndexMap.put(analysisCell, lastRowIndex);
                isOriginalCell = true;
            } else {
                collectionLastIndexMap.put(analysisCell, ++lastRowIndex);
            }
            lastColumnIndex = analysisCell.getColumnIndex();
            break;
        case HORIZONTAL:
            lastRowIndex = analysisCell.getRowIndex();
            lastColumnIndex = collectionLastIndexMap.get(analysisCell);
            if (lastColumnIndex == null) {
                lastColumnIndex = analysisCell.getColumnIndex();
                collectionLastIndexMap.put(analysisCell, lastColumnIndex);
                isOriginalCell = true;
            } else {
                collectionLastIndexMap.put(analysisCell, ++lastColumnIndex);
            }
            break;
        default:
            throw new ExcelGenerateException("The wrong direction.");
    }
    Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell, rowWriteHandlerContext);
    cellWriteHandlerContext.setRow(row);
    cellWriteHandlerContext.setRowIndex(lastRowIndex);
    cellWriteHandlerContext.setColumnIndex(lastColumnIndex);
    Cell cell = createCellIfNecessary(row, lastColumnIndex, cellWriteHandlerContext);
    cellWriteHandlerContext.setCell(cell);
    if (isOriginalCell) {
        Map<AnalysisCell, CellStyle> collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent(currentUniqueDataFlag, key -> MapUtils.newHashMap());
        collectionFieldStyleMap.put(analysisCell, cell.getCellStyle());
    }
}
Also used : AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) AnalysisCell(com.alibaba.excel.write.metadata.fill.AnalysisCell) Cell(org.apache.poi.ss.usermodel.Cell) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException)

Example 3 with ExcelGenerateException

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

the class ExcelBuilderImpl method fill.

@Override
public void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) {
    try {
        if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) {
            throw new ExcelGenerateException("Calling the 'fill' method must use a template.");
        }
        if (context.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) {
            throw new ExcelGenerateException("csv does not support filling data.");
        }
        context.currentSheet(writeSheet, WriteTypeEnum.FILL);
        if (excelWriteFillExecutor == null) {
            excelWriteFillExecutor = new ExcelWriteFillExecutor(context);
        }
        excelWriteFillExecutor.fill(data, fillConfig);
    } catch (RuntimeException e) {
        finishOnException();
        throw e;
    } catch (Throwable e) {
        finishOnException();
        throw new ExcelGenerateException(e);
    }
}
Also used : ExcelWriteFillExecutor(com.alibaba.excel.write.executor.ExcelWriteFillExecutor) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException)

Example 4 with ExcelGenerateException

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

the class ExcelBuilderImpl method addContent.

@Override
public void addContent(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable) {
    try {
        context.currentSheet(writeSheet, WriteTypeEnum.ADD);
        context.currentTable(writeTable);
        if (excelWriteAddExecutor == null) {
            excelWriteAddExecutor = new ExcelWriteAddExecutor(context);
        }
        excelWriteAddExecutor.add(data);
    } catch (RuntimeException e) {
        finishOnException();
        throw e;
    } catch (Throwable e) {
        finishOnException();
        throw new ExcelGenerateException(e);
    }
}
Also used : ExcelWriteAddExecutor(com.alibaba.excel.write.executor.ExcelWriteAddExecutor) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException)

Example 5 with ExcelGenerateException

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

the class WriteWorkbookHolder method copyTemplate.

private void copyTemplate() throws IOException {
    if (writeWorkbook.getTemplateFile() == null && writeWorkbook.getTemplateInputStream() == null) {
        return;
    }
    if (this.excelType == ExcelTypeEnum.CSV) {
        throw new ExcelGenerateException("csv cannot use template.");
    }
    byte[] templateFileByte = null;
    if (writeWorkbook.getTemplateFile() != null) {
        templateFileByte = FileUtils.readFileToByteArray(writeWorkbook.getTemplateFile());
    } else if (writeWorkbook.getTemplateInputStream() != null) {
        try {
            templateFileByte = IoUtils.toByteArray(writeWorkbook.getTemplateInputStream());
        } finally {
            if (autoCloseStream) {
                writeWorkbook.getTemplateInputStream().close();
            }
        }
    }
    this.tempTemplateInputStream = new ByteArrayInputStream(templateFileByte);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException)

Aggregations

ExcelGenerateException (com.alibaba.excel.exception.ExcelGenerateException)8 Cell (org.apache.poi.ss.usermodel.Cell)2 ExcelWriteAddExecutor (com.alibaba.excel.write.executor.ExcelWriteAddExecutor)1 ExcelWriteFillExecutor (com.alibaba.excel.write.executor.ExcelWriteFillExecutor)1 WriteWorkbook (com.alibaba.excel.write.metadata.WriteWorkbook)1 AnalysisCell (com.alibaba.excel.write.metadata.fill.AnalysisCell)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 Test (org.junit.Test)1