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.

CellWalk.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.ss.util.cellwalk;
  16. import org.apache.poi.ss.usermodel.Cell;
  17. import org.apache.poi.ss.usermodel.CellType;
  18. import org.apache.poi.ss.usermodel.Row;
  19. import org.apache.poi.ss.usermodel.Sheet;
  20. import org.apache.poi.ss.util.CellRangeAddress;
  21. /**
  22. * Traverse cell range.
  23. */
  24. public class CellWalk {
  25. private final Sheet sheet;
  26. private final CellRangeAddress range;
  27. private boolean traverseEmptyCells;
  28. public CellWalk(Sheet sheet, CellRangeAddress range) {
  29. this.sheet = sheet;
  30. this.range = range;
  31. this.traverseEmptyCells = false;
  32. }
  33. /**
  34. * Should we call handler on empty (blank) cells. Default is
  35. * false.
  36. *
  37. * @return true if handler should be called on empty (blank)
  38. * cells, false otherwise.
  39. */
  40. public boolean isTraverseEmptyCells() {
  41. return traverseEmptyCells;
  42. }
  43. /**
  44. * Sets the traverseEmptyCells property.
  45. *
  46. * @param traverseEmptyCells new property value
  47. */
  48. public void setTraverseEmptyCells(boolean traverseEmptyCells) {
  49. this.traverseEmptyCells = traverseEmptyCells;
  50. }
  51. /**
  52. * Traverse cell range from top left to bottom right cell.
  53. *
  54. * @param handler handler to call on each appropriate cell
  55. */
  56. public void traverse(CellHandler handler) {
  57. int firstRow = range.getFirstRow();
  58. int lastRow = range.getLastRow();
  59. int firstColumn = range.getFirstColumn();
  60. int lastColumn = range.getLastColumn();
  61. final int width = lastColumn - firstColumn + 1;
  62. SimpleCellWalkContext ctx = new SimpleCellWalkContext();
  63. Row currentRow;
  64. Cell currentCell;
  65. for (ctx.rowNumber = firstRow; ctx.rowNumber <= lastRow; ++ctx.rowNumber) {
  66. currentRow = sheet.getRow(ctx.rowNumber);
  67. if (currentRow == null) {
  68. continue;
  69. }
  70. for (ctx.colNumber = firstColumn; ctx.colNumber <= lastColumn; ++ctx.colNumber) {
  71. currentCell = currentRow.getCell(ctx.colNumber);
  72. if (currentCell == null) {
  73. continue;
  74. }
  75. if (isEmpty(currentCell) && !traverseEmptyCells) {
  76. continue;
  77. }
  78. long rowSize = Math.multiplyExact(Math.subtractExact(ctx.rowNumber, firstRow), (long)width);
  79. ctx.ordinalNumber = Math.addExact(rowSize, (ctx.colNumber - firstColumn + 1));
  80. handler.onCell(currentCell, ctx);
  81. }
  82. }
  83. }
  84. private boolean isEmpty(Cell cell) {
  85. return (cell.getCellType() == CellType.BLANK);
  86. }
  87. /**
  88. * Inner class to hold walk context.
  89. */
  90. private static class SimpleCellWalkContext implements CellWalkContext {
  91. private long ordinalNumber;
  92. private int rowNumber;
  93. private int colNumber;
  94. @Override
  95. public long getOrdinalNumber() {
  96. return ordinalNumber;
  97. }
  98. @Override
  99. public int getRowNumber() {
  100. return rowNumber;
  101. }
  102. @Override
  103. public int getColumnNumber() {
  104. return colNumber;
  105. }
  106. }
  107. }