Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Cell.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.usermodel;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. import org.apache.poi.ss.formula.FormulaParseException;
  19. import org.apache.poi.ss.util.CellAddress;
  20. import org.apache.poi.ss.util.CellRangeAddress;
  21. import org.apache.poi.util.Internal;
  22. import org.apache.poi.util.Removal;
  23. /**
  24. * High level representation of a cell in a row of a spreadsheet.
  25. * <p>
  26. * Cells can be numeric, formula-based or string-based (text). The cell type
  27. * specifies this. String cells cannot conatin numbers and numeric cells cannot
  28. * contain strings (at least according to our model). Client apps should do the
  29. * conversions themselves. Formula cells have the formula string, as well as
  30. * the formula result, which can be numeric or string.
  31. * </p>
  32. * <p>
  33. * Cells should have their number (0 based) before being added to a row.
  34. * </p>
  35. */
  36. public interface Cell {
  37. /**
  38. * Numeric Cell type (0)
  39. * @see #setCellType(int)
  40. * @see #getCellType()
  41. * @deprecated POI 3.15 beta 3. Use {@link CellType#NUMERIC} instead.
  42. */
  43. @Removal(version="4.0")
  44. int CELL_TYPE_NUMERIC = 0; //CellType.NUMERIC.getCode();
  45. /**
  46. * String Cell type (1)
  47. * @see #setCellType(int)
  48. * @see #getCellType()
  49. * @deprecated POI 3.15 beta 3. Use {@link CellType#STRING} instead.
  50. */
  51. @Removal(version="4.0")
  52. int CELL_TYPE_STRING = 1; //CellType.STRING.getCode();
  53. /**
  54. * Formula Cell type (2)
  55. * @see #setCellType(int)
  56. * @see #getCellType()
  57. * @deprecated POI 3.15 beta 3. Use {@link CellType#FORMULA} instead.
  58. */
  59. @Removal(version="4.0")
  60. int CELL_TYPE_FORMULA = 2; //CellType.FORMULA.getCode();
  61. /**
  62. * Blank Cell type (3)
  63. * @see #setCellType(int)
  64. * @see #getCellType()
  65. * @deprecated POI 3.15 beta 3. Use {@link CellType#BLANK} instead.
  66. */
  67. @Removal(version="4.0")
  68. int CELL_TYPE_BLANK = 3; //CellType.BLANK.getCode();
  69. /**
  70. * Boolean Cell type (4)
  71. * @see #setCellType(int)
  72. * @see #getCellType()
  73. * @deprecated POI 3.15 beta 3. Use {@link CellType#BOOLEAN} instead.
  74. */
  75. @Removal(version="4.0")
  76. int CELL_TYPE_BOOLEAN = 4; //CellType.BOOLEAN.getCode();
  77. /**
  78. * Error Cell type (5)
  79. * @see #setCellType(int)
  80. * @see #getCellType()
  81. * @deprecated POI 3.15 beta 3. Use {@link CellType#ERROR} instead.
  82. */
  83. @Removal(version="4.0")
  84. int CELL_TYPE_ERROR = 5; //CellType.ERROR.getCode();
  85. /**
  86. * Returns column index of this cell
  87. *
  88. * @return zero-based column index of a column in a sheet.
  89. */
  90. int getColumnIndex();
  91. /**
  92. * Returns row index of a row in the sheet that contains this cell
  93. *
  94. * @return zero-based row index of a row in the sheet that contains this cell
  95. */
  96. int getRowIndex();
  97. /**
  98. * Returns the sheet this cell belongs to
  99. *
  100. * @return the sheet this cell belongs to
  101. */
  102. Sheet getSheet();
  103. /**
  104. * Returns the Row this cell belongs to
  105. *
  106. * @return the Row that owns this cell
  107. */
  108. Row getRow();
  109. /**
  110. * Set the cells type (numeric, formula or string).
  111. * <p>If the cell currently contains a value, the value will
  112. * be converted to match the new type, if possible. Formatting
  113. * is generally lost in the process however.</p>
  114. * <p>If what you want to do is get a String value for your
  115. * numeric cell, <i>stop!</i>. This is not the way to do it.
  116. * Instead, for fetching the string value of a numeric or boolean
  117. * or date cell, use {@link DataFormatter} instead.</p>
  118. *
  119. * @throws IllegalArgumentException if the specified cell type is invalid
  120. * @throws IllegalStateException if the current value cannot be converted to the new type
  121. * @see CellType#NUMERIC
  122. * @see CellType#STRING
  123. * @see CellType#FORMULA
  124. * @see CellType#BLANK
  125. * @see CellType#BOOLEAN
  126. * @see CellType#ERROR
  127. * @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
  128. */
  129. @Removal(version="4.0")
  130. void setCellType(int cellType);
  131. /**
  132. * Set the cells type (numeric, formula or string).
  133. * <p>If the cell currently contains a value, the value will
  134. * be converted to match the new type, if possible. Formatting
  135. * is generally lost in the process however.</p>
  136. * <p>If what you want to do is get a String value for your
  137. * numeric cell, <i>stop!</i>. This is not the way to do it.
  138. * Instead, for fetching the string value of a numeric or boolean
  139. * or date cell, use {@link DataFormatter} instead.</p>
  140. *
  141. * @throws IllegalArgumentException if the specified cell type is invalid
  142. * @throws IllegalStateException if the current value cannot be converted to the new type
  143. */
  144. void setCellType(CellType cellType);
  145. /**
  146. * Return the cell type.
  147. *
  148. * Will return {@link CellType} in version 4.0 of POI.
  149. * For forwards compatibility, do not hard-code cell type literals in your code.
  150. *
  151. * @return the cell type
  152. * @deprecated POI 3.15. Will return a {@link CellType} enum in the future.
  153. */
  154. int getCellType();
  155. /**
  156. * Return the cell type.
  157. *
  158. * @return the cell type
  159. * @since POI 3.15 beta 3
  160. * @deprecated POI 3.15 beta 3
  161. * Will be renamed to <code>getCellType()</code> when we make the CellType enum transition in POI 4.0. See bug 59791.
  162. */
  163. @Removal(version="4.2")
  164. CellType getCellTypeEnum();
  165. /**
  166. * Only valid for formula cells
  167. *
  168. * Will return {@link CellType} in a future version of POI.
  169. * For forwards compatibility, do not hard-code cell type literals in your code.
  170. *
  171. * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
  172. * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
  173. * on the cached value of the formula
  174. * @deprecated 3.15. Will return a {@link CellType} enum in the future.
  175. */
  176. int getCachedFormulaResultType();
  177. /**
  178. * Only valid for formula cells
  179. * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
  180. * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
  181. * on the cached value of the formula
  182. * @since POI 3.15 beta 3
  183. * @deprecated POI 3.15 beta 3
  184. * Will be renamed to <code>getCachedFormulaResultType()</code> when we make the CellType enum transition in POI 4.0. See bug 59791.
  185. */
  186. CellType getCachedFormulaResultTypeEnum();
  187. /**
  188. * Set a numeric value for the cell
  189. *
  190. * @param value the numeric value to set this cell to. For formulas we'll set the
  191. * precalculated value, for numerics we'll set its value. For other types we
  192. * will change the cell to a numeric cell and set its value.
  193. */
  194. void setCellValue(double value);
  195. /**
  196. * <p>Converts the supplied date to its equivalent Excel numeric value and sets
  197. * that into the cell.</p>
  198. *
  199. * <p><b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
  200. * cases (when entering date values), Excel automatically adjusts the
  201. * <i>cell style</i> to some date format, creating the illusion that the cell
  202. * data type is now something besides {@link CellType#NUMERIC}. POI
  203. * does not attempt to replicate this behaviour. To make a numeric cell
  204. * display as a date, use {@link #setCellStyle(CellStyle)} etc.</p>
  205. *
  206. * @param value the numeric value to set this cell to. For formulas we'll set the
  207. * precalculated value, for numerics we'll set its value. For other types we
  208. * will change the cell to a numerics cell and set its value.
  209. */
  210. void setCellValue(Date value);
  211. /**
  212. * <p>Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
  213. * a date.</p>
  214. * <p>
  215. * This will set the cell value based on the Calendar's timezone. As Excel
  216. * does not support timezones this means that both 20:00+03:00 and
  217. * 20:00-03:00 will be reported as the same value (20:00) even that there
  218. * are 6 hours difference between the two times. This difference can be
  219. * preserved by using <code>setCellValue(value.getTime())</code> which will
  220. * automatically shift the times to the default timezone.
  221. * </p>
  222. *
  223. * @param value the date value to set this cell to. For formulas we'll set the
  224. * precalculated value, for numerics we'll set its value. For othertypes we
  225. * will change the cell to a numeric cell and set its value.
  226. */
  227. void setCellValue(Calendar value);
  228. /**
  229. * Set a rich string value for the cell.
  230. *
  231. * @param value value to set the cell to. For formulas we'll set the formula
  232. * string, for String cells we'll set its value. For other types we will
  233. * change the cell to a string cell and set its value.
  234. * If value is null then we will change the cell to a Blank cell.
  235. */
  236. void setCellValue(RichTextString value);
  237. /**
  238. * Set a string value for the cell.
  239. *
  240. * @param value value to set the cell to. For formulas we'll set the formula
  241. * string, for String cells we'll set its value. For other types we will
  242. * change the cell to a string cell and set its value.
  243. * If value is null then we will change the cell to a Blank cell.
  244. */
  245. void setCellValue(String value);
  246. /**
  247. * Sets formula for this cell.
  248. * <p>
  249. * Note, this method only sets the formula string and does not calculate the formula value.
  250. * To set the precalculated value use {@link #setCellValue(double)} or {@link #setCellValue(String)}
  251. * </p>
  252. *
  253. * @param formula the formula to set, e.g. <code>"SUM(C4:E4)"</code>.
  254. * If the argument is <code>null</code> then the current formula is removed.
  255. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  256. */
  257. void setCellFormula(String formula) throws FormulaParseException;
  258. /**
  259. * Return a formula for the cell, for example, <code>SUM(C4:E4)</code>
  260. *
  261. * @return a formula for the cell
  262. * @throws IllegalStateException if the cell type returned by {@link #getCellTypeEnum()} is not {@link CellType#FORMULA}
  263. */
  264. String getCellFormula();
  265. /**
  266. * Get the value of the cell as a number.
  267. * <p>
  268. * For strings we throw an exception. For blank cells we return a 0.
  269. * For formulas or error cells we return the precalculated value;
  270. * </p>
  271. * @return the value of the cell as a number
  272. * @throws IllegalStateException if the cell type returned by {@link #getCellTypeEnum()} is {@link CellType#STRING}
  273. * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
  274. * @see DataFormatter for turning this number into a string similar to that which Excel would render this number as.
  275. */
  276. double getNumericCellValue();
  277. /**
  278. * Get the value of the cell as a date.
  279. * <p>
  280. * For strings we throw an exception. For blank cells we return a null.
  281. * </p>
  282. * @return the value of the cell as a date
  283. * @throws IllegalStateException if the cell type returned by {@link #getCellTypeEnum()} is {@link CellType#STRING}
  284. * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
  285. * @see DataFormatter for formatting this date into a string similar to how excel does.
  286. */
  287. Date getDateCellValue();
  288. /**
  289. * Get the value of the cell as a XSSFRichTextString
  290. * <p>
  291. * For numeric cells we throw an exception. For blank cells we return an empty string.
  292. * For formula cells we return the pre-calculated value if a string, otherwise an exception.
  293. * </p>
  294. * @return the value of the cell as a XSSFRichTextString
  295. */
  296. RichTextString getRichStringCellValue();
  297. /**
  298. * Get the value of the cell as a string
  299. * <p>
  300. * For numeric cells we throw an exception. For blank cells we return an empty string.
  301. * For formulaCells that are not string Formulas, we throw an exception.
  302. * </p>
  303. * @return the value of the cell as a string
  304. */
  305. String getStringCellValue();
  306. /**
  307. * Set a boolean value for the cell
  308. *
  309. * @param value the boolean value to set this cell to. For formulas we'll set the
  310. * precalculated value, for booleans we'll set its value. For other types we
  311. * will change the cell to a boolean cell and set its value.
  312. */
  313. void setCellValue(boolean value);
  314. /**
  315. * Set a error value for the cell
  316. *
  317. * @param value the error value to set this cell to. For formulas we'll set the
  318. * precalculated value , for errors we'll set
  319. * its value. For other types we will change the cell to an error
  320. * cell and set its value.
  321. * @see FormulaError
  322. */
  323. void setCellErrorValue(byte value);
  324. /**
  325. * Get the value of the cell as a boolean.
  326. * <p>
  327. * For strings, numbers, and errors, we throw an exception. For blank cells we return a false.
  328. * </p>
  329. * @return the value of the cell as a boolean
  330. * @throws IllegalStateException if the cell type returned by {@link #getCellTypeEnum()}
  331. * is not {@link CellType#BOOLEAN}, {@link CellType#BLANK} or {@link CellType#FORMULA}
  332. */
  333. boolean getBooleanCellValue();
  334. /**
  335. * Get the value of the cell as an error code.
  336. * <p>
  337. * For strings, numbers, and booleans, we throw an exception.
  338. * For blank cells we return a 0.
  339. * </p>
  340. *
  341. * @return the value of the cell as an error code
  342. * @throws IllegalStateException if the cell type returned by {@link #getCellTypeEnum()} isn't {@link CellType#ERROR}
  343. * @see FormulaError for error codes
  344. */
  345. byte getErrorCellValue();
  346. /**
  347. * <p>Set the style for the cell. The style should be an CellStyle created/retrieved from
  348. * the Workbook.</p>
  349. *
  350. * <p>To change the style of a cell without affecting other cells that use the same style,
  351. * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}</p>
  352. *
  353. * @param style reference contained in the workbook.
  354. * If the value is null then the style information is removed causing the cell to used the default workbook style.
  355. * @see org.apache.poi.ss.usermodel.Workbook#createCellStyle()
  356. */
  357. void setCellStyle(CellStyle style);
  358. /**
  359. * Return the cell's style.
  360. *
  361. * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as
  362. * <code>workbook.getCellStyleAt(0)</code>
  363. * @see Workbook#getCellStyleAt(int)
  364. */
  365. CellStyle getCellStyle();
  366. /**
  367. * Sets this cell as the active cell for the worksheet
  368. */
  369. void setAsActiveCell();
  370. /**
  371. * Gets the address of this cell
  372. *
  373. * @return <code>A1</code> style address of this cell
  374. * @since 3.14beta1
  375. */
  376. CellAddress getAddress();
  377. /**
  378. * Assign a comment to this cell
  379. *
  380. * @param comment comment associated with this cell
  381. */
  382. void setCellComment(Comment comment);
  383. /**
  384. * Returns comment associated with this cell
  385. *
  386. * @return comment associated with this cell or <code>null</code> if not found
  387. */
  388. Comment getCellComment();
  389. /**
  390. * Removes the comment for this cell, if there is one.
  391. */
  392. void removeCellComment();
  393. /**
  394. * @return hyperlink associated with this cell or <code>null</code> if not found
  395. */
  396. Hyperlink getHyperlink();
  397. /**
  398. * Assign a hyperlink to this cell
  399. *
  400. * @param link hyperlink associated with this cell
  401. */
  402. void setHyperlink(Hyperlink link);
  403. /**
  404. * Removes the hyperlink for this cell, if there is one.
  405. */
  406. void removeHyperlink();
  407. /**
  408. * Only valid for array formula cells
  409. *
  410. * @return range of the array formula group that the cell belongs to.
  411. */
  412. CellRangeAddress getArrayFormulaRange();
  413. /**
  414. * @return <code>true</code> if this cell is part of group of cells having a common array formula.
  415. */
  416. boolean isPartOfArrayFormulaGroup();
  417. }