You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FromHowTo.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.eventusermodel.examples;
  16. import java.io.InputStream;
  17. import java.util.Iterator;
  18. import org.apache.poi.xssf.eventusermodel.XSSFReader;
  19. import org.apache.poi.xssf.model.SharedStringsTable;
  20. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.xml.sax.Attributes;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.InputSource;
  25. import org.xml.sax.SAXException;
  26. import org.xml.sax.XMLReader;
  27. import org.xml.sax.helpers.DefaultHandler;
  28. import org.xml.sax.helpers.XMLReaderFactory;
  29. /**
  30. * XSSF and SAX (Event API)
  31. */
  32. public class FromHowTo {
  33. public void processOneSheet(String filename) throws Exception {
  34. OPCPackage pkg = OPCPackage.open(filename);
  35. XSSFReader r = new XSSFReader( pkg );
  36. SharedStringsTable sst = r.getSharedStringsTable();
  37. XMLReader parser = fetchSheetParser(sst);
  38. // rId2 found by processing the Workbook
  39. // Seems to either be rId# or rSheet#
  40. InputStream sheet2 = r.getSheet("rId2");
  41. InputSource sheetSource = new InputSource(sheet2);
  42. parser.parse(sheetSource);
  43. sheet2.close();
  44. }
  45. public void processAllSheets(String filename) throws Exception {
  46. OPCPackage pkg = OPCPackage.open(filename);
  47. XSSFReader r = new XSSFReader( pkg );
  48. SharedStringsTable sst = r.getSharedStringsTable();
  49. XMLReader parser = fetchSheetParser(sst);
  50. Iterator<InputStream> sheets = r.getSheetsData();
  51. while(sheets.hasNext()) {
  52. System.out.println("Processing new sheet:\n");
  53. InputStream sheet = sheets.next();
  54. InputSource sheetSource = new InputSource(sheet);
  55. parser.parse(sheetSource);
  56. sheet.close();
  57. System.out.println("");
  58. }
  59. }
  60. public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
  61. XMLReader parser =
  62. XMLReaderFactory.createXMLReader(
  63. "org.apache.xerces.parsers.SAXParser"
  64. );
  65. ContentHandler handler = new SheetHandler(sst);
  66. parser.setContentHandler(handler);
  67. return parser;
  68. }
  69. /**
  70. * See org.xml.sax.helpers.DefaultHandler javadocs
  71. */
  72. private static class SheetHandler extends DefaultHandler {
  73. private SharedStringsTable sst;
  74. private String lastContents;
  75. private boolean nextIsString;
  76. private SheetHandler(SharedStringsTable sst) {
  77. this.sst = sst;
  78. }
  79. public void startElement(String uri, String localName, String name,
  80. Attributes attributes) throws SAXException {
  81. // c => cell
  82. if(name.equals("c")) {
  83. // Print the cell reference
  84. System.out.print(attributes.getValue("r") + " - ");
  85. // Figure out if the value is an index in the SST
  86. String cellType = attributes.getValue("t");
  87. if(cellType != null && cellType.equals("s")) {
  88. nextIsString = true;
  89. } else {
  90. nextIsString = false;
  91. }
  92. }
  93. // Clear contents cache
  94. lastContents = "";
  95. }
  96. public void endElement(String uri, String localName, String name)
  97. throws SAXException {
  98. // Process the last contents as required.
  99. // Do now, as characters() may be called more than once
  100. if(nextIsString) {
  101. int idx = Integer.parseInt(lastContents);
  102. lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
  103. nextIsString = false;
  104. }
  105. // v => contents of a cell
  106. // Output after we've seen the string contents
  107. if(name.equals("v")) {
  108. System.out.println(lastContents);
  109. }
  110. }
  111. public void characters(char[] ch, int start, int length)
  112. throws SAXException {
  113. lastContents += new String(ch, start, length);
  114. }
  115. }
  116. public static void main(String[] args) throws Exception {
  117. FromHowTo howto = new FromHowTo();
  118. howto.processOneSheet(args[0]);
  119. howto.processAllSheets(args[0]);
  120. }
  121. }