use try-with-resources in more places
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1817247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
89a788ea5b
commit
5f4f1b5846
@ -209,12 +209,12 @@ public class XLSX2CSV {
|
|||||||
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
|
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
InputStream stream = iter.next();
|
try (InputStream stream = iter.next()) {
|
||||||
String sheetName = iter.getSheetName();
|
String sheetName = iter.getSheetName();
|
||||||
this.output.println();
|
this.output.println();
|
||||||
this.output.println(sheetName + " [index=" + index + "]:");
|
this.output.println(sheetName + " [index=" + index + "]:");
|
||||||
processSheet(styles, strings, new SheetToCSV(), stream);
|
processSheet(styles, strings, new SheetToCSV(), stream);
|
||||||
stream.close();
|
}
|
||||||
++index;
|
++index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -237,9 +237,9 @@ public class XLSX2CSV {
|
|||||||
minColumns = Integer.parseInt(args[1]);
|
minColumns = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
// The package open is instantaneous, as it should be.
|
// The package open is instantaneous, as it should be.
|
||||||
OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ);
|
try (OPCPackage p = OPCPackage.open(xlsxFile.getPath(), PackageAccess.READ)) {
|
||||||
XLSX2CSV xlsx2csv = new XLSX2CSV(p, System.out, minColumns);
|
XLSX2CSV xlsx2csv = new XLSX2CSV(p, System.out, minColumns);
|
||||||
xlsx2csv.process();
|
xlsx2csv.process();
|
||||||
p.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,10 @@ import java.util.Iterator;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
import org.apache.poi.openxml4j.opc.PackageAccess;
|
import org.apache.poi.openxml4j.opc.PackageAccess;
|
||||||
|
import org.apache.poi.util.SAXHelper;
|
||||||
import org.apache.poi.xssf.eventusermodel.XLSX2CSV;
|
import org.apache.poi.xssf.eventusermodel.XLSX2CSV;
|
||||||
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
||||||
import org.apache.poi.xssf.model.SharedStringsTable;
|
import org.apache.poi.xssf.model.SharedStringsTable;
|
||||||
@ -33,7 +36,6 @@ import org.xml.sax.InputSource;
|
|||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.XMLReader;
|
import org.xml.sax.XMLReader;
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
import org.xml.sax.helpers.XMLReaderFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XSSF and SAX (Event API) basic example.
|
* XSSF and SAX (Event API) basic example.
|
||||||
@ -49,10 +51,10 @@ public class FromHowTo {
|
|||||||
XMLReader parser = fetchSheetParser(sst);
|
XMLReader parser = fetchSheetParser(sst);
|
||||||
|
|
||||||
// process the first sheet
|
// process the first sheet
|
||||||
InputStream sheet2 = r.getSheetsData().next();
|
try (InputStream sheet = r.getSheetsData().next()) {
|
||||||
InputSource sheetSource = new InputSource(sheet2);
|
InputSource sheetSource = new InputSource(sheet);
|
||||||
parser.parse(sheetSource);
|
parser.parse(sheetSource);
|
||||||
sheet2.close();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,17 +68,17 @@ public class FromHowTo {
|
|||||||
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();
|
try (InputStream sheet = sheets.next()) {
|
||||||
InputSource sheetSource = new InputSource(sheet);
|
InputSource sheetSource = new InputSource(sheet);
|
||||||
parser.parse(sheetSource);
|
parser.parse(sheetSource);
|
||||||
sheet.close();
|
}
|
||||||
System.out.println("");
|
System.out.println("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
|
public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException, ParserConfigurationException {
|
||||||
XMLReader parser = XMLReaderFactory.createXMLReader();
|
XMLReader parser = SAXHelper.newXMLReader();
|
||||||
ContentHandler handler = new SheetHandler(sst);
|
ContentHandler handler = new SheetHandler(sst);
|
||||||
parser.setContentHandler(handler);
|
parser.setContentHandler(handler);
|
||||||
return parser;
|
return parser;
|
||||||
|
@ -39,11 +39,13 @@ public class HybridStreaming {
|
|||||||
private static final String SHEET_TO_STREAM = "large sheet";
|
private static final String SHEET_TO_STREAM = "large sheet";
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, SAXException {
|
public static void main(String[] args) throws IOException, SAXException {
|
||||||
InputStream sourceBytes = new FileInputStream("workbook.xlsx");
|
try (InputStream sourceBytes = new FileInputStream("workbook.xlsx")) {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
|
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
|
||||||
/** Avoid DOM parse of large sheet */
|
/**
|
||||||
|
* Avoid DOM parse of large sheet
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void parseSheet(java.util.Map<String,XSSFSheet> shIdMap, CTSheet ctSheet) {
|
public void parseSheet(java.util.Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
|
||||||
if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
|
if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
|
||||||
super.parseSheet(shIdMap, ctSheet);
|
super.parseSheet(shIdMap, ctSheet);
|
||||||
}
|
}
|
||||||
@ -54,7 +56,7 @@ public class HybridStreaming {
|
|||||||
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
|
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
|
||||||
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
|
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
|
||||||
workbook.close();
|
workbook.close();
|
||||||
sourceBytes.close();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SheetContentsHandler createSheetContentsHandler() {
|
private static SheetContentsHandler createSheetContentsHandler() {
|
||||||
|
Loading…
Reference in New Issue
Block a user