Java Code Examples for org.apache.poi.xssf.eventusermodel.XSSFReader#getSheetsData()
The following examples show how to use
org.apache.poi.xssf.eventusermodel.XSSFReader#getSheetsData() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Xlsx2TmxHelper.java From translationstudio8 with GNU General Public License v2.0 | 6 votes |
public void parseXlsxFileAndWriteTmxBody(String fileName, AbstractWriter tmxWriter, IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException, OpenXML4JException { this.tmxWriter = tmxWriter; this.monitor = monitor; File file = new File(fileName); long length = file.length(); monitor.beginTask("", countTotal(length)); OPCPackage p = OPCPackage.open(fileName, PackageAccess.READ); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(p); XSSFReader xssfReader = new XSSFReader(p); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); try { while (iter.hasNext()) { InputStream stream = iter.next(); parse(stream, strings, tmxWriter); stream.close(); // 目前只处理第一个sheet break; } } finally { p.close(); } monitor.done(); }
Example 2
Source File: SaxExcelReader.java From myexcel with Apache License 2.0 | 6 votes |
/** * Initiates the processing of the XLS workbook file to CSV. * * @throws IOException If reading the data from the package fails. * @throws SAXException if parsing the XML data fails. */ private void process(OPCPackage xlsxPackage) throws IOException, OpenXML4JException, SAXException { long startTime = System.currentTimeMillis(); StringsCache stringsCache = new StringsCache(); try { ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(xlsxPackage, stringsCache); XSSFReader xssfReader = new XSSFReader(xlsxPackage); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); if (readConfig.readAllSheet) { while (iter.hasNext()) { try (InputStream stream = iter.next()) { processSheet(strings, new XSSFSaxReadHandler<>(result, readConfig), stream); } } } else if (!readConfig.sheetNames.isEmpty()) { while (iter.hasNext()) { try (InputStream stream = iter.next()) { if (readConfig.sheetNames.contains(iter.getSheetName())) { processSheet(strings, new XSSFSaxReadHandler<>(result, readConfig), stream); } } } } else { int index = 0; while (iter.hasNext()) { try (InputStream stream = iter.next()) { if (readConfig.sheetIndexs.contains(index)) { processSheet(strings, new XSSFSaxReadHandler<>(result, readConfig), stream); } ++index; } } } } finally { stringsCache.clearAll(); } log.info("Sax import takes {} ms", System.currentTimeMillis() - startTime); }
Example 3
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param filename * @param fileKey * @throws Exception */ public void process( String filename ) throws Exception { OPCPackage pkg = OPCPackage.open(filename); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 4
Source File: XLSX2CSV.java From azeroth with Apache License 2.0 | 6 votes |
/** * Initiates the processing of the XLS workbook file to CSV. * * @throws IOException * @throws OpenXML4JException * @throws ParserConfigurationException * @throws SAXException */ public List<String> 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(); int index = 0; while (iter.hasNext()) { if (blankRowNum == 10) { break; } InputStream stream = iter.next(); String sheetName = iter.getSheetName(); results.add(ExcelValidator.SHEET_NAME_PREFIX + sheetName); processSheet(styles, strings, new SheetToCSV(), stream); stream.close(); ++index; } return results; }
Example 5
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param is * @param fileKey * @throws Exception */ public void process( InputStream is ) throws Exception { OPCPackage pkg = OPCPackage.open( is ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 6
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param filename * @param fileKey * @throws Exception */ public void process( String filename ) throws Exception { OPCPackage pkg = OPCPackage.open( filename ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 7
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param is * @param fileKey * @throws Exception */ public void process( InputStream is ) throws Exception { OPCPackage pkg = OPCPackage.open( is ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 8
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param filename * @param fileKey * @throws Exception */ public void process( String filename ) throws Exception { OPCPackage pkg = OPCPackage.open( filename ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 9
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param is * @param fileKey * @throws Exception */ public void process( InputStream is ) throws Exception { OPCPackage pkg = OPCPackage.open( is ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 10
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param filename * @param fileKey * @throws Exception */ public void process( String filename ) throws Exception { OPCPackage pkg = OPCPackage.open( filename ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 11
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param is * @param fileKey * @throws Exception */ public void process( InputStream is ) throws Exception { OPCPackage pkg = OPCPackage.open( is ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 12
Source File: XlsxRowReader.java From translationstudio8 with GNU General Public License v2.0 | 6 votes |
public void readRows(IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException, OpenXML4JException { monitor.beginTask("", 10); monitor.worked(1); OPCPackage p = OPCPackage.open(xlsxFile, PackageAccess.READ); ReadOnlySharedStringsTable shareString = new ReadOnlySharedStringsTable(p); XSSFReader xssfReader = new XSSFReader(p); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); try { while (iter.hasNext()) { InputStream stream = iter.next(); readCells(stream, shareString, new SubProgressMonitor(monitor, 9)); stream.close(); // 目前只处理第一个sheet break; } } finally { p.close(); monitor.done(); } }
Example 13
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param is * @param fileKey * @throws Exception */ public void process( InputStream is ) throws Exception { OPCPackage pkg = OPCPackage.open( is ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 14
Source File: Excel2007Reader.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param filename * @param fileKey * @throws Exception */ public void process( String filename ) throws Exception { OPCPackage pkg = OPCPackage.open( filename ); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse( sheetSource ); sheet.close(); } //数据读取完成 }
Example 15
Source File: XSSFUtil.java From javautils with Apache License 2.0 | 6 votes |
public void processAllSheets(String filename) throws Exception { OPCPackage pkg = OPCPackage.open(filename); XSSFReader r = new XSSFReader( pkg ); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while(sheets.hasNext()) { System.out.println("Processing new sheet:\n"); InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse(sheetSource); sheet.close(); System.out.println(""); } }
Example 16
Source File: ExcelReader.java From frpMgr with MIT License | 6 votes |
/** * 遍历工作簿中所有的电子表格 * @param filename * @throws Exception */ public void process(String filename) throws Exception { OPCPackage pkg = OPCPackage.open(filename); XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); XMLReader parser = fetchSheetParser(sst); Iterator<InputStream> sheets = r.getSheetsData(); while (sheets.hasNext()) { curRow = 0; sheetIndex++; InputStream sheet = sheets.next(); InputSource sheetSource = new InputSource(sheet); parser.parse(sheetSource); sheet.close(); } }
Example 17
Source File: StreamingWorkbookReader.java From components with Apache License 2.0 | 5 votes |
void loadSheets(XSSFReader reader, SharedStringsTable sst, StylesTable stylesTable, int rowCacheSize) throws IOException, InvalidFormatException, XMLStreamException { lookupSheetNames(reader.getWorkbookData()); Iterator<InputStream> iter = reader.getSheetsData(); int i = 0; while (iter.hasNext()) { XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next()); if(i < sheetNames.size()) { sheets.add(new StreamingSheet(sheetNames.get(i++), new StreamingSheetReader(sst, stylesTable, parser, rowCacheSize))); } } }
Example 18
Source File: XlsxLoader.java From timbuctoo with GNU General Public License v3.0 | 5 votes |
@Override public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException { try { RowCellHandler rowCellHandler = makeRowCellHandler(importer); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setNamespaceAware(true); for (Tuple<String, File> file : files) { OPCPackage pkg = OPCPackage.open(file.getRight().getPath()); XSSFReader xssfReader = new XSSFReader(pkg); final SharedStringsTable sharedStringsTable = xssfReader.getSharedStringsTable(); XSSFReader.SheetIterator worksheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); while (worksheets.hasNext()) { final InputStream sheet = worksheets.next(); XMLReader sheetParser = saxFactory.newSAXParser().getXMLReader(); sheetParser.setContentHandler(new SheetXmlParser(sharedStringsTable, rowCellHandler)); rowCellHandler.start(worksheets.getSheetName()); sheetParser.parse(new InputSource(sheet)); rowCellHandler.finish(); } } } catch (SAXException | OpenXML4JException | ParserConfigurationException e) { throw new InvalidFileException("Not a valid Excel file", e); } }
Example 19
Source File: XSSFExcelPolicy.java From bdf3 with Apache License 2.0 | 5 votes |
@Override public void apply(XSSFContext context) throws Exception { OPCPackage xlsxPackage = OPCPackage.open(context.getInpuStream()); XSSFReader xssfReader = new XSSFReader(xlsxPackage); context.setStyles(xssfReader.getStylesTable()); context.setStrings(new ReadOnlySharedStringsTable(xlsxPackage)); initContext(context); XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader .getSheetsData(); while (iter.hasNext()) { InputStream stream = iter.next(); context.setInpuStream(stream); String sheetName = context.getImporterSolution().getExcelSheetName(); if (StringUtils.isNotEmpty(sheetName)) { if (sheetName.equals(iter.getSheetName())) { sheetPolicy.apply(context); break; } } else { sheetPolicy.apply(context); } stream.close(); } parseRecordPolicy.apply(context); }
Example 20
Source File: ReaderUtil.java From zerocell with Apache License 2.0 | 4 votes |
/** * Processes data from an Excel file contained in the OPCPackage using the * reader implementation * <p> * Please note that the process will read data from the first sheet in the * File when if sheet name is not specified * (i.e. the sheet name defaults to the {@link EntityHandler.DEFAULT_SHEET}) * </p> * @param opcPackage the OpenXML OPC Package * @param sheetName The sheet name * @param reader the reader implementation that handles the entity mapping */ private static void process(OPCPackage opcPackage, String sheetName, ZeroCellReader reader) { try { DataFormatter dataFormatter = new DataFormatter(); ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(opcPackage); XSSFReader xssfReader = new XSSFReader(opcPackage); StylesTable stylesTable = xssfReader.getStylesTable(); InputStream sheetInputStream = null; XSSFReader.SheetIterator sheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); while (sheets.hasNext()) { sheetInputStream = sheets.next(); if (EntityHandler.DEFAULT_SHEET.equalsIgnoreCase(sheetName)) { break; } if (sheets.getSheetName().equalsIgnoreCase(sheetName)) { break; } else { sheetInputStream = null; } } if (Objects.isNull(sheetInputStream)) { throw new SheetNotFoundException(sheetName); } XMLReader xmlReader = SAXHelper.newXMLReader(); xmlReader.setContentHandler(new XSSFSheetXMLHandler(stylesTable, strings, reader, dataFormatter, false)); xmlReader.parse(new InputSource(sheetInputStream)); sheetInputStream.close(); xmlReader = null; sheetInputStream = null; stylesTable = null; strings = null; xssfReader = null; } catch(InvalidFormatException | EmptyFileException | NotOfficeXmlFileException ife) { throw new ZeroCellException(ERROR_NOT_OPENXML); } catch(SheetNotFoundException ex) { throw new ZeroCellException(ex.getMessage()); } catch (ZeroCellException ze) { throw ze; // Rethrow the Exception } catch (Exception e) { throw new ZeroCellException("Failed to process file", e); } }