org.apache.poi.xssf.model.SharedStringsTable Java Examples

The following examples show how to use org.apache.poi.xssf.model.SharedStringsTable. 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: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #2
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 7 votes vote down vote up
/**只遍历一个电子表格,其中sheetId为要遍历的sheet索引,从1开始,1-3
 * @param filename
 * @param sheetId
 * @throws Exception
 */
public void processOneSheet(String filename,int sheetId) throws Exception {
	OPCPackage pkg = OPCPackage.open(filename);
	XSSFReader r = new XSSFReader(pkg);
	SharedStringsTable sst = r.getSharedStringsTable();
	XMLReader parser = fetchSheetParser(sst);
	
	// 根据 rId# 或 rSheet# 查找sheet
	InputStream sheet2 = r.getSheet( "rId"+sheetId);
	sheetIndex++;
	InputSource sheetSource = new InputSource(sheet2);
	parser.parse(sheetSource);
	sheet2.close();
}
 
Example #3
Source File: ExcelReader.java    From frpMgr with MIT License 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #4
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #5
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @param filename
 * @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 vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @param filename
 * @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 #9
Source File: XSSFUtil.java    From javautils with Apache License 2.0 6 votes vote down vote up
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 #10
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #13
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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: StaxPoiSheetTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
public void testInlineString() throws Exception {
  final String sheetId = "1";
  final String sheetName = "Sheet 1";
  XSSFReader reader = mockXSSFReader( sheetId, SHEET_INLINE_STRINGS,
    mock( SharedStringsTable.class ),
    mock( StylesTable.class ) );
  StaxPoiSheet spSheet = new StaxPoiSheet( reader, sheetName, sheetId );
  IKCell[] rowCells = spSheet.getRow( 0 );
  assertEquals( "Test1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "Test2", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
  rowCells = spSheet.getRow( 1 );
  assertEquals( "value 1 1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "value 2 1", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
  rowCells = spSheet.getRow( 2 );
  assertEquals( "value 1 2", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "value 2 2", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
}
 
Example #15
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #16
Source File: StreamingWorkbookReader.java    From excel-streaming-reader with Apache License 2.0 6 votes vote down vote up
void loadSheets(XSSFReader reader, SharedStringsTable sst, StylesTable stylesTable, int rowCacheSize)
        throws IOException, InvalidFormatException, XMLStreamException {
  lookupSheetNames(reader);

  //Some workbooks have multiple references to the same sheet. Need to filter
  //them out before creating the XMLEventReader by keeping track of their URIs.
  //The sheets are listed in order, so we must keep track of insertion order.
  SheetIterator iter = (SheetIterator) reader.getSheetsData();
  Map<URI, InputStream> sheetStreams = new LinkedHashMap<>();
  while(iter.hasNext()) {
    InputStream is = iter.next();
    sheetStreams.put(iter.getSheetPart().getPartName().getURI(), is);
  }

  //Iterate over the loaded streams
  int i = 0;
  for(URI uri : sheetStreams.keySet()) {
    XMLEventReader parser = StaxHelper.newXMLInputFactory().createXMLEventReader(sheetStreams.get(uri));
    sheets.add(new StreamingSheet(sheetProperties.get(i++).get("name"), new StreamingSheetReader(sst, stylesTable, parser, use1904Dates, rowCacheSize)));
  }
}
 
Example #17
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #18
Source File: StaxPoiSheetTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
public void testInlineString() throws Exception {
  final String sheetId = "1";
  final String sheetName = "Sheet 1";
  XSSFReader reader = mockXSSFReader( sheetId, SHEET_INLINE_STRINGS,
    mock( SharedStringsTable.class ),
    mock( StylesTable.class ) );
  StaxPoiSheet spSheet = new StaxPoiSheet( reader, sheetName, sheetId );
  KCell[] rowCells = spSheet.getRow( 0 );
  assertEquals( "Test1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "Test2", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
  rowCells = spSheet.getRow( 1 );
  assertEquals( "value 1 1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "value 2 1", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
  rowCells = spSheet.getRow( 2 );
  assertEquals( "value 1 2", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "value 2 2", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
}
 
Example #19
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #20
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 遍历工作簿中所有的电子表格
 * @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 #21
Source File: StaxPoiSheetTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testInlineString() throws Exception {
  final String sheetId = "1";
  final String sheetName = "Sheet 1";
  XSSFReader reader = mockXSSFReader( sheetId, SHEET_INLINE_STRINGS,
    mock( SharedStringsTable.class ),
    mock( StylesTable.class ) );
  StaxPoiSheet spSheet = new StaxPoiSheet( reader, sheetName, sheetId );
  KCell[] rowCells = spSheet.getRow( 0 );
  assertEquals( "Test1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "Test2", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
  rowCells = spSheet.getRow( 1 );
  assertEquals( "value 1 1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "value 2 1", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
  rowCells = spSheet.getRow( 2 );
  assertEquals( "value 1 2", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 0 ].getType() );
  assertEquals( "value 2 2", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.STRING_FORMULA, rowCells[ 1 ].getType() );
}
 
Example #22
Source File: StaxPoiSheetTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private XSSFReader mockXSSFReader( final String sheetId,
                                   final String sheetContent, final SharedStringsTable sst, final StylesTable styles )
  throws Exception {
  XSSFReader reader = mock( XSSFReader.class );
  when( reader.getSharedStringsTable() ).thenReturn( sst );
  when( reader.getStylesTable() ).thenReturn( styles );
  when( reader.getSheet( sheetId ) ).thenAnswer( new Answer<InputStream>() {
    public InputStream answer( InvocationOnMock invocation ) throws Throwable {
      return IOUtils.toInputStream( sheetContent, "UTF-8" );
    }
  } );
  return reader;
}
 
Example #23
Source File: XlsxFileReader.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private XMLReader fetchSheetParser(StylesTable st, SharedStringsTable sst,
		XlsxRowCallBack callback, int xlsxRowsToRead) throws SAXException {
	XMLReader parser = getXMLReader( );
	ContentHandler handler = new SheetHandler(st, sst, callback, xlsxRowsToRead);
	parser.setContentHandler(handler);
	return parser;
}
 
Example #24
Source File: XlsxLoader.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@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 #25
Source File: StreamingSheetReader.java    From excel-streaming-reader with Apache License 2.0 5 votes vote down vote up
public StreamingSheetReader(SharedStringsTable sst, StylesTable stylesTable, XMLEventReader parser,
                            final boolean use1904Dates, int rowCacheSize) {
  this.sst = sst;
  this.stylesTable = stylesTable;
  this.parser = parser;
  this.use1904Dates = use1904Dates;
  this.rowCacheSize = rowCacheSize;
}
 
Example #26
Source File: XlsxFileReader.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private SheetHandler(StylesTable st, SharedStringsTable sst, XlsxRowCallBack callback, int xlsxRowsToRead) {
	this.sst = sst;
	this.st = st;
	this.callback = callback;
	values = new ArrayList<Object>();
	this.cellDataType = cDataType.NUMBER;
	this.xlsxRowsToRead = xlsxRowsToRead;
	sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" );//ISO date format
}
 
Example #27
Source File: StaxPoiSheetTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptySheet() throws Exception {
  XSSFReader reader = mockXSSFReader( "sheet1", SHEET_EMPTY,
    mock( SharedStringsTable.class ),
    mock( StylesTable.class ) );
  // check no exceptions
  StaxPoiSheet sheet = new StaxPoiSheet( reader, "empty", "sheet1" );
  for ( int j = 0; j < sheet.getRows(); j++ ) {
    sheet.getRow( j );
  }
}
 
Example #28
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**只遍历一个电子表格,其中sheetId为要遍历的sheet索引,从1开始,1-3
 * @param filename
 * @param sheetId
 * @throws Exception
 */
public void processOneSheet(String filename,int sheetId) throws Exception {
	OPCPackage pkg = OPCPackage.open(filename);
	XSSFReader r = new XSSFReader(pkg);
	SharedStringsTable sst = r.getSharedStringsTable();
	XMLReader parser = fetchSheetParser(sst);
	
	// 根据 rId# 或 rSheet# 查找sheet
	InputStream sheet2 = r.getSheet( "rId"+sheetId);
	sheetIndex++;
	InputSource sheetSource = new InputSource(sheet2);
	parser.parse(sheetSource);
	sheet2.close();
}
 
Example #29
Source File: StaxPoiSheetTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoUsedRangeSpecified() throws Exception {
  final String sheetId = "1";
  final String sheetName = "Sheet 1";
  SharedStringsTable sharedStringsTableMock =
    mockSharedStringsTable( "Report ID", "Report ID", "Approval Status", "Total Report Amount", "Policy",
      "ReportIdValue_1", "ReportIdValue_1", "ApprovalStatusValue_1", "PolicyValue_1" );
  XSSFReader reader =
    mockXSSFReader( sheetId, SHEET_NO_USED_RANGE_SPECIFIED, sharedStringsTableMock, mock( StylesTable.class ) );
  StaxPoiSheet spSheet = new StaxPoiSheet( reader, sheetName, sheetId );
  // The first row is empty - it should have empty rowCells
  KCell[] rowCells = spSheet.getRow( 0 );
  assertEquals( 0, rowCells.length );
  // The second row - is the header - just skip it
  rowCells = spSheet.getRow( 1 );
  assertEquals( 0, rowCells.length );
  // The row3 - is the first row with data - validating it
  rowCells = spSheet.getRow( 2 );
  assertEquals( KCellType.LABEL, rowCells[ 0 ].getType() );
  assertEquals( "ReportIdValue_1", rowCells[ 0 ].getValue() );
  assertEquals( KCellType.LABEL, rowCells[ 1 ].getType() );
  assertEquals( "ReportIdValue_1", rowCells[ 1 ].getValue() );
  assertEquals( KCellType.LABEL, rowCells[ 2 ].getType() );
  assertEquals( "ApprovalStatusValue_1", rowCells[ 2 ].getValue() );
  assertEquals( KCellType.NUMBER, rowCells[ 3 ].getType() );
  assertEquals( 2623.0, rowCells[ 3 ].getValue() );
  assertEquals( KCellType.LABEL, rowCells[ 4 ].getType() );
  assertEquals( "PolicyValue_1", rowCells[ 4 ].getValue() );
}
 
Example #30
Source File: Excel2007Reader.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**只遍历一个电子表格,其中sheetId为要遍历的sheet索引,从1开始,1-3
 * @param filename
 * @param sheetId
 * @throws Exception
 */
public void processOneSheet(String filename,int sheetId) throws Exception {
	OPCPackage pkg = OPCPackage.open(filename);
	XSSFReader r = new XSSFReader(pkg);
	SharedStringsTable sst = r.getSharedStringsTable();
	XMLReader parser = fetchSheetParser(sst);
	
	// 根据 rId# 或 rSheet# 查找sheet
	InputStream sheet2 = r.getSheet("rId"+sheetId);
	sheetIndex++;
	InputSource sheetSource = new InputSource(sheet2);
	parser.parse(sheetSource);
	sheet2.close();
}