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

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