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.

SXSSFRow.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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.streaming;
  16. import java.util.Iterator;
  17. import java.util.Map.Entry;
  18. import java.util.NoSuchElementException;
  19. import java.util.SortedMap;
  20. import java.util.Spliterator;
  21. import java.util.Spliterators;
  22. import java.util.TreeMap;
  23. import org.apache.poi.ss.SpreadsheetVersion;
  24. import org.apache.poi.ss.formula.eval.NotImplementedException;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellStyle;
  27. import org.apache.poi.ss.usermodel.CellType;
  28. import org.apache.poi.ss.usermodel.Row;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.util.Internal;
  31. import org.apache.poi.util.NotImplemented;
  32. /**
  33. * Streaming version of XSSFRow implementing the "BigGridDemo" strategy.
  34. */
  35. public class SXSSFRow implements Row, Comparable<SXSSFRow>
  36. {
  37. private static final Boolean UNDEFINED = null;
  38. private final SXSSFSheet _sheet; // parent sheet
  39. private final SortedMap<Integer, SXSSFCell> _cells = new TreeMap<>();
  40. private short _style = -1; // index of cell style in style table
  41. private short _height = -1; // row height in twips (1/20 point)
  42. private boolean _zHeight; // row zero-height (this is somehow different than being hidden)
  43. private int _outlineLevel; // Outlining level of the row, when outlining is on
  44. // use Boolean to have a tri-state for on/off/undefined
  45. private Boolean _hidden = UNDEFINED;
  46. private Boolean _collapsed = UNDEFINED;
  47. private int _rowNum;
  48. public SXSSFRow(SXSSFSheet sheet)
  49. {
  50. _sheet=sheet;
  51. }
  52. public Iterator<Cell> allCellsIterator()
  53. {
  54. return new CellIterator();
  55. }
  56. public Spliterator<Cell> allCellsSpliterator()
  57. {
  58. return Spliterators.spliterator(allCellsIterator(), getLastCellNum(), 0);
  59. }
  60. public boolean hasCustomHeight()
  61. {
  62. return _height!=-1;
  63. }
  64. @Override
  65. public int getOutlineLevel(){
  66. return _outlineLevel;
  67. }
  68. void setOutlineLevel(int level){
  69. _outlineLevel = level;
  70. }
  71. /**
  72. * get row hidden state: Hidden (true), Unhidden (false), Undefined (null)
  73. *
  74. * @return row hidden state
  75. */
  76. public Boolean getHidden() {
  77. return _hidden;
  78. }
  79. /**
  80. * set row hidden state: Hidden (true), Unhidden (false), Undefined (null)
  81. *
  82. * @param hidden row hidden state
  83. */
  84. public void setHidden(Boolean hidden) {
  85. this._hidden = hidden;
  86. }
  87. public Boolean getCollapsed() {
  88. return _collapsed;
  89. }
  90. public void setCollapsed(Boolean collapsed) {
  91. this._collapsed = collapsed;
  92. }
  93. //begin of interface implementation
  94. /**
  95. * Use this to create new cells within the row and return it.
  96. * <p>
  97. * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
  98. * either through calling {@code setCellValue} or {@code setCellType}.
  99. *
  100. * @param column - the column number this cell represents
  101. * @return Cell a high level representation of the created cell.
  102. * @throws IllegalArgumentException if columnIndex &lt; 0 or greater than the maximum number of supported columns
  103. * (255 for *.xls, 1048576 for *.xlsx)
  104. */
  105. @Override
  106. public SXSSFCell createCell(int column)
  107. {
  108. return createCell(column, CellType.BLANK);
  109. }
  110. /**
  111. * Use this to create new cells within the row and return it.
  112. * <p>
  113. * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
  114. * either through calling setCellValue or setCellType.
  115. *
  116. * @param column - the column number this cell represents
  117. * @return Cell a high level representation of the created cell.
  118. * @throws IllegalArgumentException if columnIndex &lt; 0 or greater than a maximum number of supported columns
  119. * (255 for *.xls, 1048576 for *.xlsx)
  120. */
  121. @Override
  122. public SXSSFCell createCell(int column, CellType type)
  123. {
  124. checkBounds(column);
  125. SXSSFCell cell = new SXSSFCell(this, type);
  126. _cells.put(column, cell);
  127. return cell;
  128. }
  129. /**
  130. * @throws RuntimeException if the bounds are exceeded.
  131. */
  132. private static void checkBounds(int cellIndex) {
  133. SpreadsheetVersion v = SpreadsheetVersion.EXCEL2007;
  134. int maxcol = SpreadsheetVersion.EXCEL2007.getLastColumnIndex();
  135. if (cellIndex < 0 || cellIndex > maxcol) {
  136. throw new IllegalArgumentException("Invalid column index (" + cellIndex
  137. + "). Allowable column range for " + v.name() + " is (0.."
  138. + maxcol + ") or ('A'..'" + v.getLastColumnName() + "')");
  139. }
  140. }
  141. /**
  142. * Remove the Cell from this row.
  143. *
  144. * @param cell the cell to remove
  145. */
  146. @Override
  147. public void removeCell(Cell cell)
  148. {
  149. int index = getCellIndex((SXSSFCell) cell);
  150. _cells.remove(index);
  151. }
  152. /**
  153. * Return the column number of a cell if it is in this row
  154. * Otherwise return -1
  155. *
  156. * @param cell the cell to get the index of
  157. * @return cell column index if it is in this row, -1 otherwise
  158. */
  159. /*package*/ int getCellIndex(SXSSFCell cell)
  160. {
  161. for (Entry<Integer, SXSSFCell> entry : _cells.entrySet()) {
  162. if (entry.getValue()==cell) {
  163. return entry.getKey();
  164. }
  165. }
  166. return -1;
  167. }
  168. /**
  169. * Set the row number of this row.
  170. *
  171. * @param rowNum the row number (0-based)
  172. * @throws IllegalArgumentException if rowNum &lt; 0
  173. */
  174. @Override
  175. public void setRowNum(int rowNum)
  176. {
  177. this._rowNum = rowNum;
  178. _sheet.changeRowNum(this, rowNum);
  179. }
  180. /**
  181. * Get row number this row represents
  182. *
  183. * @return the row number (0 based)
  184. */
  185. @Override
  186. public int getRowNum()
  187. {
  188. return _rowNum;
  189. }
  190. /**
  191. * Get the cell representing a given column (logical cell) 0-based.
  192. * If cell is missing or blank, uses the workbook's MissingCellPolicy
  193. * to determine the return value.
  194. *
  195. * @param cellnum 0 based column number
  196. * @return Cell representing that column or null if undefined.
  197. * @see #getCell(int, org.apache.poi.ss.usermodel.Row.MissingCellPolicy)
  198. * @throws RuntimeException if cellnum is out of bounds
  199. */
  200. @Override
  201. public SXSSFCell getCell(int cellnum) {
  202. MissingCellPolicy policy = _sheet.getWorkbook().getMissingCellPolicy();
  203. return getCell(cellnum, policy);
  204. }
  205. /**
  206. * Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
  207. *
  208. * @return the cell at the given (0 based) index
  209. * @throws IllegalArgumentException if cellnum &lt; 0 or the specified MissingCellPolicy is invalid
  210. */
  211. @Override
  212. public SXSSFCell getCell(int cellnum, MissingCellPolicy policy)
  213. {
  214. checkBounds(cellnum);
  215. final SXSSFCell cell = _cells.get(cellnum);
  216. switch (policy) {
  217. case RETURN_NULL_AND_BLANK:
  218. return cell;
  219. case RETURN_BLANK_AS_NULL:
  220. boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK);
  221. return (isBlank) ? null : cell;
  222. case CREATE_NULL_AS_BLANK:
  223. return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell;
  224. default:
  225. throw new IllegalArgumentException("Illegal policy " + policy);
  226. }
  227. }
  228. /**
  229. * Get the number of the first cell contained in this row.
  230. *
  231. * @return short representing the first logical cell in the row,
  232. * or -1 if the row does not contain any cells.
  233. */
  234. @Override
  235. public short getFirstCellNum()
  236. {
  237. try {
  238. return _cells.firstKey().shortValue();
  239. } catch (final NoSuchElementException e) {
  240. return -1;
  241. }
  242. }
  243. /**
  244. * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
  245. * happens to be the 1-based column number of the last cell. This value can be used as a
  246. * standard upper bound when iterating over cells:
  247. * <pre>
  248. * short minColIx = row.getFirstCellNum();
  249. * short maxColIx = row.getLastCellNum();
  250. * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
  251. * Cell cell = row.getCell(colIx);
  252. * if(cell == null) {
  253. * continue;
  254. * }
  255. * //... do something with cell
  256. * }
  257. * </pre>
  258. *
  259. * @return short representing the last logical cell in the row <b>PLUS ONE</b>,
  260. * or -1 if the row does not contain any cells.
  261. */
  262. @Override
  263. public short getLastCellNum()
  264. {
  265. return _cells.isEmpty() ? -1 : (short)(_cells.lastKey() + 1);
  266. }
  267. /**
  268. * Gets the number of defined cells (NOT number of cells in the actual row!).
  269. * That is to say if only columns 0,4,5 have values then there would be 3.
  270. *
  271. * @return int representing the number of defined cells in the row.
  272. */
  273. @Override
  274. public int getPhysicalNumberOfCells()
  275. {
  276. return _cells.size();
  277. }
  278. /**
  279. * Set the row's height or set to ff (-1) for undefined/default-height. Set the height in "twips" or
  280. * 1/20th of a point.
  281. *
  282. * @param height rowheight or 0xff for undefined (use sheet default)
  283. */
  284. @Override
  285. public void setHeight(short height)
  286. {
  287. _height=height;
  288. }
  289. /**
  290. * Set whether or not to display this row with 0 height
  291. *
  292. * @param zHeight height is zero or not.
  293. */
  294. @Override
  295. public void setZeroHeight(boolean zHeight)
  296. {
  297. _zHeight=zHeight;
  298. }
  299. /**
  300. * Get whether or not to display this row with 0 height
  301. *
  302. * @return - zHeight height is zero or not.
  303. */
  304. @Override
  305. public boolean getZeroHeight()
  306. {
  307. return _zHeight;
  308. }
  309. /**
  310. * Set the row's height in points.
  311. *
  312. * @param height the height in points. <code>-1</code> resets to the default height
  313. */
  314. @Override
  315. public void setHeightInPoints(float height)
  316. {
  317. if(height==-1) {
  318. _height=-1;
  319. } else {
  320. _height=(short)(height*20);
  321. }
  322. }
  323. /**
  324. * Get the row's height measured in twips (1/20th of a point). If the height is not set, the default worksheet value is returned,
  325. * See {@link Sheet#getDefaultRowHeightInPoints()}
  326. *
  327. * @return row height measured in twips (1/20th of a point)
  328. */
  329. @Override
  330. public short getHeight()
  331. {
  332. return (short)(_height==-1?getSheet().getDefaultRowHeightInPoints()*20:_height);
  333. }
  334. /**
  335. * Returns row height measured in point size. If the height is not set, the default worksheet value is returned,
  336. * See {@link Sheet#getDefaultRowHeightInPoints()}
  337. *
  338. * @return row height measured in point size
  339. * @see Sheet#getDefaultRowHeightInPoints()
  340. */
  341. @Override
  342. public float getHeightInPoints()
  343. {
  344. return (float)(_height==-1?getSheet().getDefaultRowHeightInPoints():_height/20.0);
  345. }
  346. /**
  347. * Is this row formatted? Most aren't, but some rows
  348. * do have whole-row styles. For those that do, you
  349. * can get the formatting from {@link #getRowStyle()}
  350. */
  351. @Override
  352. public boolean isFormatted() {
  353. return _style > -1;
  354. }
  355. /**
  356. * Returns the whole-row cell style. Most rows won't
  357. * have one of these, so will return null. Call
  358. * {@link #isFormatted()} to check first.
  359. */
  360. @Override
  361. public CellStyle getRowStyle() {
  362. if(!isFormatted()) {
  363. return null;
  364. }
  365. return getSheet().getWorkbook().getCellStyleAt(_style);
  366. }
  367. @Internal
  368. /*package*/ int getRowStyleIndex() {
  369. return _style;
  370. }
  371. /**
  372. * Applies a whole-row cell styling to the row.
  373. * The row style can be cleared by passing in <code>null</code>.
  374. */
  375. @Override
  376. public void setRowStyle(CellStyle style) {
  377. if(style == null) {
  378. _style = -1;
  379. } else {
  380. _style = style.getIndex();
  381. }
  382. }
  383. /**
  384. * {@inheritDoc}
  385. */
  386. @Override
  387. public Iterator<Cell> cellIterator()
  388. {
  389. return new FilledCellIterator();
  390. }
  391. /**
  392. * Create a spliterator over the cells from [0, getLastCellNum()).
  393. * Includes blank cells, excludes empty cells
  394. *
  395. * Returns a spliterator over all filled cells (created via Row.createCell())
  396. * Throws ConcurrentModificationException if cells are added, moved, or
  397. * removed after the spliterator is created.
  398. *
  399. * @since POI 5.2.0
  400. */
  401. @Override
  402. @SuppressWarnings("unchecked")
  403. public Spliterator<Cell> spliterator() {
  404. return (Spliterator<Cell>)(Spliterator<? extends Cell>) _cells.values().spliterator();
  405. }
  406. /**
  407. * Returns the Sheet this row belongs to
  408. *
  409. * @return the Sheet that owns this row
  410. */
  411. @Override
  412. public SXSSFSheet getSheet()
  413. {
  414. return _sheet;
  415. }
  416. //end of interface implementation
  417. void setRowNumWithoutUpdatingSheet(int rowNum)
  418. {
  419. this._rowNum = rowNum;
  420. }
  421. /**
  422. * Create an iterator over the cells from [0, getLastCellNum()).
  423. * Includes blank cells, excludes empty cells
  424. *
  425. * Returns an iterator over all filled cells (created via Row.createCell())
  426. * Throws ConcurrentModificationException if cells are added, moved, or
  427. * removed after the iterator is created.
  428. */
  429. public class FilledCellIterator implements Iterator<Cell>
  430. {
  431. private final Iterator<SXSSFCell> iter = _cells.values().iterator();
  432. @Override
  433. public boolean hasNext()
  434. {
  435. return iter.hasNext();
  436. }
  437. @Override
  438. public Cell next() throws NoSuchElementException
  439. {
  440. return iter.next();
  441. }
  442. @Override
  443. public void remove()
  444. {
  445. throw new UnsupportedOperationException();
  446. }
  447. }
  448. /**
  449. * returns all cells including empty cells (<code>null</code> values are returned
  450. * for empty cells).
  451. * This method is not synchronized. This iterator should not be used after
  452. * cells are added, moved, or removed, though a ConcurrentModificationException
  453. * is NOT thrown.
  454. */
  455. public class CellIterator implements Iterator<Cell>
  456. {
  457. final int maxColumn = getLastCellNum(); //last column PLUS ONE
  458. int pos;
  459. @Override
  460. public boolean hasNext()
  461. {
  462. return pos < maxColumn;
  463. }
  464. @Override
  465. public Cell next() throws NoSuchElementException
  466. {
  467. if (hasNext()) {
  468. return _cells.get(pos++);
  469. } else {
  470. throw new NoSuchElementException();
  471. }
  472. }
  473. @Override
  474. public void remove()
  475. {
  476. throw new UnsupportedOperationException();
  477. }
  478. }
  479. /**
  480. * Compares two <code>SXSSFRow</code> objects. Two rows are equal if they belong to the same worksheet and
  481. * their row indexes are equal.
  482. *
  483. * @param other the <code>SXSSFRow</code> to be compared.
  484. * @return <ul>
  485. * <li>
  486. * the value <code>0</code> if the row number of this <code>SXSSFRow</code> is
  487. * equal to the row number of the argument <code>SXSSFRow</code>
  488. * </li>
  489. * <li>
  490. * a value less than <code>0</code> if the row number of this this <code>SXSSFRow</code> is
  491. * numerically less than the row number of the argument <code>SXSSFRow</code>
  492. * </li>
  493. * <li>
  494. * a value greater than <code>0</code> if the row number of this this <code>SXSSFRow</code> is
  495. * numerically greater than the row number of the argument <code>SXSSFRow</code>
  496. * </li>
  497. * </ul>
  498. * @throws IllegalArgumentException if the argument row belongs to a different worksheet
  499. */
  500. @Override
  501. public int compareTo(SXSSFRow other) {
  502. if (this.getSheet() != other.getSheet()) {
  503. throw new IllegalArgumentException("The compared rows must belong to the same sheet");
  504. }
  505. int thisRow = this.getRowNum();
  506. int otherRow = other.getRowNum();
  507. return Integer.compare(thisRow, otherRow);
  508. }
  509. @Override
  510. public boolean equals(Object obj)
  511. {
  512. if (!(obj instanceof SXSSFRow))
  513. {
  514. return false;
  515. }
  516. SXSSFRow other = (SXSSFRow) obj;
  517. return (this.getRowNum() == other.getRowNum()) &&
  518. (this.getSheet() == other.getSheet());
  519. }
  520. @Override
  521. public int hashCode() {
  522. return _cells.hashCode();
  523. }
  524. @Override
  525. @NotImplemented
  526. public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step){
  527. throw new NotImplementedException("shiftCellsRight");
  528. }
  529. @Override
  530. @NotImplemented
  531. public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step){
  532. throw new NotImplementedException("shiftCellsLeft");
  533. }
  534. }