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();
}
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();
}
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();
}
Aggregations