Close two more file-handles in Example FromHowTo

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1738035 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-04-06 19:50:20 +00:00
parent 8b10d4d71b
commit 5c1780aa72

View File

@ -40,35 +40,43 @@ import org.xml.sax.helpers.XMLReaderFactory;
public class FromHowTo { public class FromHowTo {
public void processFirstSheet(String filename) throws Exception { public void processFirstSheet(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename); OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg ); try {
SharedStringsTable sst = r.getSharedStringsTable(); XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst); XMLReader parser = fetchSheetParser(sst);
// To look up the Sheet Name / Sheet Order / rID, // To look up the Sheet Name / Sheet Order / rID,
// you need to process the core Workbook stream. // you need to process the core Workbook stream.
// Normally it's of the form rId# or rSheet# // Normally it's of the form rId# or rSheet#
InputStream sheet2 = r.getSheet("rId2"); InputStream sheet2 = r.getSheet("rId2");
InputSource sheetSource = new InputSource(sheet2); InputSource sheetSource = new InputSource(sheet2);
parser.parse(sheetSource); parser.parse(sheetSource);
sheet2.close(); sheet2.close();
} finally {
pkg.close();
}
} }
public void processAllSheets(String filename) throws Exception { public void processAllSheets(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename); OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg ); try {
SharedStringsTable sst = r.getSharedStringsTable(); XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst); XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData(); Iterator<InputStream> sheets = r.getSheetsData();
while(sheets.hasNext()) { while (sheets.hasNext()) {
System.out.println("Processing new sheet:\n"); System.out.println("Processing new sheet:\n");
InputStream sheet = sheets.next(); InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet); InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource); parser.parse(sheetSource);
sheet.close(); sheet.close();
System.out.println(""); System.out.println("");
}
} finally {
pkg.close();
} }
} }