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.

XSSFRow.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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.usermodel;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.Set;
  19. import java.util.TreeMap;
  20. import org.apache.poi.ss.formula.FormulaShifter;
  21. import org.apache.poi.ss.SpreadsheetVersion;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellCopyPolicy;
  24. import org.apache.poi.ss.usermodel.CellStyle;
  25. import org.apache.poi.ss.usermodel.Row;
  26. import org.apache.poi.ss.util.CellRangeAddress;
  27. import org.apache.poi.ss.util.CellReference;
  28. import org.apache.poi.util.Beta;
  29. import org.apache.poi.util.Internal;
  30. import org.apache.poi.xssf.model.CalculationChain;
  31. import org.apache.poi.xssf.model.StylesTable;
  32. import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter;
  33. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
  34. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
  35. /**
  36. * High level representation of a row of a spreadsheet.
  37. */
  38. public class XSSFRow implements Row, Comparable<XSSFRow> {
  39. //private static final POILogger _logger = POILogFactory.getLogger(XSSFRow.class);
  40. /**
  41. * the xml bean containing all cell definitions for this row
  42. */
  43. private final CTRow _row;
  44. /**
  45. * Cells of this row keyed by their column indexes.
  46. * The TreeMap ensures that the cells are ordered by columnIndex in the ascending order.
  47. */
  48. private final TreeMap<Integer, XSSFCell> _cells;
  49. /**
  50. * the parent sheet
  51. */
  52. private final XSSFSheet _sheet;
  53. /**
  54. * Construct a XSSFRow.
  55. *
  56. * @param row the xml bean containing all cell definitions for this row.
  57. * @param sheet the parent sheet.
  58. */
  59. @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
  60. protected XSSFRow(CTRow row, XSSFSheet sheet) {
  61. _row = row;
  62. _sheet = sheet;
  63. _cells = new TreeMap<Integer, XSSFCell>();
  64. for (CTCell c : row.getCArray()) {
  65. XSSFCell cell = new XSSFCell(this, c);
  66. _cells.put(cell.getColumnIndex(), cell);
  67. sheet.onReadCell(cell);
  68. }
  69. }
  70. /**
  71. * Returns the XSSFSheet this row belongs to
  72. *
  73. * @return the XSSFSheet that owns this row
  74. */
  75. public XSSFSheet getSheet() {
  76. return this._sheet;
  77. }
  78. /**
  79. * Cell iterator over the physically defined cells:
  80. * <blockquote><pre>
  81. * for (Iterator<Cell> it = row.cellIterator(); it.hasNext(); ) {
  82. * Cell cell = it.next();
  83. * ...
  84. * }
  85. * </pre></blockquote>
  86. *
  87. * @return an iterator over cells in this row.
  88. */
  89. @SuppressWarnings("unchecked")
  90. public Iterator<Cell> cellIterator() {
  91. return (Iterator<Cell>)(Iterator<? extends Cell>)_cells.values().iterator();
  92. }
  93. /**
  94. * Alias for {@link #cellIterator()} to allow foreach loops:
  95. * <blockquote><pre>
  96. * for(Cell cell : row){
  97. * ...
  98. * }
  99. * </pre></blockquote>
  100. *
  101. * @return an iterator over cells in this row.
  102. */
  103. public Iterator<Cell> iterator() {
  104. return cellIterator();
  105. }
  106. /**
  107. * Compares two <code>XSSFRow</code> objects. Two rows are equal if they belong to the same worksheet and
  108. * their row indexes are equal.
  109. *
  110. * @param row the <code>XSSFRow</code> to be compared.
  111. * @return the value <code>0</code> if the row number of this <code>XSSFRow</code> is
  112. * equal to the row number of the argument <code>XSSFRow</code>; a value less than
  113. * <code>0</code> if the row number of this this <code>XSSFRow</code> is numerically less
  114. * than the row number of the argument <code>XSSFRow</code>; and a value greater
  115. * than <code>0</code> if the row number of this this <code>XSSFRow</code> is numerically
  116. * greater than the row number of the argument <code>XSSFRow</code>.
  117. * @throws IllegalArgumentException if the argument row belongs to a different worksheet
  118. */
  119. public int compareTo(XSSFRow row) {
  120. int thisVal = this.getRowNum();
  121. if(row.getSheet() != getSheet()) throw new IllegalArgumentException("The compared rows must belong to the same XSSFSheet");
  122. int anotherVal = row.getRowNum();
  123. return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
  124. }
  125. /**
  126. * Use this to create new cells within the row and return it.
  127. * <p>
  128. * The cell that is returned is a {@link Cell#CELL_TYPE_BLANK}. The type can be changed
  129. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
  130. * </p>
  131. * @param columnIndex - the column number this cell represents
  132. * @return Cell a high level representation of the created cell.
  133. * @throws IllegalArgumentException if columnIndex < 0 or greater than 16384,
  134. * the maximum number of columns supported by the SpreadsheetML format (.xlsx)
  135. */
  136. public XSSFCell createCell(int columnIndex) {
  137. return createCell(columnIndex, Cell.CELL_TYPE_BLANK);
  138. }
  139. /**
  140. * Use this to create new cells within the row and return it.
  141. *
  142. * @param columnIndex - the column number this cell represents
  143. * @param type - the cell's data type
  144. * @return XSSFCell a high level representation of the created cell.
  145. * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0
  146. * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx)
  147. * @see Cell#CELL_TYPE_BLANK
  148. * @see Cell#CELL_TYPE_BOOLEAN
  149. * @see Cell#CELL_TYPE_ERROR
  150. * @see Cell#CELL_TYPE_FORMULA
  151. * @see Cell#CELL_TYPE_NUMERIC
  152. * @see Cell#CELL_TYPE_STRING
  153. */
  154. public XSSFCell createCell(int columnIndex, int type) {
  155. CTCell ctCell;
  156. XSSFCell prev = _cells.get(columnIndex);
  157. if(prev != null){
  158. ctCell = prev.getCTCell();
  159. ctCell.set(CTCell.Factory.newInstance());
  160. } else {
  161. ctCell = _row.addNewC();
  162. }
  163. XSSFCell xcell = new XSSFCell(this, ctCell);
  164. xcell.setCellNum(columnIndex);
  165. if (type != Cell.CELL_TYPE_BLANK) {
  166. xcell.setCellType(type);
  167. }
  168. _cells.put(columnIndex, xcell);
  169. return xcell;
  170. }
  171. /**
  172. * Returns the cell at the given (0 based) index,
  173. * with the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} from the parent Workbook.
  174. *
  175. * @return the cell at the given (0 based) index
  176. */
  177. public XSSFCell getCell(int cellnum) {
  178. return getCell(cellnum, _sheet.getWorkbook().getMissingCellPolicy());
  179. }
  180. /**
  181. * Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
  182. *
  183. * @return the cell at the given (0 based) index
  184. * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid
  185. * @see Row#RETURN_NULL_AND_BLANK
  186. * @see Row#RETURN_BLANK_AS_NULL
  187. * @see Row#CREATE_NULL_AS_BLANK
  188. */
  189. public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
  190. if(cellnum < 0) throw new IllegalArgumentException("Cell index must be >= 0");
  191. XSSFCell cell = _cells.get(cellnum);
  192. if(policy == RETURN_NULL_AND_BLANK) {
  193. return cell;
  194. }
  195. if(policy == RETURN_BLANK_AS_NULL) {
  196. if(cell == null) return cell;
  197. if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
  198. return null;
  199. }
  200. return cell;
  201. }
  202. if(policy == CREATE_NULL_AS_BLANK) {
  203. if(cell == null) {
  204. return createCell((short)cellnum, Cell.CELL_TYPE_BLANK);
  205. }
  206. return cell;
  207. }
  208. throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
  209. }
  210. /**
  211. * Get the number of the first cell contained in this row.
  212. *
  213. * @return short representing the first logical cell in the row,
  214. * or -1 if the row does not contain any cells.
  215. */
  216. public short getFirstCellNum() {
  217. return (short)(_cells.size() == 0 ? -1 : _cells.firstKey());
  218. }
  219. /**
  220. * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
  221. * happens to be the 1-based column number of the last cell. This value can be used as a
  222. * standard upper bound when iterating over cells:
  223. * <pre>
  224. * short minColIx = row.getFirstCellNum();
  225. * short maxColIx = row.getLastCellNum();
  226. * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
  227. * XSSFCell cell = row.getCell(colIx);
  228. * if(cell == null) {
  229. * continue;
  230. * }
  231. * //... do something with cell
  232. * }
  233. * </pre>
  234. *
  235. * @return short representing the last logical cell in the row <b>PLUS ONE</b>,
  236. * or -1 if the row does not contain any cells.
  237. */
  238. public short getLastCellNum() {
  239. return (short)(_cells.size() == 0 ? -1 : (_cells.lastKey() + 1));
  240. }
  241. /**
  242. * 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,
  243. * See {@link org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()}
  244. *
  245. * @return row height measured in twips (1/20th of a point)
  246. */
  247. public short getHeight() {
  248. return (short)(getHeightInPoints()*20);
  249. }
  250. /**
  251. * Returns row height measured in point size. If the height is not set, the default worksheet value is returned,
  252. * See {@link org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()}
  253. *
  254. * @return row height measured in point size
  255. * @see org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()
  256. */
  257. public float getHeightInPoints() {
  258. if (this._row.isSetHt()) {
  259. return (float) this._row.getHt();
  260. }
  261. return _sheet.getDefaultRowHeightInPoints();
  262. }
  263. /**
  264. * Set the height in "twips" or 1/20th of a point.
  265. *
  266. * @param height the height in "twips" or 1/20th of a point. <code>-1</code> resets to the default height
  267. */
  268. public void setHeight(short height) {
  269. if (height == -1) {
  270. if (_row.isSetHt()) _row.unsetHt();
  271. if (_row.isSetCustomHeight()) _row.unsetCustomHeight();
  272. } else {
  273. _row.setHt((double) height / 20);
  274. _row.setCustomHeight(true);
  275. }
  276. }
  277. /**
  278. * Set the row's height in points.
  279. *
  280. * @param height the height in points. <code>-1</code> resets to the default height
  281. */
  282. public void setHeightInPoints(float height) {
  283. setHeight((short)(height == -1 ? -1 : (height*20)));
  284. }
  285. /**
  286. * Gets the number of defined cells (NOT number of cells in the actual row!).
  287. * That is to say if only columns 0,4,5 have values then there would be 3.
  288. *
  289. * @return int representing the number of defined cells in the row.
  290. */
  291. public int getPhysicalNumberOfCells() {
  292. return _cells.size();
  293. }
  294. /**
  295. * Get row number this row represents
  296. *
  297. * @return the row number (0 based)
  298. */
  299. public int getRowNum() {
  300. return (int) (_row.getR() - 1);
  301. }
  302. /**
  303. * Set the row number of this row.
  304. *
  305. * @param rowIndex the row number (0-based)
  306. * @throws IllegalArgumentException if rowNum < 0 or greater than 1048575
  307. */
  308. public void setRowNum(int rowIndex) {
  309. int maxrow = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
  310. if (rowIndex < 0 || rowIndex > maxrow) {
  311. throw new IllegalArgumentException("Invalid row number (" + rowIndex
  312. + ") outside allowable range (0.." + maxrow + ")");
  313. }
  314. _row.setR(rowIndex + 1);
  315. }
  316. /**
  317. * Get whether or not to display this row with 0 height
  318. *
  319. * @return - height is zero or not.
  320. */
  321. public boolean getZeroHeight() {
  322. return this._row.getHidden();
  323. }
  324. /**
  325. * Set whether or not to display this row with 0 height
  326. *
  327. * @param height height is zero or not.
  328. */
  329. public void setZeroHeight(boolean height) {
  330. this._row.setHidden(height);
  331. }
  332. /**
  333. * Is this row formatted? Most aren't, but some rows
  334. * do have whole-row styles. For those that do, you
  335. * can get the formatting from {@link #getRowStyle()}
  336. */
  337. public boolean isFormatted() {
  338. return _row.isSetS();
  339. }
  340. /**
  341. * Returns the whole-row cell style. Most rows won't
  342. * have one of these, so will return null. Call
  343. * {@link #isFormatted()} to check first.
  344. */
  345. public XSSFCellStyle getRowStyle() {
  346. if(!isFormatted()) return null;
  347. StylesTable stylesSource = getSheet().getWorkbook().getStylesSource();
  348. if(stylesSource.getNumCellStyles() > 0) {
  349. return stylesSource.getStyleAt((int)_row.getS());
  350. } else {
  351. return null;
  352. }
  353. }
  354. /**
  355. * Applies a whole-row cell styling to the row.
  356. * If the value is null then the style information is removed,
  357. * causing the cell to used the default workbook style.
  358. */
  359. public void setRowStyle(CellStyle style) {
  360. if(style == null) {
  361. if(_row.isSetS()) {
  362. _row.unsetS();
  363. _row.unsetCustomFormat();
  364. }
  365. } else {
  366. StylesTable styleSource = getSheet().getWorkbook().getStylesSource();
  367. XSSFCellStyle xStyle = (XSSFCellStyle)style;
  368. xStyle.verifyBelongsToStylesSource(styleSource);
  369. long idx = styleSource.putStyle(xStyle);
  370. _row.setS(idx);
  371. _row.setCustomFormat(true);
  372. }
  373. }
  374. /**
  375. * Remove the Cell from this row.
  376. *
  377. * @param cell the cell to remove
  378. */
  379. public void removeCell(Cell cell) {
  380. if (cell.getRow() != this) {
  381. throw new IllegalArgumentException("Specified cell does not belong to this row");
  382. }
  383. XSSFCell xcell = (XSSFCell)cell;
  384. if(xcell.isPartOfArrayFormulaGroup()) {
  385. xcell.notifyArrayFormulaChanging();
  386. }
  387. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
  388. _sheet.getWorkbook().onDeleteFormula(xcell);
  389. }
  390. _cells.remove(cell.getColumnIndex());
  391. }
  392. /**
  393. * Returns the underlying CTRow xml bean containing all cell definitions in this row
  394. *
  395. * @return the underlying CTRow xml bean
  396. */
  397. @Internal
  398. public CTRow getCTRow(){
  399. return _row;
  400. }
  401. /**
  402. * Fired when the document is written to an output stream.
  403. *
  404. * @see org.apache.poi.xssf.usermodel.XSSFSheet#write(java.io.OutputStream) ()
  405. */
  406. @SuppressWarnings("deprecation")
  407. protected void onDocumentWrite(){
  408. // check if cells in the CTRow are ordered
  409. boolean isOrdered = true;
  410. CTCell[] cArray = _row.getCArray();
  411. if (cArray.length != _cells.size()) {
  412. isOrdered = false;
  413. } else {
  414. int i = 0;
  415. for (XSSFCell cell : _cells.values()) {
  416. CTCell c1 = cell.getCTCell();
  417. CTCell c2 = cArray[i++];
  418. String r1 = c1.getR();
  419. String r2 = c2.getR();
  420. if (!(r1==null ? r2==null : r1.equals(r2))){
  421. isOrdered = false;
  422. break;
  423. }
  424. }
  425. }
  426. if(!isOrdered){
  427. cArray = new CTCell[_cells.size()];
  428. int i = 0;
  429. for (XSSFCell xssfCell : _cells.values()) {
  430. cArray[i] = (CTCell) xssfCell.getCTCell().copy();
  431. // we have to copy and re-create the XSSFCell here because the
  432. // elements as otherwise setCArray below invalidates all the columns!
  433. // see Bug 56170, XMLBeans seems to always release previous objects
  434. // in the CArray, so we need to provide completely new ones here!
  435. //_cells.put(entry.getKey(), new XSSFCell(this, cArray[i]));
  436. xssfCell.setCTCell(cArray[i]);
  437. i++;
  438. }
  439. _row.setCArray(cArray);
  440. }
  441. }
  442. /**
  443. * @return formatted xml representation of this row
  444. */
  445. @Override
  446. public String toString(){
  447. return _row.toString();
  448. }
  449. /**
  450. * update cell references when shifting rows
  451. *
  452. * @param n the number of rows to move
  453. */
  454. protected void shift(int n) {
  455. int rownum = getRowNum() + n;
  456. CalculationChain calcChain = _sheet.getWorkbook().getCalculationChain();
  457. int sheetId = (int)_sheet.sheet.getSheetId();
  458. String msg = "Row[rownum="+getRowNum()+"] contains cell(s) included in a multi-cell array formula. " +
  459. "You cannot change part of an array.";
  460. for(Cell c : this){
  461. XSSFCell cell = (XSSFCell)c;
  462. if(cell.isPartOfArrayFormulaGroup()){
  463. cell.notifyArrayFormulaChanging(msg);
  464. }
  465. //remove the reference in the calculation chain
  466. if(calcChain != null) calcChain.removeItem(sheetId, cell.getReference());
  467. CTCell ctCell = cell.getCTCell();
  468. String r = new CellReference(rownum, cell.getColumnIndex()).formatAsString();
  469. ctCell.setR(r);
  470. }
  471. setRowNum(rownum);
  472. }
  473. /**
  474. * Copy the cells from srcRow to this row
  475. * If this row is not a blank row, this will merge the two rows, overwriting
  476. * the cells in this row with the cells in srcRow
  477. * If srcRow is null, overwrite cells in destination row with blank values, styles, etc per cell copy policy
  478. * srcRow may be from a different sheet in the same workbook
  479. * @param srcRow the rows to copy from
  480. * @param policy the policy to determine what gets copied
  481. */
  482. @Beta
  483. public void copyRowFrom(Row srcRow, CellCopyPolicy policy) {
  484. if (srcRow == null) {
  485. // srcRow is blank. Overwrite cells with blank values, blank styles, etc per cell copy policy
  486. for (Cell destCell : this) {
  487. final XSSFCell srcCell = null;
  488. // FIXME: remove type casting when copyCellFrom(Cell, CellCopyPolicy) is added to Cell interface
  489. ((XSSFCell)destCell).copyCellFrom(srcCell, policy);
  490. }
  491. if (policy.isCopyMergedRegions()) {
  492. // Remove MergedRegions in dest row
  493. final int destRowNum = getRowNum();
  494. int index = 0;
  495. final Set<Integer> indices = new HashSet<Integer>();
  496. for (CellRangeAddress destRegion : getSheet().getMergedRegions()) {
  497. if (destRowNum == destRegion.getFirstRow() && destRowNum == destRegion.getLastRow()) {
  498. indices.add(index);
  499. }
  500. index++;
  501. }
  502. getSheet().removeMergedRegions(indices);
  503. }
  504. if (policy.isCopyRowHeight()) {
  505. // clear row height
  506. setHeight((short)-1);
  507. }
  508. }
  509. else {
  510. for (final Cell c : srcRow){
  511. final XSSFCell srcCell = (XSSFCell)c;
  512. final XSSFCell destCell = createCell(srcCell.getColumnIndex(), srcCell.getCellType());
  513. destCell.copyCellFrom(srcCell, policy);
  514. }
  515. final XSSFRowShifter rowShifter = new XSSFRowShifter(_sheet);
  516. final int sheetIndex = _sheet.getWorkbook().getSheetIndex(_sheet);
  517. final String sheetName = _sheet.getWorkbook().getSheetName(sheetIndex);
  518. final int srcRowNum = srcRow.getRowNum();
  519. final int destRowNum = getRowNum();
  520. final int rowDifference = destRowNum - srcRowNum;
  521. final FormulaShifter shifter = FormulaShifter.createForRowCopy(sheetIndex, sheetName, srcRowNum, srcRowNum, rowDifference, SpreadsheetVersion.EXCEL2007);
  522. rowShifter.updateRowFormulas(this, shifter);
  523. // Copy merged regions that are fully contained on the row
  524. // FIXME: is this something that rowShifter could be doing?
  525. if (policy.isCopyMergedRegions()) {
  526. for (CellRangeAddress srcRegion : srcRow.getSheet().getMergedRegions()) {
  527. if (srcRowNum == srcRegion.getFirstRow() && srcRowNum == srcRegion.getLastRow()) {
  528. CellRangeAddress destRegion = srcRegion.copy();
  529. destRegion.setFirstRow(destRowNum);
  530. destRegion.setLastRow(destRowNum);
  531. getSheet().addMergedRegion(destRegion);
  532. }
  533. }
  534. }
  535. if (policy.isCopyRowHeight()) {
  536. setHeight(srcRow.getHeight());
  537. }
  538. }
  539. }
  540. public int getOutlineLevel() {
  541. return _row.getOutlineLevel();
  542. }
  543. }