您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Workbook.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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.IOException;
  17. import java.io.OutputStream;
  18. import java.util.List;
  19. import org.apache.poi.ss.formula.udf.UDFFinder;
  20. import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
  21. /**
  22. * High level representation of a Excel workbook. This is the first object most users
  23. * will construct whether they are reading or writing a workbook. It is also the
  24. * top level object for creating new sheets/etc.
  25. */
  26. public interface Workbook {
  27. /** Extended windows meta file */
  28. public static final int PICTURE_TYPE_EMF = 2;
  29. /** Windows Meta File */
  30. public static final int PICTURE_TYPE_WMF = 3;
  31. /** Mac PICT format */
  32. public static final int PICTURE_TYPE_PICT = 4;
  33. /** JPEG format */
  34. public static final int PICTURE_TYPE_JPEG = 5;
  35. /** PNG format */
  36. public static final int PICTURE_TYPE_PNG = 6;
  37. /** Device independent bitmap */
  38. public static final int PICTURE_TYPE_DIB = 7;
  39. /**
  40. * Indicates the sheet is visible.
  41. *
  42. * @see #setSheetHidden(int, int)
  43. */
  44. public static final int SHEET_STATE_VISIBLE = 0;
  45. /**
  46. * Indicates the book window is hidden, but can be shown by the user via the user interface.
  47. *
  48. * @see #setSheetHidden(int, int)
  49. */
  50. public static final int SHEET_STATE_HIDDEN = 1;
  51. /**
  52. * Indicates the sheet is hidden and cannot be shown in the user interface (UI).
  53. *
  54. * <p>
  55. * In Excel this state is only available programmatically in VBA:
  56. * <code>ThisWorkbook.Sheets("MySheetName").Visible = xlSheetVeryHidden </code>
  57. * </p>
  58. *
  59. * @see #setSheetHidden(int, int)
  60. */
  61. public static final int SHEET_STATE_VERY_HIDDEN = 2;
  62. /**
  63. * Convenience method to get the active sheet. The active sheet is is the sheet
  64. * which is currently displayed when the workbook is viewed in Excel.
  65. * 'Selected' sheet(s) is a distinct concept.
  66. *
  67. * @return the index of the active sheet (0-based)
  68. */
  69. int getActiveSheetIndex();
  70. /**
  71. * Convenience method to set the active sheet. The active sheet is is the sheet
  72. * which is currently displayed when the workbook is viewed in Excel.
  73. * 'Selected' sheet(s) is a distinct concept.
  74. *
  75. * @param sheetIndex index of the active sheet (0-based)
  76. */
  77. void setActiveSheet(int sheetIndex);
  78. /**
  79. * Gets the first tab that is displayed in the list of tabs in excel.
  80. *
  81. * @return the first tab that to display in the list of tabs (0-based).
  82. */
  83. int getFirstVisibleTab();
  84. /**
  85. * Sets the first tab that is displayed in the list of tabs in excel.
  86. *
  87. * @param sheetIndex the first tab that to display in the list of tabs (0-based)
  88. */
  89. void setFirstVisibleTab(int sheetIndex);
  90. /**
  91. * Sets the order of appearance for a given sheet.
  92. *
  93. * @param sheetname the name of the sheet to reorder
  94. * @param pos the position that we want to insert the sheet into (0 based)
  95. */
  96. void setSheetOrder(String sheetname, int pos);
  97. /**
  98. * Sets the tab whose data is actually seen when the sheet is opened.
  99. * This may be different from the "selected sheet" since excel seems to
  100. * allow you to show the data of one sheet when another is seen "selected"
  101. * in the tabs (at the bottom).
  102. *
  103. * @see Sheet#setSelected(boolean)
  104. * @param index the index of the sheet to select (0 based)
  105. */
  106. void setSelectedTab(int index);
  107. /**
  108. * Set the sheet name.
  109. *
  110. * @param sheet number (0 based)
  111. * @throws IllegalArgumentException if the name is greater than 31 chars or contains <code>/\?*[]</code>
  112. */
  113. void setSheetName(int sheet, String name);
  114. /**
  115. * Set the sheet name
  116. *
  117. * @param sheet sheet number (0 based)
  118. * @return Sheet name
  119. */
  120. String getSheetName(int sheet);
  121. /**
  122. * Returns the index of the sheet by his name
  123. *
  124. * @param name the sheet name
  125. * @return index of the sheet (0 based)
  126. */
  127. int getSheetIndex(String name);
  128. /**
  129. * Returns the index of the given sheet
  130. *
  131. * @param sheet the sheet to look up
  132. * @return index of the sheet (0 based)
  133. */
  134. int getSheetIndex(Sheet sheet);
  135. /**
  136. * Sreate an Sheet for this Workbook, adds it to the sheets and returns
  137. * the high level representation. Use this to create new sheets.
  138. *
  139. * @return Sheet representing the new sheet.
  140. */
  141. Sheet createSheet();
  142. /**
  143. * Create an Sheet for this Workbook, adds it to the sheets and returns
  144. * the high level representation. Use this to create new sheets.
  145. *
  146. * @param sheetname sheetname to set for the sheet.
  147. * @return Sheet representing the new sheet.
  148. * @throws IllegalArgumentException if the name is greater than 31 chars or contains <code>/\?*[]</code>
  149. */
  150. Sheet createSheet(String sheetname);
  151. /**
  152. * Create an Sheet from an existing sheet in the Workbook.
  153. *
  154. * @return Sheet representing the cloned sheet.
  155. */
  156. Sheet cloneSheet(int sheetNum);
  157. /**
  158. * Get the number of spreadsheets in the workbook
  159. *
  160. * @return the number of sheets
  161. */
  162. int getNumberOfSheets();
  163. /**
  164. * Get the Sheet object at the given index.
  165. *
  166. * @param index of the sheet number (0-based physical & logical)
  167. * @return Sheet at the provided index
  168. */
  169. Sheet getSheetAt(int index);
  170. /**
  171. * Get sheet with the given name
  172. *
  173. * @param name of the sheet
  174. * @return Sheet with the name provided or <code>null</code> if it does not exist
  175. */
  176. Sheet getSheet(String name);
  177. /**
  178. * Removes sheet at the given index
  179. *
  180. * @param index of the sheet to remove (0-based)
  181. */
  182. void removeSheetAt(int index);
  183. /**
  184. * Sets the repeating rows and columns for a sheet (as found in
  185. * File->PageSetup->Sheet). This is function is included in the workbook
  186. * because it creates/modifies name records which are stored at the
  187. * workbook level.
  188. * <p>
  189. * To set just repeating columns:
  190. * <pre>
  191. * workbook.setRepeatingRowsAndColumns(0,0,1,-1-1);
  192. * </pre>
  193. * To set just repeating rows:
  194. * <pre>
  195. * workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4);
  196. * </pre>
  197. * To remove all repeating rows and columns for a sheet.
  198. * <pre>
  199. * workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1);
  200. * </pre>
  201. *
  202. * @param sheetIndex 0 based index to sheet.
  203. * @param startColumn 0 based start of repeating columns.
  204. * @param endColumn 0 based end of repeating columns.
  205. * @param startRow 0 based start of repeating rows.
  206. * @param endRow 0 based end of repeating rows.
  207. */
  208. void setRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow);
  209. /**
  210. * Create a new Font and add it to the workbook's font table
  211. *
  212. * @return new font object
  213. */
  214. Font createFont();
  215. /**
  216. * Finds a font that matches the one with the supplied attributes
  217. *
  218. * @return the font with the matched attributes or <code>null</code>
  219. */
  220. Font findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline);
  221. /**
  222. * Get the number of fonts in the font table
  223. *
  224. * @return number of fonts
  225. */
  226. short getNumberOfFonts();
  227. /**
  228. * Get the font at the given index number
  229. *
  230. * @param idx index number (0-based)
  231. * @return font at the index
  232. */
  233. Font getFontAt(short idx);
  234. /**
  235. * Create a new Cell style and add it to the workbook's style table
  236. *
  237. * @return the new Cell Style object
  238. */
  239. CellStyle createCellStyle();
  240. /**
  241. * Get the number of styles the workbook contains
  242. *
  243. * @return count of cell styles
  244. */
  245. short getNumCellStyles();
  246. /**
  247. * Get the cell style object at the given index
  248. *
  249. * @param idx index within the set of styles (0-based)
  250. * @return CellStyle object at the index
  251. */
  252. CellStyle getCellStyleAt(short idx);
  253. /**
  254. * Write out this workbook to an Outputstream.
  255. *
  256. * @param stream - the java OutputStream you wish to write to
  257. * @exception IOException if anything can't be written.
  258. */
  259. void write(OutputStream stream) throws IOException;
  260. /**
  261. * @return the total number of defined names in this workbook
  262. */
  263. int getNumberOfNames();
  264. /**
  265. * @param name the name of the defined name
  266. * @return the defined name with the specified name. <code>null</code> if not found.
  267. */
  268. Name getName(String name);
  269. /**
  270. * @param nameIndex position of the named range (0-based)
  271. * @return the defined name at the specified index
  272. * @throws IllegalArgumentException if the supplied index is invalid
  273. */
  274. Name getNameAt(int nameIndex);
  275. /**
  276. * Creates a new (uninitialised) defined name in this workbook
  277. *
  278. * @return new defined name object
  279. */
  280. Name createName();
  281. /**
  282. * Gets the defined name index by name<br/>
  283. * <i>Note:</i> Excel defined names are case-insensitive and
  284. * this method performs a case-insensitive search.
  285. *
  286. * @param name the name of the defined name
  287. * @return zero based index of the defined name. <tt>-1</tt> if not found.
  288. */
  289. int getNameIndex(String name);
  290. /**
  291. * Remove the defined name at the specified index
  292. *
  293. * @param index named range index (0 based)
  294. */
  295. void removeName(int index);
  296. /**
  297. * Remove a defined name by name
  298. *
  299. * @param name the name of the defined name
  300. */
  301. void removeName(String name);
  302. /**
  303. * Sets the printarea for the sheet provided
  304. * <p>
  305. * i.e. Reference = $A$1:$B$2
  306. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  307. * @param reference Valid name Reference for the Print Area
  308. */
  309. void setPrintArea(int sheetIndex, String reference);
  310. /**
  311. * For the Convenience of Java Programmers maintaining pointers.
  312. * @see #setPrintArea(int, String)
  313. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  314. * @param startColumn Column to begin printarea
  315. * @param endColumn Column to end the printarea
  316. * @param startRow Row to begin the printarea
  317. * @param endRow Row to end the printarea
  318. */
  319. void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow);
  320. /**
  321. * Retrieves the reference for the printarea of the specified sheet,
  322. * the sheet name is appended to the reference even if it was not specified.
  323. *
  324. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  325. * @return String Null if no print area has been defined
  326. */
  327. String getPrintArea(int sheetIndex);
  328. /**
  329. * Delete the printarea for the sheet specified
  330. *
  331. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  332. */
  333. void removePrintArea(int sheetIndex);
  334. /**
  335. * Retrieves the current policy on what to do when
  336. * getting missing or blank cells from a row.
  337. * <p>
  338. * The default is to return blank and null cells.
  339. * {@link MissingCellPolicy}
  340. * </p>
  341. */
  342. MissingCellPolicy getMissingCellPolicy();
  343. /**
  344. * Sets the policy on what to do when
  345. * getting missing or blank cells from a row.
  346. *
  347. * This will then apply to all calls to
  348. * {@link Row#getCell(int)} }. See
  349. * {@link MissingCellPolicy}
  350. */
  351. void setMissingCellPolicy(MissingCellPolicy missingCellPolicy);
  352. /**
  353. * Returns the instance of DataFormat for this workbook.
  354. *
  355. * @return the DataFormat object
  356. */
  357. DataFormat createDataFormat();
  358. /**
  359. * Adds a picture to the workbook.
  360. *
  361. * @param pictureData The bytes of the picture
  362. * @param format The format of the picture.
  363. *
  364. * @return the index to this picture (1 based).
  365. * @see #PICTURE_TYPE_EMF
  366. * @see #PICTURE_TYPE_WMF
  367. * @see #PICTURE_TYPE_PICT
  368. * @see #PICTURE_TYPE_JPEG
  369. * @see #PICTURE_TYPE_PNG
  370. * @see #PICTURE_TYPE_DIB
  371. */
  372. int addPicture(byte[] pictureData, int format);
  373. /**
  374. * Gets all pictures from the Workbook.
  375. *
  376. * @return the list of pictures (a list of {@link PictureData} objects.)
  377. */
  378. List<? extends PictureData> getAllPictures();
  379. /**
  380. * Returns an object that handles instantiating concrete
  381. * classes of the various instances one needs for HSSF and XSSF.
  382. */
  383. CreationHelper getCreationHelper();
  384. /**
  385. * @return <code>false</code> if this workbook is not visible in the GUI
  386. */
  387. boolean isHidden();
  388. /**
  389. * @param hiddenFlag pass <code>false</code> to make the workbook visible in the GUI
  390. */
  391. void setHidden(boolean hiddenFlag);
  392. /**
  393. * Check whether a sheet is hidden.
  394. * <p>
  395. * Note that a sheet could instead be set to be very hidden, which is different
  396. * ({@link #isSheetVeryHidden(int)})
  397. * </p>
  398. * @param sheetIx Number
  399. * @return <code>true</code> if sheet is hidden
  400. */
  401. boolean isSheetHidden(int sheetIx);
  402. /**
  403. * Check whether a sheet is very hidden.
  404. * <p>
  405. * This is different from the normal hidden status
  406. * ({@link #isSheetHidden(int)})
  407. * </p>
  408. * @param sheetIx sheet index to check
  409. * @return <code>true</code> if sheet is very hidden
  410. */
  411. boolean isSheetVeryHidden(int sheetIx);
  412. /**
  413. * Hide or unhide a sheet
  414. *
  415. * @param sheetIx the sheet index (0-based)
  416. * @param hidden True to mark the sheet as hidden, false otherwise
  417. */
  418. void setSheetHidden(int sheetIx, boolean hidden);
  419. /**
  420. * Hide or unhide a sheet.
  421. *
  422. * <ul>
  423. * <li>0 - visible. </li>
  424. * <li>1 - hidden. </li>
  425. * <li>2 - very hidden.</li>
  426. * </ul>
  427. * @param sheetIx the sheet index (0-based)
  428. * @param hidden one of the following <code>Workbook</code> constants:
  429. * <code>Workbook.SHEET_STATE_VISIBLE</code>,
  430. * <code>Workbook.SHEET_STATE_HIDDEN</code>, or
  431. * <code>Workbook.SHEET_STATE_VERY_HIDDEN</code>.
  432. * @throws IllegalArgumentException if the supplied sheet index or state is invalid
  433. */
  434. void setSheetHidden(int sheetIx, int hidden);
  435. /**
  436. * Register a new toolpack in this workbook.
  437. *
  438. * @param toopack the toolpack to register
  439. */
  440. void addToolPack(UDFFinder toopack);
  441. }