Search in sources :

Example 1 with SXSSFWorkbookWithCustomZipEntrySource

use of org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource in project poi by apache.

the class SavePasswordProtectedXlsx method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        throw new IllegalArgumentException("Expected 2 params: filename and password");
    }
    TempFileUtils.checkTempFiles();
    String filename = args[0];
    String password = args[1];
    SXSSFWorkbookWithCustomZipEntrySource wb = new SXSSFWorkbookWithCustomZipEntrySource();
    try {
        for (int i = 0; i < 10; i++) {
            SXSSFSheet sheet = wb.createSheet("Sheet" + i);
            for (int r = 0; r < 1000; r++) {
                SXSSFRow row = sheet.createRow(r);
                for (int c = 0; c < 100; c++) {
                    SXSSFCell cell = row.createCell(c);
                    cell.setCellValue("abcd");
                }
            }
        }
        EncryptedTempData tempData = new EncryptedTempData();
        try {
            wb.write(tempData.getOutputStream());
            save(tempData.getInputStream(), filename, password);
            System.out.println("Saved " + filename);
        } finally {
            tempData.dispose();
        }
    } finally {
        wb.close();
        wb.dispose();
    }
    TempFileUtils.checkTempFiles();
}
Also used : EncryptedTempData(org.apache.poi.poifs.crypt.temp.EncryptedTempData) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) SXSSFRow(org.apache.poi.xssf.streaming.SXSSFRow) SXSSFCell(org.apache.poi.xssf.streaming.SXSSFCell) SXSSFSheet(org.apache.poi.xssf.streaming.SXSSFSheet)

Example 2 with SXSSFWorkbookWithCustomZipEntrySource

use of org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource in project poi by apache.

the class TestSXSSFWorkbookWithCustomZipEntrySource method customZipEntrySourceForWriteAndRead.

// write an encrypted workbook to disk, and encrypt any temporary files as well
@Test
public void customZipEntrySourceForWriteAndRead() throws IOException, GeneralSecurityException, InvalidFormatException {
    SXSSFWorkbookWithCustomZipEntrySource workbook = new SXSSFWorkbookWithCustomZipEntrySource();
    SXSSFSheet sheet1 = workbook.createSheet(sheetName);
    SXSSFRow row1 = sheet1.createRow(1);
    SXSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    EncryptedTempData tempData = new EncryptedTempData();
    workbook.write(tempData.getOutputStream());
    workbook.close();
    workbook.dispose();
    ZipEntrySource zipEntrySource = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());
    tempData.dispose();
    OPCPackage opc = OPCPackage.open(zipEntrySource);
    XSSFWorkbook xwb = new XSSFWorkbook(opc);
    zipEntrySource.close();
    XSSFSheet xs1 = xwb.getSheetAt(0);
    assertEquals(sheetName, xs1.getSheetName());
    XSSFRow xr1 = xs1.getRow(1);
    XSSFCell xc1 = xr1.getCell(1);
    assertEquals(cellValue, xc1.getStringCellValue());
    xwb.close();
    opc.close();
}
Also used : EncryptedTempData(org.apache.poi.poifs.crypt.temp.EncryptedTempData) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource) AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 3 with SXSSFWorkbookWithCustomZipEntrySource

use of org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource in project poi by apache.

the class TestSXSSFWorkbookWithCustomZipEntrySource method customZipEntrySource.

// write an unencrypted workbook to disk, but any temporary files are encrypted
@Test
public void customZipEntrySource() throws IOException {
    SXSSFWorkbookWithCustomZipEntrySource workbook = new SXSSFWorkbookWithCustomZipEntrySource();
    SXSSFSheet sheet1 = workbook.createSheet(sheetName);
    SXSSFRow row1 = sheet1.createRow(1);
    SXSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    ByteArrayOutputStream os = new ByteArrayOutputStream(8192);
    workbook.write(os);
    workbook.close();
    workbook.dispose();
    XSSFWorkbook xwb = new XSSFWorkbook(new ByteArrayInputStream(os.toByteArray()));
    XSSFSheet xs1 = xwb.getSheetAt(0);
    assertEquals(sheetName, xs1.getSheetName());
    XSSFRow xr1 = xs1.getRow(1);
    XSSFCell xc1 = xr1.getCell(1);
    assertEquals(cellValue, xc1.getStringCellValue());
    xwb.close();
}
Also used : SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SXSSFWorkbookWithCustomZipEntrySource (org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource)3 EncryptedTempData (org.apache.poi.poifs.crypt.temp.EncryptedTempData)2 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 ZipEntrySource (org.apache.poi.openxml4j.util.ZipEntrySource)1 AesZipFileZipEntrySource (org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource)1 SXSSFCell (org.apache.poi.xssf.streaming.SXSSFCell)1 SXSSFRow (org.apache.poi.xssf.streaming.SXSSFRow)1 SXSSFSheet (org.apache.poi.xssf.streaming.SXSSFSheet)1