<changes>
<release version="3.8-beta5" date="2011-??-??">
+ <action dev="poi-developers" type="fix">52204 - Deprecated XSSFWorkbook(String path) constructor because it does not close underlying .zip file</action>
<action dev="poi-developers" type="fix">46288 - fixed refcount of Fill pictures in HSLF </action>
<action dev="poi-developers" type="add">51961 - support compression of temp files in SXSSF </action>
<action dev="poi-developers" type="add">52268 - support cloning sheets with drawings in XSSF </action>
==================================================================== */\r
package org.apache.poi.xssf.usermodel.examples;\r
\r
+import org.apache.poi.openxml4j.opc.OPCPackage;\r
import org.apache.poi.xssf.extractor.XSSFExportToXml;\r
import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
import org.apache.poi.xssf.usermodel.XSSFMap;\r
public class CustomXMLMapping {\r
\r
public static void main(String[] args) throws Exception {\r
- XSSFWorkbook wb = new XSSFWorkbook(args[0]);\r
+ OPCPackage pkg = OPCPackage.open(args[0]);\r
+ XSSFWorkbook wb = new XSSFWorkbook(pkg);\r
\r
for (XSSFMap map : wb.getCustomXMLMappings()) {\r
XSSFExportToXml exporter = new XSSFExportToXml(map);\r
String xml = os.toString("UTF-8");\r
System.out.println(xml);\r
}\r
+ pkg.close();\r
}\r
}\r
*/\r
public class EmbeddedObjects {\r
public static void main(String[] args) throws Exception {\r
- XSSFWorkbook workbook = new XSSFWorkbook(args[0]);\r
+ OPCPackage pkg = OPCPackage.open(args[0]);\r
+ XSSFWorkbook workbook = new XSSFWorkbook(pkg);\r
for (PackagePart pPart : workbook.getAllEmbedds()) {\r
String contentType = pPart.getContentType();\r
// Excel Workbook - either binary or OpenXML\r
InputStream inputStream = pPart.getInputStream();\r
}\r
}\r
+ pkg.close();\r
}\r
}
\ No newline at end of file
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
+import java.io.FileInputStream;
+
/**
* Iterate over rows and cells
*/
public class IterateCells {
public static void main(String[] args) throws Exception {
- Workbook wb = new XSSFWorkbook(args[0]);
+ Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet sheet = wb.getSheetAt(i);
System.out.println(wb.getSheetName(i));
package org.apache.poi.xssf.dev;
+import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
public final class XSSFSave {
public static void main(String[] args) throws Exception {
for (int i = 0; i < args.length; i++) {
- XSSFWorkbook wb = new XSSFWorkbook(args[i]);
+ OPCPackage pkg = OPCPackage.open(args[i]);
+ XSSFWorkbook wb = new XSSFWorkbook(pkg);
int sep = args[i].lastIndexOf('.');
String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x");
FileOutputStream out = new FileOutputStream(outfile);
wb.write(out);
out.close();
+
+ pkg.close();
}
}
/**
* Constructs a XSSFWorkbook object given a file name.
*
+ * <p>
+ * This constructor is deprecated since POI-3.8 because it does not close
+ * the underlying .zip file stream. In short, there are two ways to open a OPC package:
+ * </p>
+ * <ol>
+ * <li>
+ * from file which leads to invoking java.util.zip.ZipFile(File file)
+ * deep in POI internals.
+ * </li>
+ * <li>
+ * from input stream in which case we first read everything into memory and
+ * then pass the data to ZipInputStream.
+ * </li>
+ * <ol>
+ * <p>
+ * It should be noted, that (2) uses quite a bit more memory than (1), which
+ * doesn't need to hold the whole zip file in memory, and can take advantage
+ * of native methods.
+ * </p>
+ * <p>
+ * To construct a workbook from file use the
+ * {@link #XSSFWorkbook(org.apache.poi.openxml4j.opc.OPCPackage)} constructor:
+ * <pre><code>
+ * OPCPackage pkg = OPCPackage.open(path);
+ * XSSFWorkbook wb = new XSSFWorkbook(pkg);
+ * // work with the wb object
+ * ......
+ * pkg.close(); // gracefully closes the underlying zip file
+ * </code></pre>
+ * </p>
+ *
* @param path the file name.
+ * @deprecated
*/
+ @Deprecated
public XSSFWorkbook(String path) throws IOException {
this(openPackage(path));
}