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.

EventExample.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.examples.hssf.usermodel;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
  20. import org.apache.poi.hssf.eventusermodel.HSSFListener;
  21. import org.apache.poi.hssf.eventusermodel.HSSFRequest;
  22. import org.apache.poi.hssf.record.BOFRecord;
  23. import org.apache.poi.hssf.record.BoundSheetRecord;
  24. import org.apache.poi.hssf.record.LabelSSTRecord;
  25. import org.apache.poi.hssf.record.NumberRecord;
  26. import org.apache.poi.hssf.record.RowRecord;
  27. import org.apache.poi.hssf.record.SSTRecord;
  28. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  29. /**
  30. * This example shows how to use the event API for reading a file.
  31. */
  32. @SuppressWarnings({"java:S106","java:S4823"})
  33. public class EventExample implements HSSFListener {
  34. private SSTRecord sstrec;
  35. /**
  36. * This method listens for incoming records and handles them as required.
  37. * @param record The record that was found while reading.
  38. */
  39. @Override
  40. public void processRecord(org.apache.poi.hssf.record.Record record)
  41. {
  42. switch (record.getSid())
  43. {
  44. // the BOFRecord can represent either the beginning of a sheet or the workbook
  45. case BOFRecord.sid:
  46. BOFRecord bof = (BOFRecord) record;
  47. if (bof.getType() == BOFRecord.TYPE_WORKBOOK)
  48. {
  49. System.out.println("Encountered workbook");
  50. // assigned to the class level member
  51. } else if (bof.getType() == BOFRecord.TYPE_WORKSHEET)
  52. {
  53. System.out.println("Encountered sheet reference");
  54. }
  55. break;
  56. case BoundSheetRecord.sid:
  57. BoundSheetRecord bsr = (BoundSheetRecord) record;
  58. System.out.println("New sheet named: " + bsr.getSheetname());
  59. break;
  60. case RowRecord.sid:
  61. RowRecord rowrec = (RowRecord) record;
  62. System.out.println("Row found, first column at "
  63. + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());
  64. break;
  65. case NumberRecord.sid:
  66. NumberRecord numrec = (NumberRecord) record;
  67. System.out.println("Cell found with value " + numrec.getValue()
  68. + " at row " + numrec.getRow() + " and column " + numrec.getColumn());
  69. break;
  70. // SSTRecords store a array of unique strings used in Excel.
  71. case SSTRecord.sid:
  72. sstrec = (SSTRecord) record;
  73. for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
  74. {
  75. System.out.println("String table value " + k + " = " + sstrec.getString(k));
  76. }
  77. break;
  78. case LabelSSTRecord.sid:
  79. LabelSSTRecord lrec = (LabelSSTRecord) record;
  80. System.out.println("String cell found with value "
  81. + sstrec.getString(lrec.getSSTIndex()));
  82. break;
  83. }
  84. }
  85. /**
  86. * Read an excel file and spit out what we find.
  87. *
  88. * @param args Expect one argument that is the file to read.
  89. * @throws IOException When there is an error processing the file.
  90. */
  91. public static void main(String[] args) throws IOException
  92. {
  93. // create a new file input stream with the input file specified
  94. // at the command line
  95. try (FileInputStream fin = new FileInputStream(args[0])) {
  96. // create a new org.apache.poi.poifs.filesystem.Filesystem
  97. try (POIFSFileSystem poifs = new POIFSFileSystem(fin)) {
  98. // get the Workbook (excel part) stream in a InputStream
  99. try (InputStream din = poifs.createDocumentInputStream("Workbook")) {
  100. // construct out HSSFRequest object
  101. HSSFRequest req = new HSSFRequest();
  102. // lazy listen for ALL records with the listener shown above
  103. req.addListenerForAllRecords(new EventExample());
  104. // create our event factory
  105. HSSFEventFactory factory = new HSSFEventFactory();
  106. // process our events based on the document input stream
  107. factory.processEvents(req, din);
  108. }
  109. }
  110. }
  111. System.out.println("done.");
  112. }
  113. }