use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestBugs method bug56325.
@Test
public void bug56325() throws IOException {
HSSFWorkbook wb1;
POIFSFileSystem fs;
File file = HSSFTestDataSamples.getSampleFile("56325.xls");
InputStream stream = new FileInputStream(file);
try {
fs = new POIFSFileSystem(stream);
wb1 = new HSSFWorkbook(fs);
} finally {
stream.close();
}
assertEquals(3, wb1.getNumberOfSheets());
wb1.removeSheetAt(0);
assertEquals(2, wb1.getNumberOfSheets());
HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
wb1.close();
fs.close();
assertEquals(2, wb2.getNumberOfSheets());
wb2.removeSheetAt(0);
assertEquals(1, wb2.getNumberOfSheets());
wb2.removeSheetAt(0);
assertEquals(0, wb2.getNumberOfSheets());
HSSFWorkbook wb3 = writeOutAndReadBack(wb2);
wb2.close();
assertEquals(0, wb3.getNumberOfSheets());
wb3.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class TestBugs method bug51535.
/**
* Large row numbers and NPOIFS vs POIFS
*/
@SuppressWarnings("resource")
@Test
public void bug51535() throws Exception {
byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51535.xls");
HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(new ByteArrayInputStream(data)).getRoot(), false);
HSSFWorkbook wbNPOIFS = new HSSFWorkbook(new NPOIFSFileSystem(new ByteArrayInputStream(data)).getRoot(), false);
for (HSSFWorkbook wb : new HSSFWorkbook[] { wbPOIFS, wbNPOIFS }) {
assertEquals(3, wb.getNumberOfSheets());
// Check directly
HSSFSheet s = wb.getSheetAt(0);
assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());
// Extract and check
ExcelExtractor ex = new ExcelExtractor(wb);
String text = ex.getText();
assertContains(text, "Top Left Cell");
assertContains(text, "Top Right Cell");
assertContains(text, "Bottom Left Cell");
assertContains(text, "Bottom Right Cell");
ex.close();
}
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class WriteTitle method main.
/**
* <p>Runs the example program.</p>
*
* @param args Command-line arguments. The first and only command-line
* argument is the name of the POI file system to create.
* @throws IOException if any I/O exception occurs.
* @throws WritingNotSupportedException if HPSF does not (yet) support
* writing a certain property type.
*/
public static void main(final String[] args) throws WritingNotSupportedException, IOException {
/* Check whether we have exactly one command-line argument. */
if (args.length != 1) {
System.err.println("Usage: " + WriteTitle.class.getName() + "destinationPOIFS");
System.exit(1);
}
final String fileName = args[0];
/* Create a mutable property set. Initially it contains a single section
* with no properties. */
final MutablePropertySet mps = new MutablePropertySet();
/* Retrieve the section the property set already contains. */
final MutableSection ms = (MutableSection) mps.getSections().get(0);
/* Turn the property set into a summary information property. This is
* done by setting the format ID of its first section to
* SectionIDMap.SUMMARY_INFORMATION_ID. */
ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
/* Create an empty property. */
final MutableProperty p = new MutableProperty();
/* Fill the property with appropriate settings so that it specifies the
* document's title. */
p.setID(PropertyIDMap.PID_TITLE);
p.setType(Variant.VT_LPWSTR);
p.setValue("Sample title");
/* Place the property into the section. */
ms.setProperty(p);
/* Create the POI file system the property set is to be written to. */
final POIFSFileSystem poiFs = new POIFSFileSystem();
/* For writing the property set into a POI file system it has to be
* handed over to the POIFS.createDocument() method as an input stream
* which produces the bytes making out the property set stream. */
final InputStream is = mps.toInputStream();
/* Create the summary information property set in the POI file
* system. It is given the default name most (if not all) summary
* information property sets have. */
poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
/* Write the whole POI file system to a disk file. */
FileOutputStream fos = new FileOutputStream(fileName);
poiFs.writeFilesystem(fos);
fos.close();
poiFs.close();
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class CopyCompare method main.
/**
* <p>Runs the example program. The application expects one or two
* arguments:</p>
*
* <ol>
*
* <li><p>The first argument is the disk file name of the POI filesystem to
* copy.</p></li>
*
* <li><p>The second argument is optional. If it is given, it is the name of
* a disk file the copy of the POI filesystem will be written to. If it is
* not given, the copy will be written to a temporary file which will be
* deleted at the end of the program.</p></li>
*
* </ol>
*
* @param args Command-line arguments.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @exception IOException if any I/O exception occurs.
* @exception UnsupportedEncodingException if a character encoding is not
* supported.
*/
public static void main(final String[] args) throws NoPropertySetStreamException, MarkUnsupportedException, UnsupportedEncodingException, IOException {
String originalFileName = null;
String copyFileName = null;
/* Check the command-line arguments. */
if (args.length == 1) {
originalFileName = args[0];
File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2");
f.deleteOnExit();
copyFileName = f.getAbsolutePath();
} else if (args.length == 2) {
originalFileName = args[0];
copyFileName = args[1];
} else {
System.err.println("Usage: " + CopyCompare.class.getName() + "originPOIFS [copyPOIFS]");
System.exit(1);
}
/* Read the origin POIFS using the eventing API. The real work is done
* in the class CopyFile which is registered here as a POIFSReader. */
final POIFSReader r = new POIFSReader();
final CopyFile cf = new CopyFile(copyFileName);
r.registerListener(cf);
r.setNotifyEmptyDirectories(true);
FileInputStream fis = new FileInputStream(originalFileName);
r.read(fis);
fis.close();
/* Write the new POIFS to disk. */
cf.close();
/* Read all documents from the original POI file system and compare them
* with the equivalent document from the copy. */
POIFSFileSystem opfs = null, cpfs = null;
try {
opfs = new POIFSFileSystem(new File(originalFileName));
cpfs = new POIFSFileSystem(new File(copyFileName));
final DirectoryEntry oRoot = opfs.getRoot();
final DirectoryEntry cRoot = cpfs.getRoot();
final StringBuffer messages = new StringBuffer();
if (equal(oRoot, cRoot, messages)) {
System.out.println("Equal");
} else {
System.out.println("Not equal: " + messages);
}
} finally {
IOUtils.closeQuietly(cpfs);
IOUtils.closeQuietly(opfs);
}
}
use of org.apache.poi.poifs.filesystem.POIFSFileSystem in project poi by apache.
the class SavePasswordProtectedXlsx method save.
public static void save(final InputStream inputStream, final String filename, final String pwd) throws InvalidFormatException, IOException, GeneralSecurityException {
POIFSFileSystem fs = null;
FileOutputStream fos = null;
OPCPackage opc = null;
try {
fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = Encryptor.getInstance(info);
enc.confirmPassword(pwd);
opc = OPCPackage.open(inputStream);
fos = new FileOutputStream(filename);
opc.save(enc.getDataStream(fs));
fs.writeFilesystem(fos);
} finally {
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(opc);
IOUtils.closeQuietly(fs);
IOUtils.closeQuietly(inputStream);
}
}
Aggregations