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.

HSSFRow.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Apache" and "Apache Software Foundation" and
  27. * "Apache POI" must not be used to endorse or promote products
  28. * derived from this software without prior written permission. For
  29. * written permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * "Apache POI", nor may "Apache" appear in their name, without
  33. * prior written permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. /*
  55. * HSSFRow.java
  56. *
  57. * Created on September 30, 2001, 3:44 PM
  58. */
  59. package org.apache.poi.hssf.usermodel;
  60. import org.apache.poi.hssf.model.Sheet;
  61. import org.apache.poi.hssf.model.Workbook;
  62. import org.apache.poi.hssf.record.CellValueRecordInterface;
  63. import org.apache.poi.hssf.record.RowRecord;
  64. import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate;
  65. import java.util.HashMap;
  66. import java.util.Iterator;
  67. /**
  68. * High level representation of a row of a spreadsheet.
  69. *
  70. * Only rows that have cells should be added to a Sheet.
  71. * @version 1.0-pre
  72. * @author Andrew C. Oliver (acoliver at apache dot org)
  73. * @author Glen Stampoultzis (glens at apache.org)
  74. */
  75. public class HSSFRow
  76. implements Comparable
  77. {
  78. // used for collections
  79. public final static int INITIAL_CAPACITY = 5;
  80. //private short rowNum;
  81. private int rowNum;
  82. //private ValueRecordsAggregate cells;
  83. // private short firstcell = -1;
  84. // private short lastcell = -1;
  85. /**
  86. * reference to low level representation
  87. */
  88. //private RowRecord row;
  89. /**
  90. * reference to containing low level Workbook
  91. */
  92. private Workbook book;
  93. /**
  94. * reference to containing Sheet
  95. */
  96. private Sheet sheet;
  97. protected HSSFRow()
  98. {
  99. }
  100. /**
  101. * Creates new HSSFRow from scratch. Only HSSFSheet should do this.
  102. *
  103. * @param book low-level Workbook object containing the sheet that contains this row
  104. * @param sheet low-level Sheet object that contains this Row
  105. * @param rowNum the row number of this row (0 based)
  106. * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(short)
  107. */
  108. //protected HSSFRow(Workbook book, Sheet sheet, short rowNum)
  109. protected HSSFRow(Workbook book, Sheet sheet, int rowNum)
  110. {
  111. this.rowNum = rowNum;
  112. //cells = new HashMap(10); // new ArrayList(INITIAL_CAPACITY);
  113. this.book = book;
  114. this.sheet = sheet;
  115. //row = new RowRecord();
  116. //row.setHeight((short) 0xff);
  117. //row.setLastCol((short) -1);
  118. //row.setFirstCol((short) -1);
  119. // row.setRowNumber(rowNum);
  120. //setRowNum(rowNum);
  121. }
  122. /**
  123. * Creates an HSSFRow from a low level RowRecord object. Only HSSFSheet should do
  124. * this. HSSFSheet uses this when an existing file is read in.
  125. *
  126. * @param book low-level Workbook object containing the sheet that contains this row
  127. * @param sheet low-level Sheet object that contains this Row
  128. * @param record the low level api object this row should represent
  129. * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(short)
  130. */
  131. protected HSSFRow(Workbook book, Sheet sheet, RowRecord record)
  132. {
  133. //this.rowNum = rowNum;
  134. //cells = new HashMap(); // ArrayList(INITIAL_CAPACITY);
  135. this.book = book;
  136. this.sheet = sheet;
  137. //row = record;
  138. // row.setHeight(record.getHeight());
  139. // row.setRowNumber(rowNum);
  140. setRowNum(record.getRowNumber());
  141. // addColumns(book, sheet, record);
  142. }
  143. /**
  144. * Use this to create new cells within the row and return it.
  145. * <p>
  146. * The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
  147. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
  148. *
  149. * @param column - the column number this cell represents
  150. *
  151. * @return HSSFCell a high level representation of the created cell.
  152. */
  153. public HSSFCell createCell(short column)
  154. {
  155. HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column);
  156. //addCell(cell);
  157. //sheet.addValueRecord(getRowNum(), cell.getCellValueRecord());
  158. return cell;
  159. }
  160. /**
  161. * Use this to create new cells within the row and return it.
  162. * <p>
  163. * The cell that is returned is a CELL_TYPE_BLANK. The type can be changed
  164. * either through calling setCellValue or setCellType.
  165. *
  166. * @param column - the column number this cell represents
  167. *
  168. * @return HSSFCell a high level representation of the created cell.
  169. * @deprecated As of 22-Jan-2002 use createCell(short) and use setCellValue to
  170. * specify the type lazily.
  171. */
  172. public HSSFCell createCell(short column, int type)
  173. {
  174. HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), column, type);
  175. addCell(cell);
  176. sheet.addValueRecord(getRowNum(), cell.getCellValueRecord());
  177. return cell;
  178. }
  179. /**
  180. * remove the HSSFCell from this row.
  181. * @param cell to remove
  182. */
  183. public void removeCell(HSSFCell cell)
  184. {
  185. CellValueRecordInterface cval = cell.getCellValueRecord();
  186. sheet.removeValueRecord(getRowNum(), cval);
  187. //cells.remove(new Integer(cell.getCellNum()));
  188. if (cell.getCellNum() == getLastCol(rowNum))
  189. {
  190. sheet.getRow(rowNum).setLastCol(findLastCell(sheet.getRow(rowNum).getLastCol()));
  191. }
  192. if (cell.getCellNum() == getFirstCol(rowNum))
  193. {
  194. setFirstCol(findFirstCell(getFirstCol(rowNum)));
  195. }
  196. }
  197. /**
  198. * create a high level HSSFCell object from an existing low level record. Should
  199. * only be called from HSSFSheet or HSSFRow itself.
  200. * @param cell low level cell to create the high level representation from
  201. * @return HSSFCell representing the low level record passed in
  202. */
  203. protected HSSFCell createCellFromRecord(CellValueRecordInterface cell)
  204. {
  205. HSSFCell hcell = new HSSFCell(book, sheet, getRowNum(), cell);
  206. addCell(hcell);
  207. sheet.addValueRecord(getRowNum(),cell);
  208. return hcell;
  209. }
  210. /**
  211. * set the row number of this row.
  212. * @param rowNum the row number (0-based)
  213. */
  214. //public void setRowNum(short rowNum)
  215. public void setRowNum(int rowNum)
  216. {
  217. this.rowNum = rowNum;
  218. //if (row != null)
  219. //{
  220. // row.setRowNumber(rowNum); // used only for KEY comparison (HSSFRow)
  221. //}
  222. }
  223. /**
  224. * get row number this row represents
  225. * @return the row number (0 based)
  226. */
  227. //public short getRowNum()
  228. public int getRowNum()
  229. {
  230. return rowNum;
  231. }
  232. /**
  233. * used internally to add a cell.
  234. */
  235. private void addCell(HSSFCell cell)
  236. {
  237. if (getFirstCol(rowNum) == -1)
  238. {
  239. setFirstCol(cell.getCellNum());
  240. }
  241. if (getLastCol(rowNum) == -1)
  242. {
  243. setLastCol(cell.getCellNum());
  244. }
  245. //cells.put(new Integer(cell.getCellNum()), cell);
  246. sheet.addValueRecord(this.rowNum, cell.getCellValueRecord());
  247. if (cell.getCellNum() < getFirstCol(rowNum))
  248. {
  249. setFirstCol(cell.getCellNum());
  250. }
  251. if (cell.getCellNum() > getLastCol(rowNum))
  252. {
  253. setLastCol(cell.getCellNum());
  254. }
  255. }
  256. private void setLastCol(short cell) {
  257. sheet.setLastColForRow(rowNum, cell);
  258. }
  259. private void setFirstCol(short cell) {
  260. sheet.setFirstColForRow(rowNum, cell);
  261. }
  262. private short getLastCol(int row) {
  263. return sheet.getLastColForRow(row);
  264. }
  265. private short getFirstCol(int row) {
  266. return sheet.getFirstColForRow(row);
  267. }
  268. /**
  269. * get the hssfcell representing a given column (logical cell) 0-based. If you
  270. * ask for a cell that is not defined....you get a null.
  271. *
  272. * @param cellnum 0 based column number
  273. * @return HSSFCell representing that column or null if undefined.
  274. */
  275. public HSSFCell getCell(short cellnum)
  276. {
  277. HSSFCell retval = null;
  278. CellValueRecordInterface cval = sheet.getValueRecord(rowNum, cellnum);
  279. if (cval != null) {
  280. retval = new HSSFCell(book, sheet, rowNum, cval);
  281. }
  282. /* for (int k = 0; k < cells.size(); k++)
  283. {
  284. HSSFCell cell = ( HSSFCell ) cells.get(k);
  285. if (cell.getCellNum() == cellnum)
  286. {
  287. return cell;
  288. }
  289. }*/
  290. return retval;
  291. }
  292. /**
  293. * get the number of the first cell contained in this row.
  294. * @return short representing the first logical cell in the row
  295. */
  296. public short getFirstCellNum()
  297. {
  298. if (getPhysicalNumberOfCells() == 0)
  299. return -1;
  300. else
  301. return getFirstCol(rowNum);
  302. }
  303. /**
  304. * get the number of the last cell contained in this row.
  305. * @return short representing the last logical cell in the row
  306. */
  307. public short getLastCellNum()
  308. {
  309. if (getPhysicalNumberOfCells() == 0)
  310. return -1;
  311. else
  312. return getLastCol(rowNum);
  313. }
  314. /**
  315. * gets the number of defined cells (NOT number of cells in the actual row!).
  316. * That is to say if only columns 0,4,5 have values then there would be 3.
  317. * @return int representing the number of defined cells in the row.
  318. */
  319. public int getPhysicalNumberOfCells()
  320. {
  321. // sheet.get
  322. // if (cells == null)
  323. // {
  324. // return 0; // shouldn't be possible but it is due to missing API support for BLANK/MULBLANK
  325. // }
  326. // return cells.size();
  327. return sheet.getPhysicalNumberOfRows();
  328. }
  329. /**
  330. * set the row's height or set to ff (-1) for undefined/default-height. Set the height in "twips" or
  331. * 1/20th of a point.
  332. * @param height rowheight or 0xff for undefined (use sheet default)
  333. */
  334. public void setHeight(short height)
  335. {
  336. // row.setOptionFlags(
  337. sheet.getRow(rowNum).setBadFontHeight(true);
  338. sheet.getRow(rowNum).setHeight(height);
  339. }
  340. /**
  341. * set the row's height in points.
  342. * @param height row height in points
  343. */
  344. public void setHeightInPoints(float height)
  345. {
  346. // row.setOptionFlags(
  347. sheet.getRow(rowNum).setBadFontHeight(true);
  348. sheet.getRow(rowNum).setHeight((short) (height * 20));
  349. }
  350. /**
  351. * get the row's height or ff (-1) for undefined/default-height in twips (1/20th of a point)
  352. * @return rowheight or 0xff for undefined (use sheet default)
  353. */
  354. public short getHeight()
  355. {
  356. return sheet.getRow(rowNum).getHeight();
  357. }
  358. /**
  359. * get the row's height or ff (-1) for undefined/default-height in points (20*getHeight())
  360. * @return rowheight or 0xff for undefined (use sheet default)
  361. */
  362. public float getHeightInPoints()
  363. {
  364. return (sheet.getRow(rowNum).getHeight() / 20);
  365. }
  366. /**
  367. * get the lowlevel RowRecord represented by this object - should only be called
  368. * by other parts of the high level API
  369. *
  370. * @return RowRecord this row represents
  371. */
  372. protected RowRecord getRowRecord()
  373. {
  374. return sheet.getRow(rowNum);
  375. }
  376. /**
  377. * used internally to refresh the "last cell" when the last cell is removed.
  378. */
  379. private short findLastCell(short lastcell)
  380. {
  381. short cellnum = (short) (lastcell - 1);
  382. HSSFCell r = getCell(cellnum);
  383. while (r == null && cellnum >= 0)
  384. {
  385. r = getCell(--cellnum);
  386. }
  387. return cellnum;
  388. }
  389. /**
  390. * used internally to refresh the "first cell" when the first cell is removed.
  391. */
  392. private short findFirstCell(short firstcell)
  393. {
  394. short cellnum = (short) (firstcell + 1);
  395. HSSFCell r = getCell(cellnum);
  396. while (r == null && cellnum <= getLastCellNum())
  397. {
  398. r = getCell(++cellnum);
  399. }
  400. if (cellnum > getLastCellNum())
  401. return -1;
  402. return cellnum;
  403. }
  404. /**
  405. * @return cell iterator of the physically defined cells. Note element 4 may
  406. * actually be row cell depending on how many are defined!
  407. */
  408. public Iterator cellIterator()
  409. {
  410. return new RowCellIterator(this.book, this.sheet, this.rowNum);
  411. }
  412. public int compareTo(Object obj)
  413. {
  414. HSSFRow loc = (HSSFRow) obj;
  415. if (this.getRowNum() == loc.getRowNum())
  416. {
  417. return 0;
  418. }
  419. if (this.getRowNum() < loc.getRowNum())
  420. {
  421. return -1;
  422. }
  423. if (this.getRowNum() > loc.getRowNum())
  424. {
  425. return 1;
  426. }
  427. return -1;
  428. }
  429. public boolean equals(Object obj)
  430. {
  431. if (!(obj instanceof HSSFRow))
  432. {
  433. return false;
  434. }
  435. HSSFRow loc = (HSSFRow) obj;
  436. if (this.getRowNum() == loc.getRowNum())
  437. {
  438. return true;
  439. }
  440. return false;
  441. }
  442. }
  443. class RowCellIterator implements Iterator {
  444. Iterator cells;
  445. Workbook book;
  446. Sheet sheet;
  447. int row;
  448. public RowCellIterator(Workbook book, Sheet sheet, int row) {
  449. this.sheet = sheet;
  450. this.book = book;
  451. this.row = row;
  452. cells = this.sheet.rowCellIterator(row);
  453. }
  454. public boolean hasNext() {
  455. return cells.hasNext();
  456. }
  457. public Object next() {
  458. HSSFCell retval = null;
  459. if (cells.hasNext()) {
  460. retval = new HSSFCell(book, sheet, row, ((CellValueRecordInterface)cells.next()));
  461. }
  462. return retval;
  463. }
  464. public void remove() {
  465. cells.remove();
  466. }
  467. }