Writing out sheet to text file using POI event model
Posted
by
Eduardo Dennis
on Stack Overflow
See other posts from Stack Overflow
or by Eduardo Dennis
Published on 2013-11-11T14:45:34Z
Indexed on
2013/11/11
15:54 UTC
Read the original article
Hit count: 311
I am using XLSX2CSV example to parse large sheets from a workbook. Since I only need to output the data for specific sheets I added an if statement in the process method to test for the specific sheets. When the condition is met I continue with the process.
public void process()
throws IOException, OpenXML4JException, ParserConfigurationException, SAXException {
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(this.xlsxPackage);
XSSFReader xssfReader = new XSSFReader(this.xlsxPackage);
StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
while (iter.hasNext()) {
InputStream stream = iter.next();
String sheetName = iter.getSheetName();
if (sheetName.equals("SHEET1")||sheetName.equals("SHEET2")||sheetName.equals("SHEET3")||sheetName.equals("SHEET4")||sheetName.equals("SHEET5")){
processSheet(styles, strings, stream);
try {
System.setOut(new PrintStream(
new FileOutputStream("C:\\Users\\edennis.AD\\Desktop\\test\\"+sheetName+".txt")));
} catch (Exception e) {
e.printStackTrace();
}
stream.close();
}
}
}
But I need to output text file and not sure how to do it. I tried to use the System.set() method to output everything from system.out to text but that's not working I just get blank files.
© Stack Overflow or respective owner