Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MissingRecordAwareHSSFListener.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.hssf.eventusermodel;
  16. import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
  17. import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
  18. import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord;
  19. import org.apache.poi.hssf.record.BOFRecord;
  20. import org.apache.poi.hssf.record.BlankRecord;
  21. import org.apache.poi.hssf.record.CellValueRecordInterface;
  22. import org.apache.poi.hssf.record.MulBlankRecord;
  23. import org.apache.poi.hssf.record.MulRKRecord;
  24. import org.apache.poi.hssf.record.NoteRecord;
  25. import org.apache.poi.hssf.record.NumberRecord;
  26. import org.apache.poi.hssf.record.Record;
  27. import org.apache.poi.hssf.record.RecordFactory;
  28. import org.apache.poi.hssf.record.RowRecord;
  29. import org.apache.poi.hssf.record.SharedFormulaRecord;
  30. /**
  31. * <p>A HSSFListener which tracks rows and columns, and will
  32. * trigger your HSSFListener for all rows and cells,
  33. * even the ones that aren't actually stored in the file.</p>
  34. * <p>This allows your code to have a more "Excel" like
  35. * view of the data in the file, and not have to worry
  36. * (as much) about if a particular row/cell is in the
  37. * file, or was skipped from being written as it was
  38. * blank.
  39. */
  40. public final class MissingRecordAwareHSSFListener implements HSSFListener {
  41. private HSSFListener childListener;
  42. // Need to have different counters for cell rows and
  43. // row rows, as you sometimes get a RowRecord in the
  44. // middle of some cells, and that'd break everything
  45. private int lastRowRow;
  46. private int lastCellRow;
  47. private int lastCellColumn;
  48. /**
  49. * Constructs a new MissingRecordAwareHSSFListener, which
  50. * will fire processRecord on the supplied child
  51. * HSSFListener for all Records, and missing records.
  52. * @param listener The HSSFListener to pass records on to
  53. */
  54. public MissingRecordAwareHSSFListener(HSSFListener listener) {
  55. resetCounts();
  56. childListener = listener;
  57. }
  58. public void processRecord(Record record) {
  59. int thisRow;
  60. int thisColumn;
  61. CellValueRecordInterface[] expandedRecords = null;
  62. if (record instanceof CellValueRecordInterface) {
  63. CellValueRecordInterface valueRec = (CellValueRecordInterface) record;
  64. thisRow = valueRec.getRow();
  65. thisColumn = valueRec.getColumn();
  66. } else {
  67. thisRow = -1;
  68. thisColumn = -1;
  69. switch (record.getSid()) {
  70. // the BOFRecord can represent either the beginning of a sheet or
  71. // the workbook
  72. case BOFRecord.sid:
  73. BOFRecord bof = (BOFRecord) record;
  74. if (bof.getType() == bof.TYPE_WORKBOOK || bof.getType() == bof.TYPE_WORKSHEET) {
  75. // Reset the row and column counts - new workbook / worksheet
  76. resetCounts();
  77. }
  78. break;
  79. case RowRecord.sid:
  80. RowRecord rowrec = (RowRecord) record;
  81. //System.out.println("Row " + rowrec.getRowNumber() + " found, first column at "
  82. // + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());
  83. // If there's a jump in rows, fire off missing row records
  84. if (lastRowRow + 1 < rowrec.getRowNumber()) {
  85. for (int i = (lastRowRow + 1); i < rowrec.getRowNumber(); i++) {
  86. MissingRowDummyRecord dr = new MissingRowDummyRecord(i);
  87. childListener.processRecord(dr);
  88. }
  89. }
  90. // Record this as the last row we saw
  91. lastRowRow = rowrec.getRowNumber();
  92. break;
  93. case SharedFormulaRecord.sid:
  94. // SharedFormulaRecord occurs after the first FormulaRecord of the cell range.
  95. // There are probably (but not always) more cell records after this
  96. // - so don't fire off the LastCellOfRowDummyRecord yet
  97. childListener.processRecord(record);
  98. return;
  99. case MulBlankRecord.sid:
  100. // These appear in the middle of the cell records, to
  101. // specify that the next bunch are empty but styled
  102. // Expand this out into multiple blank cells
  103. MulBlankRecord mbr = (MulBlankRecord)record;
  104. expandedRecords = RecordFactory.convertBlankRecords(mbr);
  105. break;
  106. case MulRKRecord.sid:
  107. // This is multiple consecutive number cells in one record
  108. // Exand this out into multiple regular number cells
  109. MulRKRecord mrk = (MulRKRecord)record;
  110. expandedRecords = RecordFactory.convertRKRecords(mrk);
  111. break;
  112. case NoteRecord.sid:
  113. NoteRecord nrec = (NoteRecord) record;
  114. thisRow = nrec.getRow();
  115. thisColumn = nrec.getColumn();
  116. break;
  117. }
  118. }
  119. // First part of expanded record handling
  120. if(expandedRecords != null && expandedRecords.length > 0) {
  121. thisRow = expandedRecords[0].getRow();
  122. thisColumn = expandedRecords[0].getColumn();
  123. }
  124. // If we're on cells, and this cell isn't in the same
  125. // row as the last one, then fire the
  126. // dummy end-of-row records
  127. if(thisRow != lastCellRow && lastCellRow > -1) {
  128. for(int i=lastCellRow; i<thisRow; i++) {
  129. int cols = -1;
  130. if(i == lastCellRow) {
  131. cols = lastCellColumn;
  132. }
  133. childListener.processRecord(new LastCellOfRowDummyRecord(i, cols));
  134. }
  135. }
  136. // If we've just finished with the cells, then fire the
  137. // final dummy end-of-row record
  138. if(lastCellRow != -1 && lastCellColumn != -1 && thisRow == -1) {
  139. childListener.processRecord(new LastCellOfRowDummyRecord(lastCellRow, lastCellColumn));
  140. lastCellRow = -1;
  141. lastCellColumn = -1;
  142. }
  143. // If we've moved onto a new row, the ensure we re-set
  144. // the column counter
  145. if(thisRow != lastCellRow) {
  146. lastCellColumn = -1;
  147. }
  148. // If there's a gap in the cells, then fire
  149. // the dummy cell records
  150. if(lastCellColumn != thisColumn-1) {
  151. for(int i=lastCellColumn+1; i<thisColumn; i++) {
  152. childListener.processRecord(new MissingCellDummyRecord(thisRow, i));
  153. }
  154. }
  155. // Next part of expanded record handling
  156. if(expandedRecords != null && expandedRecords.length > 0) {
  157. thisColumn = expandedRecords[expandedRecords.length-1].getColumn();
  158. }
  159. // Update cell and row counts as needed
  160. if(thisColumn != -1) {
  161. lastCellColumn = thisColumn;
  162. lastCellRow = thisRow;
  163. }
  164. // Pass along the record(s)
  165. if(expandedRecords != null && expandedRecords.length > 0) {
  166. for(CellValueRecordInterface r : expandedRecords) {
  167. childListener.processRecord((Record)r);
  168. }
  169. } else {
  170. childListener.processRecord(record);
  171. }
  172. }
  173. private void resetCounts() {
  174. lastRowRow = -1;
  175. lastCellRow = -1;
  176. lastCellColumn = -1;
  177. }
  178. }