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.

Workbook.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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.io.Closeable;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.apache.poi.ss.SpreadsheetVersion;
  22. import org.apache.poi.ss.formula.EvaluationWorkbook;
  23. import org.apache.poi.ss.formula.udf.UDFFinder;
  24. import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
  25. import org.apache.poi.util.Removal;
  26. /**
  27. * High level representation of a Excel workbook. This is the first object most users
  28. * will construct whether they are reading or writing a workbook. It is also the
  29. * top level object for creating new sheets/etc.
  30. */
  31. public interface Workbook extends Closeable, Iterable<Sheet> {
  32. /** Extended windows meta file */
  33. int PICTURE_TYPE_EMF = 2;
  34. /** Windows Meta File */
  35. int PICTURE_TYPE_WMF = 3;
  36. /** Mac PICT format */
  37. int PICTURE_TYPE_PICT = 4;
  38. /** JPEG format */
  39. int PICTURE_TYPE_JPEG = 5;
  40. /** PNG format */
  41. int PICTURE_TYPE_PNG = 6;
  42. /** Device independent bitmap */
  43. int PICTURE_TYPE_DIB = 7;
  44. /**
  45. * Excel silently truncates long sheet names to 31 chars.
  46. * This constant is used to ensure uniqueness in the first 31 chars
  47. */
  48. int MAX_SENSITIVE_SHEET_NAME_LEN = 31;
  49. /**
  50. * Convenience method to get the active sheet. The active sheet is is the sheet
  51. * which is currently displayed when the workbook is viewed in Excel.
  52. * 'Selected' sheet(s) is a distinct concept.
  53. *
  54. * @return the index of the active sheet (0-based)
  55. */
  56. int getActiveSheetIndex();
  57. /**
  58. * Convenience method to set the active sheet. The active sheet is is the sheet
  59. * which is currently displayed when the workbook is viewed in Excel.
  60. * 'Selected' sheet(s) is a distinct concept.
  61. *
  62. * @param sheetIndex index of the active sheet (0-based)
  63. */
  64. void setActiveSheet(int sheetIndex);
  65. /**
  66. * Gets the first tab that is displayed in the list of tabs in excel.
  67. *
  68. * @return the first tab that to display in the list of tabs (0-based).
  69. */
  70. int getFirstVisibleTab();
  71. /**
  72. * Sets the first tab that is displayed in the list of tabs in excel.
  73. *
  74. * @param sheetIndex the first tab that to display in the list of tabs (0-based)
  75. */
  76. void setFirstVisibleTab(int sheetIndex);
  77. /**
  78. * Sets the order of appearance for a given sheet.
  79. *
  80. * @param sheetname the name of the sheet to reorder
  81. * @param pos the position that we want to insert the sheet into (0 based)
  82. */
  83. void setSheetOrder(String sheetname, int pos);
  84. /**
  85. * Sets the tab whose data is actually seen when the sheet is opened.
  86. * This may be different from the "selected sheet" since excel seems to
  87. * allow you to show the data of one sheet when another is seen "selected"
  88. * in the tabs (at the bottom).
  89. *
  90. * @see Sheet#setSelected(boolean)
  91. * @param index the index of the sheet to select (0 based)
  92. */
  93. void setSelectedTab(int index);
  94. /**
  95. * Set the sheet name.
  96. * <p>
  97. * See {@link org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
  98. * for a safe way to create valid names
  99. * </p>
  100. * @param sheet number (0 based)
  101. * @throws IllegalArgumentException if the name is null or invalid
  102. * or workbook already contains a sheet with this name
  103. * @see #createSheet(String)
  104. * @see org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)
  105. */
  106. void setSheetName(int sheet, String name);
  107. /**
  108. * Get the sheet name
  109. *
  110. * @param sheet sheet number (0 based)
  111. * @return Sheet name
  112. */
  113. String getSheetName(int sheet);
  114. /**
  115. * Returns the index of the sheet by his name
  116. *
  117. * @param name the sheet name
  118. * @return index of the sheet (0 based)
  119. */
  120. int getSheetIndex(String name);
  121. /**
  122. * Returns the index of the given sheet
  123. *
  124. * @param sheet the sheet to look up
  125. * @return index of the sheet (0 based)
  126. */
  127. int getSheetIndex(Sheet sheet);
  128. /**
  129. * Create a Sheet for this Workbook, adds it to the sheets and returns
  130. * the high level representation. Use this to create new sheets.
  131. *
  132. * @return Sheet representing the new sheet.
  133. */
  134. Sheet createSheet();
  135. /**
  136. * Create a new sheet for this Workbook and return the high level representation.
  137. * Use this to create new sheets.
  138. *
  139. * <p>
  140. * Note that Excel allows sheet names up to 31 chars in length but other applications
  141. * (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars,
  142. * others - truncate such names to 31 character.
  143. * </p>
  144. * <p>
  145. * POI's SpreadsheetAPI silently truncates the input argument to 31 characters.
  146. * Example:
  147. *
  148. * <pre>{@code
  149. * Sheet sheet = workbook.createSheet("My very long sheet name which is longer than 31 chars"); // will be truncated
  150. * assert 31 == sheet.getSheetName().length();
  151. * assert "My very long sheet name which i" == sheet.getSheetName();
  152. * }</pre>
  153. *
  154. * Except the 31-character constraint, Excel applies some other rules:
  155. * <p>
  156. * Sheet name MUST be unique in the workbook and MUST NOT contain the any of the following characters:
  157. * <ul>
  158. * <li> 0x0000 </li>
  159. * <li> 0x0003 </li>
  160. * <li> colon (:) </li>
  161. * <li> backslash (\) </li>
  162. * <li> asterisk (*) </li>
  163. * <li> question mark (?) </li>
  164. * <li> forward slash (/) </li>
  165. * <li> opening square bracket ([) </li>
  166. * <li> closing square bracket (]) </li>
  167. * </ul>
  168. * The string MUST NOT begin or end with the single quote (') character.
  169. *
  170. * <p>
  171. * See {@link org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
  172. * for a safe way to create valid names
  173. * </p>
  174. * @param sheetname The name to set for the sheet.
  175. * @return Sheet representing the new sheet.
  176. * @throws IllegalArgumentException if the name is null or invalid
  177. * or workbook already contains a sheet with this name
  178. * @see org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)
  179. */
  180. Sheet createSheet(String sheetname);
  181. /**
  182. * Create a Sheet from an existing sheet in the Workbook.
  183. *
  184. * @return Sheet representing the cloned sheet.
  185. */
  186. Sheet cloneSheet(int sheetNum);
  187. /**
  188. * Returns an iterator of the sheets in the workbook
  189. * in sheet order. Includes hidden and very hidden sheets.
  190. *
  191. * @return an iterator of the sheets.
  192. */
  193. Iterator<Sheet> sheetIterator();
  194. /**
  195. * Get the number of spreadsheets in the workbook
  196. *
  197. * @return the number of sheets
  198. */
  199. int getNumberOfSheets();
  200. /**
  201. * Get the Sheet object at the given index.
  202. *
  203. * @param index of the sheet number (0-based physical &amp; logical)
  204. * @return Sheet at the provided index
  205. * @throws IllegalArgumentException if the index is out of range (index
  206. * &lt; 0 || index &gt;= getNumberOfSheets()).
  207. */
  208. Sheet getSheetAt(int index);
  209. /**
  210. * Get sheet with the given name
  211. *
  212. * @param name of the sheet
  213. * @return Sheet with the name provided or {@code null} if it does not exist
  214. */
  215. Sheet getSheet(String name);
  216. /**
  217. * Removes sheet at the given index
  218. *
  219. * @param index of the sheet to remove (0-based)
  220. */
  221. void removeSheetAt(int index);
  222. /**
  223. * Create a new Font and add it to the workbook's font table
  224. *
  225. * @return new font object
  226. */
  227. Font createFont();
  228. /**
  229. * Finds a font that matches the one with the supplied attributes
  230. *
  231. * @return the font with the matched attributes or {@code null}
  232. */
  233. Font findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline);
  234. /**
  235. * Get the number of fonts in the font table
  236. *
  237. * @return number of fonts (as int since POI 5.0.0)
  238. */
  239. int getNumberOfFonts();
  240. /**
  241. * Get the number of fonts in the font table
  242. *
  243. * @return number of fonts
  244. * @since 4.0.0
  245. */
  246. @Deprecated
  247. @Removal(version = "6.0.0")
  248. int getNumberOfFontsAsInt();
  249. /**
  250. * Get the font at the given index number
  251. *
  252. * @param idx index number (0-based)
  253. * @return font at the index
  254. * @since 4.0.0
  255. */
  256. Font getFontAt(int idx);
  257. /**
  258. * Create a new Cell style and add it to the workbook's style table
  259. *
  260. * @return the new Cell Style object
  261. * @throws IllegalStateException if the number of cell styles exceeded the limit for this type of Workbook.
  262. */
  263. CellStyle createCellStyle();
  264. /**
  265. * Get the number of styles the workbook contains
  266. *
  267. * @return count of cell styles
  268. */
  269. int getNumCellStyles();
  270. /**
  271. * Get the cell style object at the given index
  272. *
  273. * @param idx index within the set of styles (0-based)
  274. * @return CellStyle object at the index
  275. */
  276. CellStyle getCellStyleAt(int idx);
  277. /**
  278. * Write out this workbook to an OutputStream.
  279. *
  280. * @param stream - the java OutputStream you wish to write to
  281. * @throws IOException if anything can't be written.
  282. */
  283. void write(OutputStream stream) throws IOException;
  284. /**
  285. * Close the underlying input resource (File or Stream),
  286. * from which the Workbook was read.
  287. *
  288. * <p>Once this has been called, no further
  289. * operations, updates or reads should be performed on the
  290. * Workbook.
  291. */
  292. @Override
  293. void close() throws IOException;
  294. /**
  295. * @return the total number of defined names in this workbook
  296. */
  297. int getNumberOfNames();
  298. /**
  299. * @param name the name of the defined name
  300. * @return the defined name with the specified name. {@code null} if not found.
  301. */
  302. Name getName(String name);
  303. /**
  304. * Returns all defined names with the given name.
  305. *
  306. * @param name the name of the defined name
  307. * @return a list of the defined names with the specified name. An empty list is returned if none is found.
  308. */
  309. List<? extends Name> getNames(String name);
  310. /**
  311. * Returns all defined names.
  312. *
  313. * @return a list of the defined names. An empty list is returned if none is found.
  314. */
  315. List<? extends Name> getAllNames();
  316. /**
  317. * Creates a new (uninitialised) defined name in this workbook
  318. *
  319. * @return new defined name object
  320. */
  321. Name createName();
  322. /**
  323. * Remove a defined name
  324. *
  325. * @param name the name of the defined name
  326. */
  327. void removeName(Name name);
  328. /**
  329. * Adds the linking required to allow formulas referencing
  330. * the specified external workbook to be added to this one.
  331. * <p>In order for formulas such as "[MyOtherWorkbook]Sheet3!$A$5"
  332. * to be added to the file, some linking information must first
  333. * be recorded. Once a given external workbook has been linked,
  334. * then formulas using it can added. Each workbook needs linking
  335. * only once.
  336. * <p>This linking only applies for writing formulas. To link things
  337. * for evaluation, see {@link FormulaEvaluator#setupReferencedWorkbooks(java.util.Map)}
  338. *
  339. * @param name The name the workbook will be referenced as in formulas
  340. * @param workbook The open workbook to fetch the link required information from
  341. */
  342. int linkExternalWorkbook(String name, Workbook workbook);
  343. /**
  344. * Sets the printarea for the sheet provided
  345. * <p>
  346. * i.e. Reference = $A$1:$B$2
  347. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  348. * @param reference Valid name Reference for the Print Area
  349. */
  350. void setPrintArea(int sheetIndex, String reference);
  351. /**
  352. * For the Convenience of Java Programmers maintaining pointers.
  353. * @see #setPrintArea(int, String)
  354. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  355. * @param startColumn Column to begin printarea
  356. * @param endColumn Column to end the printarea
  357. * @param startRow Row to begin the printarea
  358. * @param endRow Row to end the printarea
  359. */
  360. void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow);
  361. /**
  362. * Retrieves the reference for the printarea of the specified sheet,
  363. * the sheet name is appended to the reference even if it was not specified.
  364. *
  365. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  366. * @return String Null if no print area has been defined
  367. */
  368. String getPrintArea(int sheetIndex);
  369. /**
  370. * Delete the printarea for the sheet specified
  371. *
  372. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  373. */
  374. void removePrintArea(int sheetIndex);
  375. /**
  376. * Retrieves the current policy on what to do when
  377. * getting missing or blank cells from a row.
  378. * <p>
  379. * The default is to return blank and null cells.
  380. * {@link MissingCellPolicy}
  381. * </p>
  382. */
  383. MissingCellPolicy getMissingCellPolicy();
  384. /**
  385. * Sets the policy on what to do when
  386. * getting missing or blank cells from a row.
  387. *
  388. * This will then apply to all calls to
  389. * {@link Row#getCell(int)} }. See
  390. * {@link MissingCellPolicy}
  391. */
  392. void setMissingCellPolicy(MissingCellPolicy missingCellPolicy);
  393. /**
  394. * Returns the instance of DataFormat for this workbook.
  395. *
  396. * @return the DataFormat object
  397. */
  398. DataFormat createDataFormat();
  399. /**
  400. * Adds a picture to the workbook.
  401. *
  402. * @param pictureData The bytes of the picture
  403. * @param format The format of the picture.
  404. *
  405. * @return the index to this picture (1 based).
  406. * @see #PICTURE_TYPE_EMF
  407. * @see #PICTURE_TYPE_WMF
  408. * @see #PICTURE_TYPE_PICT
  409. * @see #PICTURE_TYPE_JPEG
  410. * @see #PICTURE_TYPE_PNG
  411. * @see #PICTURE_TYPE_DIB
  412. */
  413. int addPicture(byte[] pictureData, int format);
  414. /**
  415. * Gets all pictures from the Workbook.
  416. *
  417. * @return the list of pictures (a list of {@link PictureData} objects.)
  418. */
  419. List<? extends PictureData> getAllPictures();
  420. /**
  421. * Returns an object that handles instantiating concrete
  422. * classes of the various instances one needs for HSSF and XSSF.
  423. */
  424. CreationHelper getCreationHelper();
  425. /**
  426. * @return {@code false} if this workbook is not visible in the GUI
  427. */
  428. boolean isHidden();
  429. /**
  430. * @param hiddenFlag pass {@code false} to make the workbook visible in the GUI
  431. */
  432. void setHidden(boolean hiddenFlag);
  433. /**
  434. * Check whether a sheet is hidden.
  435. * <p>
  436. * Note that a sheet could instead be set to be very hidden, which is different
  437. * ({@link #isSheetVeryHidden(int)})
  438. * </p>
  439. * @param sheetIx Number
  440. * @return {@code true} if sheet is hidden
  441. * @see #getSheetVisibility(int)
  442. */
  443. boolean isSheetHidden(int sheetIx);
  444. /**
  445. * Check whether a sheet is very hidden.
  446. * <p>
  447. * This is different from the normal hidden status
  448. * ({@link #isSheetHidden(int)})
  449. * </p>
  450. * @param sheetIx sheet index to check
  451. * @return {@code true} if sheet is very hidden
  452. * @see #getSheetVisibility(int)
  453. */
  454. boolean isSheetVeryHidden(int sheetIx);
  455. /**
  456. * Hide or unhide a sheet.
  457. *
  458. * Please note that the sheet currently set as active sheet (sheet 0 in a newly
  459. * created workbook or the one set via setActiveSheet()) cannot be hidden.
  460. *
  461. * @param sheetIx the sheet index (0-based)
  462. * @param hidden True to mark the sheet as hidden, false otherwise
  463. * @see #setSheetVisibility(int, SheetVisibility)
  464. */
  465. void setSheetHidden(int sheetIx, boolean hidden);
  466. /**
  467. * Get the visibility (visible, hidden, very hidden) of a sheet in this workbook
  468. *
  469. * @param sheetIx the index of the sheet
  470. * @return the sheet visibility
  471. * @since POI 3.16 beta 2
  472. */
  473. SheetVisibility getSheetVisibility(int sheetIx);
  474. /**
  475. * Hide or unhide a sheet.
  476. *
  477. * Please note that the sheet currently set as active sheet (sheet 0 in a newly
  478. * created workbook or the one set via setActiveSheet()) cannot be hidden.
  479. *
  480. * @param sheetIx the sheet index (0-based)
  481. * @param visibility the sheet visibility to set
  482. * @since POI 3.16 beta 2
  483. */
  484. void setSheetVisibility(int sheetIx, SheetVisibility visibility);
  485. /**
  486. * Register a new toolpack in this workbook.
  487. *
  488. * @param toolpack the toolpack to register
  489. */
  490. void addToolPack(UDFFinder toolpack);
  491. /**
  492. * Whether the application shall perform a full recalculation when the workbook is opened.
  493. * <p>
  494. * Typically you want to force formula recalculation when you modify cell formulas or values
  495. * of a workbook previously created by Excel. When set to true, this flag will tell Excel
  496. * that it needs to recalculate all formulas in the workbook the next time the file is opened.
  497. * </p>
  498. * <p>
  499. * Note, that recalculation updates cached formula results and, thus, modifies the workbook.
  500. * Depending on the version, Excel may prompt you with "Do you want to save the changes in <em>filename</em>?"
  501. * on close.
  502. * </p>
  503. *
  504. * @param value true if the application will perform a full recalculation of
  505. * workbook values when the workbook is opened
  506. * @since 3.8
  507. */
  508. void setForceFormulaRecalculation(boolean value);
  509. /**
  510. * Whether Excel will be asked to recalculate all formulas when the workbook is opened.
  511. *
  512. * @since 3.8
  513. */
  514. boolean getForceFormulaRecalculation();
  515. /**
  516. * Returns the spreadsheet version of this workbook
  517. *
  518. * @return SpreadsheetVersion enum
  519. * @since 3.14 beta 2
  520. */
  521. SpreadsheetVersion getSpreadsheetVersion();
  522. /**
  523. * Adds an OLE package manager object with the given content to the sheet
  524. *
  525. * @param oleData the payload
  526. * @param label the label of the payload
  527. * @param fileName the original filename
  528. * @param command the command to open the payload
  529. *
  530. * @return the index of the added ole object, i.e. the storage id
  531. *
  532. * @throws IOException if the object can't be embedded
  533. */
  534. int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException;
  535. /**
  536. * @return an evaluation workbook
  537. */
  538. EvaluationWorkbook createEvaluationWorkbook();
  539. }