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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. /**
  19. * High level representation of a cell in a row of a spreadsheet.
  20. * <p>
  21. * Cells can be numeric, formula-based or string-based (text). The cell type
  22. * specifies this. String cells cannot conatin numbers and numeric cells cannot
  23. * contain strings (at least according to our model). Client apps should do the
  24. * conversions themselves. Formula cells have the formula string, as well as
  25. * the formula result, which can be numeric or string.
  26. * </p>
  27. * <p>
  28. * Cells should have their number (0 based) before being added to a row.
  29. * </p>
  30. */
  31. public interface Cell {
  32. /**
  33. * Numeric Cell type (0)
  34. * @see #setCellType(int)
  35. * @see #getCellType()
  36. */
  37. public final static int CELL_TYPE_NUMERIC = 0;
  38. /**
  39. * String Cell type (1)
  40. * @see #setCellType(int)
  41. * @see #getCellType()
  42. */
  43. public final static int CELL_TYPE_STRING = 1;
  44. /**
  45. * Formula Cell type (2)
  46. * @see #setCellType(int)
  47. * @see #getCellType()
  48. */
  49. public final static int CELL_TYPE_FORMULA = 2;
  50. /**
  51. * Blank Cell type (3)
  52. * @see #setCellType(int)
  53. * @see #getCellType()
  54. */
  55. public final static int CELL_TYPE_BLANK = 3;
  56. /**
  57. * Boolean Cell type (4)
  58. * @see #setCellType(int)
  59. * @see #getCellType()
  60. */
  61. public final static int CELL_TYPE_BOOLEAN = 4;
  62. /**
  63. * Error Cell type (5)
  64. * @see #setCellType(int)
  65. * @see #getCellType()
  66. */
  67. public final static int CELL_TYPE_ERROR = 5;
  68. /**
  69. * Returns column index of this cell
  70. *
  71. * @return zero-based column index of a column in a sheet.
  72. */
  73. int getColumnIndex();
  74. /**
  75. * Returns row index of a row in the sheet that contains this cell
  76. *
  77. * @return zero-based row index of a row in the sheet that contains this cell
  78. */
  79. int getRowIndex();
  80. /**
  81. * Returns the sheet this cell belongs to
  82. *
  83. * @return the sheet this cell belongs to
  84. */
  85. Sheet getSheet();
  86. /**
  87. * Returns the Row this cell belongs to
  88. *
  89. * @return the Row that owns this cell
  90. */
  91. Row getRow();
  92. /**
  93. * Set the cells type (numeric, formula or string)
  94. *
  95. * @throws IllegalArgumentException if the specified cell type is invalid
  96. * @see #CELL_TYPE_NUMERIC
  97. * @see #CELL_TYPE_STRING
  98. * @see #CELL_TYPE_FORMULA
  99. * @see #CELL_TYPE_BLANK
  100. * @see #CELL_TYPE_BOOLEAN
  101. * @see #CELL_TYPE_ERROR
  102. */
  103. void setCellType(int cellType);
  104. /**
  105. * Return the cell type.
  106. *
  107. * @return the cell type
  108. * @see Cell#CELL_TYPE_BLANK
  109. * @see Cell#CELL_TYPE_NUMERIC
  110. * @see Cell#CELL_TYPE_STRING
  111. * @see Cell#CELL_TYPE_FORMULA
  112. * @see Cell#CELL_TYPE_BOOLEAN
  113. * @see Cell#CELL_TYPE_ERROR
  114. */
  115. int getCellType();
  116. /**
  117. * Only valid for formula cells
  118. * @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
  119. * {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
  120. * on the cached value of the formula
  121. */
  122. int getCachedFormulaResultType();
  123. /**
  124. * Set a numeric value for the cell
  125. *
  126. * @param value the numeric value to set this cell to. For formulas we'll set the
  127. * precalculated value, for numerics we'll set its value. For other types we
  128. * will change the cell to a numeric cell and set its value.
  129. */
  130. void setCellValue(double value);
  131. /**
  132. * Converts the supplied date to its equivalent Excel numeric value and sets
  133. * that into the cell.
  134. * <p/>
  135. * <b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
  136. * cases (when entering date values), Excel automatically adjusts the
  137. * <i>cell style</i> to some date format, creating the illusion that the cell
  138. * data type is now something besides {@link Cell#CELL_TYPE_NUMERIC}. POI
  139. * does not attempt to replicate this behaviour. To make a numeric cell
  140. * display as a date, use {@link #setCellStyle(CellStyle)} etc.
  141. *
  142. * @param value the numeric value to set this cell to. For formulas we'll set the
  143. * precalculated value, for numerics we'll set its value. For other types we
  144. * will change the cell to a numerics cell and set its value.
  145. */
  146. void setCellValue(Date value);
  147. /**
  148. * Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
  149. * a date.
  150. * <p>
  151. * This will set the cell value based on the Calendar's timezone. As Excel
  152. * does not support timezones this means that both 20:00+03:00 and
  153. * 20:00-03:00 will be reported as the same value (20:00) even that there
  154. * are 6 hours difference between the two times. This difference can be
  155. * preserved by using <code>setCellValue(value.getTime())</code> which will
  156. * automatically shift the times to the default timezone.
  157. * </p>
  158. *
  159. * @param value the date value to set this cell to. For formulas we'll set the
  160. * precalculated value, for numerics we'll set its value. For othertypes we
  161. * will change the cell to a numeric cell and set its value.
  162. */
  163. void setCellValue(Calendar value);
  164. /**
  165. * Set a rich string value for the cell.
  166. *
  167. * @param value value to set the cell to. For formulas we'll set the formula
  168. * string, for String cells we'll set its value. For other types we will
  169. * change the cell to a string cell and set its value.
  170. * If value is null then we will change the cell to a Blank cell.
  171. */
  172. void setCellValue(RichTextString value);
  173. /**
  174. * Set a string value for the cell.
  175. *
  176. * @param value value to set the cell to. For formulas we'll set the formula
  177. * string, for String cells we'll set its value. For other types we will
  178. * change the cell to a string cell and set its value.
  179. * If value is null then we will change the cell to a Blank cell.
  180. */
  181. void setCellValue(String value);
  182. /**
  183. * Sets formula for this cell.
  184. * <p>
  185. * Note, this method only sets the formula string and does not calculate the formula value.
  186. * To set the precalculated value use {@link #setCellValue(double)} or {@link #setCellValue(String)}
  187. * </p>
  188. *
  189. * @param formula the formula to set, e.g. <code>SUM(C4:E4)</code>.
  190. * If the argument is <code>null</code> then the current formula is removed.
  191. * @throws IllegalArgumentException if the formula is unparsable
  192. */
  193. void setCellFormula(String formula);
  194. /**
  195. * Return a formula for the cell, for example, <code>SUM(C4:E4)</code>
  196. *
  197. * @return a formula for the cell
  198. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CELL_TYPE_FORMULA
  199. */
  200. String getCellFormula();
  201. /**
  202. * Get the value of the cell as a number.
  203. * <p>
  204. * For strings we throw an exception. For blank cells we return a 0.
  205. * For formulas or error cells we return the precalculated value;
  206. * </p>
  207. * @return the value of the cell as a number
  208. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING
  209. * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
  210. * @see DataFormatter for turning this number into a string similar to that which Excel would render this number as.
  211. */
  212. double getNumericCellValue();
  213. /**
  214. * Get the value of the cell as a date.
  215. * <p>
  216. * For strings we throw an exception. For blank cells we return a null.
  217. * </p>
  218. * @return the value of the cell as a date
  219. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CELL_TYPE_STRING
  220. * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
  221. * @see DataFormatter for formatting this date into a string similar to how excel does.
  222. */
  223. Date getDateCellValue();
  224. /**
  225. * Get the value of the cell as a XSSFRichTextString
  226. * <p>
  227. * For numeric cells we throw an exception. For blank cells we return an empty string.
  228. * For formula cells we return the pre-calculated value.
  229. * </p>
  230. * @return the value of the cell as a XSSFRichTextString
  231. */
  232. RichTextString getRichStringCellValue();
  233. /**
  234. * Get the value of the cell as a string
  235. * <p>
  236. * For numeric cells we throw an exception. For blank cells we return an empty string.
  237. * For formulaCells that are not string Formulas, we return empty String.
  238. * </p>
  239. * @return the value of the cell as a string
  240. */
  241. String getStringCellValue();
  242. /**
  243. * Set a boolean value for the cell
  244. *
  245. * @param value the boolean value to set this cell to. For formulas we'll set the
  246. * precalculated value, for booleans we'll set its value. For other types we
  247. * will change the cell to a boolean cell and set its value.
  248. */
  249. void setCellValue(boolean value);
  250. /**
  251. * Set a error value for the cell
  252. *
  253. * @param value the error value to set this cell to. For formulas we'll set the
  254. * precalculated value , for errors we'll set
  255. * its value. For other types we will change the cell to an error
  256. * cell and set its value.
  257. * @see FormulaError
  258. */
  259. void setCellErrorValue(byte value);
  260. /**
  261. * Get the value of the cell as a boolean.
  262. * <p>
  263. * For strings, numbers, and errors, we throw an exception. For blank cells we return a false.
  264. * </p>
  265. * @return the value of the cell as a boolean
  266. * @throws IllegalStateException if the cell type returned by {@link #getCellType()}
  267. * is not CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA
  268. */
  269. boolean getBooleanCellValue();
  270. /**
  271. * Get the value of the cell as an error code.
  272. * <p>
  273. * For strings, numbers, and booleans, we throw an exception.
  274. * For blank cells we return a 0.
  275. * </p>
  276. *
  277. * @return the value of the cell as an error code
  278. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CELL_TYPE_ERROR
  279. * @see FormulaError for error codes
  280. */
  281. byte getErrorCellValue();
  282. /**
  283. * Set the style for the cell. The style should be an CellStyle created/retreived from
  284. * the Workbook.
  285. *
  286. * @param style reference contained in the workbook.
  287. * If the value is null then the style information is removed causing the cell to used the default workbook style.
  288. * @see org.apache.poi.ss.usermodel.Workbook#createCellStyle()
  289. */
  290. void setCellStyle(CellStyle style);
  291. /**
  292. * Return the cell's style.
  293. *
  294. * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as
  295. * <code>workbook.getCellStyleAt(0)</code>
  296. * @see Workbook#getCellStyleAt(short)
  297. */
  298. CellStyle getCellStyle();
  299. /**
  300. * Sets this cell as the active cell for the worksheet
  301. */
  302. void setAsActiveCell();
  303. /**
  304. * Assign a comment to this cell
  305. *
  306. * @param comment comment associated with this cell
  307. */
  308. void setCellComment(Comment comment);
  309. /**
  310. * Returns comment associated with this cell
  311. *
  312. * @return comment associated with this cell or <code>null</code> if not found
  313. */
  314. Comment getCellComment();
  315. /**
  316. * Removes the comment for this cell, if there is one.
  317. */
  318. void removeCellComment();
  319. /**
  320. * Returns hyperlink associated with this cell
  321. *
  322. * @return hyperlink associated with this cell or <code>null</code> if not found
  323. */
  324. Hyperlink getHyperlink();
  325. /**
  326. * Assign a hypelrink to this cell
  327. *
  328. * @param link hypelrink associated with this cell
  329. */
  330. void setHyperlink(Hyperlink link);
  331. }