use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestNonStandardWorkbookStreamNames method testOpenWORKBOOK.
/**
* Test that we can open a file with WORKBOOK
*/
@Test
public void testOpenWORKBOOK() throws IOException {
// Try to open the workbook
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
DirectoryNode root = wb.getDirectory();
// Ensure that we have a WORKBOOK entry and a summary
assertTrue(root.hasEntry("WORKBOOK"));
assertTrue(root.hasEntry("\005SummaryInformation"));
// But not a Workbook one
assertFalse(root.hasEntry("Workbook"));
wb.close();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestNonStandardWorkbookStreamNames method testOpenBOOK.
/**
* Test that we can open a file with BOOK
*/
@Test
public void testOpenBOOK() throws IOException {
// Try to open the workbook
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsB);
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
DirectoryNode root = wb.getDirectory();
// Ensure that we have a BOOK entry
assertTrue(root.hasEntry("BOOK"));
// But not a Workbook one and not a Summary one
assertFalse(root.hasEntry("Workbook"));
assertFalse(root.hasEntry("\\005SummaryInformation"));
wb.close();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestHSSFWorkbook method testRewriteFileBug58480.
@Test
public void testRewriteFileBug58480() throws IOException {
final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls");
try {
// create new workbook
{
final Workbook workbook = new HSSFWorkbook();
final Sheet sheet = workbook.createSheet("foo");
final Row row = sheet.createRow(1);
row.createCell(1).setCellValue("bar");
writeAndCloseWorkbook(workbook, file);
}
// edit the workbook
{
NPOIFSFileSystem fs = new NPOIFSFileSystem(file, false);
try {
DirectoryNode root = fs.getRoot();
final Workbook workbook = new HSSFWorkbook(root, true);
final Sheet sheet = workbook.getSheet("foo");
sheet.getRow(1).createCell(2).setCellValue("baz");
writeAndCloseWorkbook(workbook, file);
} finally {
fs.close();
}
}
} finally {
assertTrue(file.exists());
assertTrue(file.delete());
}
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class XSSFWorkbook method addOlePackage.
@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException {
// find an unused part name
OPCPackage opc = getPackage();
PackagePartName pnOLE;
int oleId = 0;
do {
try {
pnOLE = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + (++oleId) + ".bin");
} catch (InvalidFormatException e) {
throw new IOException("ole object name not recognized", e);
}
} while (opc.containPart(pnOLE));
PackagePart pp = opc.createPart(pnOLE, "application/vnd.openxmlformats-officedocument.oleObject");
Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length + 500);
ole10.writeOut(bos);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryNode root = poifs.getRoot();
root.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
root.setStorageClsid(ClassID.OLE10_PACKAGE);
// TODO: generate CombObj stream
OutputStream os = pp.getOutputStream();
poifs.writeFilesystem(os);
os.close();
poifs.close();
return oleId;
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestEncryptor method listEntry.
private void listEntry(DocumentNode de, String ext, String path) throws IOException {
path += "\\" + de.getName().replaceAll("[\\p{Cntrl}]", "_");
System.out.println(ext + ": " + path + " (" + de.getSize() + " bytes)");
String name = de.getName().replaceAll("[\\p{Cntrl}]", "_");
InputStream is = ((DirectoryNode) de.getParent()).createDocumentInputStream(de);
FileOutputStream fos = new FileOutputStream("solr." + name + "." + ext);
IOUtils.copy(is, fos);
fos.close();
is.close();
}
Aggregations