Search in sources :

Example 1 with EncryptedTempData

use of org.apache.poi.poifs.crypt.temp.EncryptedTempData 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 EncryptedTempData

use of org.apache.poi.poifs.crypt.temp.EncryptedTempData 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)

Aggregations

EncryptedTempData (org.apache.poi.poifs.crypt.temp.EncryptedTempData)2 SXSSFWorkbookWithCustomZipEntrySource (org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource)2 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 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 Test (org.junit.Test)1