Search in sources :

Example 6 with ExcelGenerateException

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

the class WriteContextImpl method doOutputStreamEncrypt07.

/**
 * To encrypt
 */
private boolean doOutputStreamEncrypt07() throws Exception {
    if (StringUtils.isEmpty(writeWorkbookHolder.getPassword()) || !ExcelTypeEnum.XLSX.equals(writeWorkbookHolder.getExcelType())) {
        return false;
    }
    if (writeWorkbookHolder.getFile() != null) {
        return false;
    }
    File tempXlsx = FileUtils.createTmpFile(UUID.randomUUID() + ".xlsx");
    FileOutputStream tempFileOutputStream = new FileOutputStream(tempXlsx);
    try {
        writeWorkbookHolder.getWorkbook().write(tempFileOutputStream);
    } finally {
        try {
            writeWorkbookHolder.getWorkbook().close();
            tempFileOutputStream.close();
        } catch (Exception e) {
            if (!tempXlsx.delete()) {
                throw new ExcelGenerateException("Can not delete temp File!");
            }
            throw e;
        }
    }
    try (POIFSFileSystem fileSystem = openFileSystemAndEncrypt(tempXlsx)) {
        fileSystem.writeFilesystem(writeWorkbookHolder.getOutputStream());
    } finally {
        if (!tempXlsx.delete()) {
            throw new ExcelGenerateException("Can not delete temp File!");
        }
    }
    return true;
}
Also used : POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException)

Example 7 with ExcelGenerateException

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

the class CsvSheet method flushData.

public void flushData() {
    try {
        for (CsvRow row : rowCache) {
            Iterator<Cell> cellIterator = row.cellIterator();
            int columnIndex = 0;
            while (cellIterator.hasNext()) {
                CsvCell csvCell = (CsvCell) cellIterator.next();
                while (csvCell.getColumnIndex() > columnIndex++) {
                    csvPrinter.print(null);
                }
                csvPrinter.print(buildCellValue(csvCell));
            }
            csvPrinter.println();
        }
        rowCache.clear();
    } catch (IOException e) {
        throw new ExcelGenerateException(e);
    }
}
Also used : IOException(java.io.IOException) Cell(org.apache.poi.ss.usermodel.Cell) ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException)

Example 8 with ExcelGenerateException

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

the class FillDataTest method t03FillCsv.

@Test
public void t03FillCsv() {
    ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class, () -> fill(fileCsv, simpleTemplateCsv));
    Assert.assertEquals("csv cannot use template.", excelGenerateException.getMessage());
}
Also used : ExcelGenerateException(com.alibaba.excel.exception.ExcelGenerateException) Test(org.junit.Test)

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