Domanda

I need to read XLSM file metadata, to files less than 4 MB The following instructions work correctly:

try {
OPCPackage pkg = OPCPackage.open (new FileInputStream ("C:\\Path to file.xlsm"));
XSSFWorkbook XSSFWorkbook = new document (pkg);
documento.getProperties poixmlProperties = ();
...
} catch (Exception ex) {
...
} finally {
...
}

For files larger than 4 MB does not run the second line (XSSFWorkbook document=new XSSFWorkbook (pkg)), jump directly to the finally block without giving any errors.

È stato utile?

Soluzione

Firstly, don't use an InputStream when you have a File object! Using an InputStream means POI has to buffer you whole thing into memory, while with a File it can leave it on disk until needed. As covered in the POI documentation, instead open with:

OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));

Secondly, you seem to be opening up the whole Excel structure and parsing that into memory, when all you want is the metadata. Don't! Firing up XSSFWorkbook takes quite a lot of processing and memory, which you don't need if all you care about is metadata, which lives in a separate area of the file

Instead, just load the properties themselves, and work with that. Your final code would be something like:

OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
POIXMLProperties props = new POIXMLProperties(pkg);
System.out.println("The title is " + props.getCorePart().getTitle());

That ought to load in only a fraction of the file's total size as memory use

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top