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

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