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.

XSSFWorkbook.java 88KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  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.xssf.usermodel;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import static org.apache.poi.xssf.usermodel.helpers.XSSFPasswordHelper.setPassword;
  18. import static org.apache.poi.xssf.usermodel.helpers.XSSFPasswordHelper.validatePassword;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Iterator;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. import java.util.Locale;
  31. import java.util.Map;
  32. import java.util.NoSuchElementException;
  33. import java.util.Spliterator;
  34. import java.util.regex.Pattern;
  35. import javax.xml.namespace.QName;
  36. import org.apache.commons.collections4.ListValuedMap;
  37. import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
  38. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  39. import org.apache.logging.log4j.LogManager;
  40. import org.apache.logging.log4j.Logger;
  41. import org.apache.poi.hpsf.ClassIDPredefined;
  42. import org.apache.poi.ooxml.POIXMLDocument;
  43. import org.apache.poi.ooxml.POIXMLDocumentPart;
  44. import org.apache.poi.ooxml.POIXMLException;
  45. import org.apache.poi.ooxml.POIXMLProperties;
  46. import org.apache.poi.ooxml.util.PackageHelper;
  47. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  48. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  49. import org.apache.poi.openxml4j.opc.OPCPackage;
  50. import org.apache.poi.openxml4j.opc.PackageAccess;
  51. import org.apache.poi.openxml4j.opc.PackagePart;
  52. import org.apache.poi.openxml4j.opc.PackagePartName;
  53. import org.apache.poi.openxml4j.opc.PackageRelationship;
  54. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  55. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  56. import org.apache.poi.openxml4j.opc.TargetMode;
  57. import org.apache.poi.poifs.crypt.HashAlgorithm;
  58. import org.apache.poi.poifs.filesystem.DirectoryNode;
  59. import org.apache.poi.poifs.filesystem.Ole10Native;
  60. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  61. import org.apache.poi.ss.SpreadsheetVersion;
  62. import org.apache.poi.ss.formula.SheetNameFormatter;
  63. import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
  64. import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
  65. import org.apache.poi.ss.formula.udf.UDFFinder;
  66. import org.apache.poi.ss.usermodel.DataFormat;
  67. import org.apache.poi.ss.usermodel.Date1904Support;
  68. import org.apache.poi.ss.usermodel.Name;
  69. import org.apache.poi.ss.usermodel.Row;
  70. import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
  71. import org.apache.poi.ss.usermodel.Sheet;
  72. import org.apache.poi.ss.usermodel.SheetVisibility;
  73. import org.apache.poi.ss.usermodel.Workbook;
  74. import org.apache.poi.ss.util.CellReference;
  75. import org.apache.poi.ss.util.WorkbookUtil;
  76. import org.apache.poi.util.Beta;
  77. import org.apache.poi.util.IOUtils;
  78. import org.apache.poi.util.Internal;
  79. import org.apache.poi.util.NotImplemented;
  80. import org.apache.poi.util.Removal;
  81. import org.apache.poi.xssf.XLSBUnsupportedException;
  82. import org.apache.poi.xssf.model.CalculationChain;
  83. import org.apache.poi.xssf.model.ExternalLinksTable;
  84. import org.apache.poi.xssf.model.MapInfo;
  85. import org.apache.poi.xssf.model.SharedStringsTable;
  86. import org.apache.poi.xssf.model.StylesTable;
  87. import org.apache.poi.xssf.model.ThemesTable;
  88. import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils;
  89. import org.apache.xmlbeans.XmlException;
  90. import org.apache.xmlbeans.XmlObject;
  91. import org.apache.xmlbeans.XmlOptions;
  92. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView;
  93. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
  94. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr;
  95. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
  96. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
  97. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
  98. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference;
  99. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCache;
  100. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCaches;
  101. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
  102. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheets;
  103. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
  104. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
  105. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookProtection;
  106. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  107. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode;
  108. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
  109. import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
  110. /**
  111. * High level representation of a SpreadsheetML workbook. This is the first object most users
  112. * will construct whether they are reading or writing a workbook. It is also the
  113. * top level object for creating new sheets/etc.
  114. */
  115. public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Support {
  116. private static final Pattern COMMA_PATTERN = Pattern.compile(",");
  117. /**
  118. * Images formats supported by XSSF but not by HSSF
  119. */
  120. public static final int PICTURE_TYPE_GIF = 8;
  121. public static final int PICTURE_TYPE_TIFF = 9;
  122. public static final int PICTURE_TYPE_EPS = 10;
  123. public static final int PICTURE_TYPE_BMP = 11;
  124. public static final int PICTURE_TYPE_WPG = 12;
  125. /**
  126. * The underlying XML bean
  127. */
  128. private CTWorkbook workbook;
  129. /**
  130. * this holds the XSSFSheet objects attached to this workbook
  131. */
  132. private List<XSSFSheet> sheets;
  133. /**
  134. * this holds the XSSFName objects attached to this workbook, keyed by lower-case name
  135. */
  136. private ListValuedMap<String, XSSFName> namedRangesByName;
  137. /**
  138. * this holds the XSSFName objects attached to this workbook
  139. */
  140. private List<XSSFName> namedRanges;
  141. /**
  142. * shared string table - a cache of strings in this workbook
  143. */
  144. private SharedStringsTable sharedStringSource;
  145. /**
  146. * A collection of shared objects used for styling content,
  147. * e.g. fonts, cell styles, colors, etc.
  148. */
  149. private StylesTable stylesSource;
  150. /**
  151. * The locator of user-defined functions.
  152. * By default, includes functions from the Excel Analysis Toolpack
  153. */
  154. private final IndexedUDFFinder _udfFinder = new IndexedUDFFinder(AggregatingUDFFinder.DEFAULT);
  155. private CalculationChain calcChain;
  156. /**
  157. * External Links, for referencing names or cells in other workbooks.
  158. */
  159. private List<ExternalLinksTable> externalLinks;
  160. /**
  161. * A collection of custom XML mappings
  162. */
  163. private MapInfo mapInfo;
  164. /**
  165. * Used to keep track of the data formatter so that all
  166. * createDataFormatter calls return the same one for a given
  167. * book. This ensures that updates from one places is visible
  168. * someplace else.
  169. */
  170. private XSSFDataFormat formatter;
  171. /**
  172. * The policy to apply in the event of missing or
  173. * blank cells when fetching from a row.
  174. * See {@link MissingCellPolicy}
  175. */
  176. private MissingCellPolicy _missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK;
  177. /**
  178. * Whether a call to {@link XSSFCell#setCellFormula(String)} will validate the formula or not.
  179. */
  180. private boolean cellFormulaValidation = true;
  181. /**
  182. * array of pictures for this workbook
  183. */
  184. private List<XSSFPictureData> pictures;
  185. private static final Logger LOG = LogManager.getLogger(XSSFWorkbook.class);
  186. /**
  187. * cached instance of XSSFCreationHelper for this workbook
  188. * @see #getCreationHelper()
  189. */
  190. private XSSFCreationHelper _creationHelper;
  191. /**
  192. * List of all pivot tables in workbook
  193. */
  194. private List<XSSFPivotTable> pivotTables;
  195. private List<CTPivotCache> pivotCaches;
  196. private final XSSFFactory xssfFactory;
  197. /**
  198. * Create a new SpreadsheetML workbook.
  199. */
  200. public XSSFWorkbook() {
  201. this(XSSFWorkbookType.XLSX);
  202. }
  203. public XSSFWorkbook(XSSFFactory factory) {
  204. this(XSSFWorkbookType.XLSX, factory);
  205. }
  206. /**
  207. * Create a new SpreadsheetML workbook.
  208. * @param workbookType The type of workbook to make (.xlsx or .xlsm).
  209. */
  210. public XSSFWorkbook(XSSFWorkbookType workbookType) {
  211. this(workbookType, null);
  212. }
  213. private XSSFWorkbook(XSSFWorkbookType workbookType, XSSFFactory factory) {
  214. super(newPackage(workbookType));
  215. this.xssfFactory = (factory == null) ? XSSFFactory.getInstance() : factory;
  216. onWorkbookCreate();
  217. }
  218. /**
  219. * Constructs a XSSFWorkbook object given a OpenXML4J {@code Package} object,
  220. * see <a href="https://poi.apache.org/oxml4j/">https://poi.apache.org/oxml4j/</a>.
  221. *
  222. * <p>Once you have finished working with the Workbook, you should close the package
  223. * by calling either {@link #close()} or {@link OPCPackage#close()}, to avoid
  224. * leaving file handles open.
  225. *
  226. * <p>Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory
  227. * footprint than an InputStream backed one.
  228. *
  229. * @param pkg the OpenXML4J {@code OPC Package} object.
  230. * @throws IOException
  231. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  232. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  233. * input format
  234. */
  235. public XSSFWorkbook(OPCPackage pkg) throws IOException {
  236. super(pkg);
  237. this.xssfFactory = XSSFFactory.getInstance();
  238. beforeDocumentRead();
  239. // Build a tree of POIXMLDocumentParts, this workbook being the root
  240. load(this.xssfFactory);
  241. // some broken Workbooks miss this...
  242. setBookViewsIfMissing();
  243. }
  244. /**
  245. * Constructs a XSSFWorkbook object, by buffering the whole stream into memory
  246. * and then opening an {@link OPCPackage} object for it.
  247. *
  248. * <p>Using an {@link InputStream} requires more memory than using a File, so
  249. * if a {@link File} is available then you should instead do something like
  250. * <pre>{@code
  251. * OPCPackage pkg = OPCPackage.open(path);
  252. * XSSFWorkbook wb = new XSSFWorkbook(pkg);
  253. * // work with the wb object
  254. * ......
  255. * pkg.close(); // gracefully closes the underlying zip file
  256. * }</pre>
  257. *
  258. * @throws IOException
  259. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  260. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  261. * input format
  262. */
  263. public XSSFWorkbook(InputStream is) throws IOException {
  264. this(PackageHelper.open(is));
  265. }
  266. /**
  267. * Constructs a XSSFWorkbook object from a given file.
  268. *
  269. * <p>Once you have finished working with the Workbook, you should close
  270. * the package by calling {@link #close()}, to avoid leaving file
  271. * handles open.
  272. *
  273. * <p>Opening a XSSFWorkbook from a file has a lower memory footprint
  274. * than opening from an InputStream
  275. *
  276. * @param file the file to open
  277. * @throws IOException
  278. * @throws InvalidFormatException
  279. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  280. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  281. * input format
  282. */
  283. public XSSFWorkbook(File file) throws IOException, InvalidFormatException {
  284. this(OPCPackage.open(file));
  285. }
  286. /**
  287. * Constructs a XSSFWorkbook object given a file name.
  288. *
  289. *
  290. * <p>Once you have finished working with the Workbook, you should close
  291. * the package by calling {@link #close()}, to avoid leaving file
  292. * handles open.
  293. *
  294. * <p>Opening a XSSFWorkbook from a file has a lower memory footprint
  295. * than opening from an InputStream
  296. *
  297. * @param path the file name.
  298. * @throws IOException
  299. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  300. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  301. * input format
  302. *
  303. */
  304. public XSSFWorkbook(String path) throws IOException {
  305. this(openPackage(path));
  306. }
  307. /**
  308. * Constructs a XSSFWorkbook object using Package Part.
  309. * @param part package part
  310. * @throws IOException
  311. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  312. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  313. * input format
  314. * @since POI 4.0.0
  315. */
  316. public XSSFWorkbook(PackagePart part) throws IOException {
  317. this(part.getInputStream());
  318. }
  319. /**
  320. * @return the XSSFFactory
  321. * @since POI 5.1.0
  322. */
  323. public XSSFFactory getXssfFactory() {
  324. return xssfFactory;
  325. }
  326. protected void beforeDocumentRead() {
  327. // Ensure it isn't a XLSB file, which we don't support
  328. if (getCorePart().getContentType().equals(XSSFRelation.XLSB_BINARY_WORKBOOK.getContentType())) {
  329. throw new XLSBUnsupportedException();
  330. }
  331. // Create arrays for parts attached to the workbook itself
  332. pivotTables = new ArrayList<>();
  333. pivotCaches = new ArrayList<>();
  334. }
  335. @Override
  336. protected void onDocumentRead() throws IOException {
  337. try {
  338. WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
  339. this.workbook = doc.getWorkbook();
  340. ThemesTable theme = null;
  341. Map<String, XSSFSheet> shIdMap = new HashMap<>();
  342. Map<String, ExternalLinksTable> elIdMap = new HashMap<>();
  343. for(RelationPart rp : getRelationParts()){
  344. POIXMLDocumentPart p = rp.getDocumentPart();
  345. if(p instanceof SharedStringsTable) {
  346. sharedStringSource = (SharedStringsTable)p;
  347. } else if(p instanceof StylesTable) {
  348. stylesSource = (StylesTable)p;
  349. } else if(p instanceof ThemesTable) {
  350. theme = (ThemesTable)p;
  351. } else if(p instanceof CalculationChain) {
  352. calcChain = (CalculationChain)p;
  353. } else if(p instanceof MapInfo) {
  354. mapInfo = (MapInfo)p;
  355. } else if (p instanceof XSSFSheet) {
  356. shIdMap.put(rp.getRelationship().getId(), (XSSFSheet)p);
  357. } else if (p instanceof ExternalLinksTable) {
  358. elIdMap.put(rp.getRelationship().getId(), (ExternalLinksTable)p);
  359. }
  360. }
  361. boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
  362. if (stylesSource == null) {
  363. // Create Styles if it is missing
  364. if (packageReadOnly) {
  365. stylesSource = new StylesTable();
  366. } else {
  367. stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, this.xssfFactory);
  368. }
  369. }
  370. stylesSource.setWorkbook(this);
  371. stylesSource.setTheme(theme);
  372. if (sharedStringSource == null) {
  373. // Create SST if it is missing
  374. if (packageReadOnly) {
  375. sharedStringSource = new SharedStringsTable();
  376. } else {
  377. sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, this.xssfFactory);
  378. }
  379. }
  380. // Load individual sheets. The order of sheets is defined by the order
  381. // of CTSheet elements in the workbook
  382. sheets = new ArrayList<>(shIdMap.size());
  383. if (this.workbook == null || this.workbook.getSheets() == null ||
  384. this.workbook.getSheets().getSheetArray() == null) {
  385. throw new POIXMLException("Cannot read a workbook without sheets");
  386. }
  387. for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
  388. parseSheet(shIdMap, ctSheet);
  389. }
  390. // Load the external links tables. Their order is defined by the order
  391. // of CTExternalReference elements in the workbook
  392. externalLinks = new ArrayList<>(elIdMap.size());
  393. if (this.workbook.isSetExternalReferences()) {
  394. for (CTExternalReference er : this.workbook.getExternalReferences().getExternalReferenceArray()) {
  395. ExternalLinksTable el = elIdMap.get(er.getId());
  396. if(el == null) {
  397. LOG.atWarn().log("ExternalLinksTable with r:id {} was defined, but didn't exist in package, skipping", er.getId());
  398. continue;
  399. }
  400. externalLinks.add(el);
  401. }
  402. }
  403. // Process the named ranges
  404. reprocessNamedRanges();
  405. } catch (XmlException e) {
  406. throw new POIXMLException(e);
  407. }
  408. }
  409. /**
  410. * Not normally to be called externally, but possibly to be overridden to avoid
  411. * the DOM based parse of large sheets (see examples).
  412. *
  413. * @throws POIXMLException a RuntimeException that can be caused by invalid OOXML data
  414. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  415. * input format
  416. */
  417. public void parseSheet(Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
  418. XSSFSheet sh = shIdMap.get(ctSheet.getId());
  419. if(sh == null) {
  420. LOG.atWarn().log("Sheet with name {} and r:id {} was defined, but didn't exist in package, skipping", ctSheet.getName(), ctSheet.getId());
  421. return;
  422. }
  423. sh.sheet = ctSheet;
  424. sh.onDocumentRead();
  425. sheets.add(sh);
  426. }
  427. /**
  428. * Create a new CTWorkbook with all values set to default
  429. */
  430. private void onWorkbookCreate() {
  431. workbook = CTWorkbook.Factory.newInstance();
  432. // don't EVER use the 1904 date system
  433. CTWorkbookPr workbookPr = workbook.addNewWorkbookPr();
  434. workbookPr.setDate1904(false);
  435. setBookViewsIfMissing();
  436. workbook.addNewSheets();
  437. POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties();
  438. expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR);
  439. sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, this.xssfFactory);
  440. stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, this.xssfFactory);
  441. stylesSource.setWorkbook(this);
  442. namedRanges = new ArrayList<>();
  443. namedRangesByName = new ArrayListValuedHashMap<>();
  444. sheets = new ArrayList<>();
  445. pivotTables = new ArrayList<>();
  446. externalLinks = new ArrayList<>();
  447. }
  448. private void setBookViewsIfMissing() {
  449. if(!workbook.isSetBookViews()) {
  450. CTBookViews bvs = workbook.addNewBookViews();
  451. CTBookView bv = bvs.addNewWorkbookView();
  452. bv.setActiveTab(0);
  453. }
  454. }
  455. /**
  456. * Create a new SpreadsheetML package and setup the default minimal content
  457. */
  458. protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
  459. OPCPackage pkg = null;
  460. try {
  461. pkg = OPCPackage.create(new UnsynchronizedByteArrayOutputStream()); // NOSONAR - we do not want to close this here
  462. // Main part
  463. PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
  464. // Create main part relationship
  465. pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
  466. // Create main document part
  467. pkg.createPart(corePartName, workbookType.getContentType());
  468. pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
  469. } catch (Exception e) {
  470. IOUtils.closeQuietly(pkg);
  471. throw new POIXMLException(e);
  472. }
  473. return pkg;
  474. }
  475. /**
  476. * Return the underlying XML bean
  477. *
  478. * @return the underlying CTWorkbook bean
  479. */
  480. @Internal
  481. public CTWorkbook getCTWorkbook() {
  482. return this.workbook;
  483. }
  484. /**
  485. * Adds a picture to the workbook.
  486. *
  487. * @param pictureData The bytes of the picture
  488. * @param format The format of the picture.
  489. *
  490. * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
  491. * @see Workbook#PICTURE_TYPE_EMF
  492. * @see Workbook#PICTURE_TYPE_WMF
  493. * @see Workbook#PICTURE_TYPE_PICT
  494. * @see Workbook#PICTURE_TYPE_JPEG
  495. * @see Workbook#PICTURE_TYPE_PNG
  496. * @see Workbook#PICTURE_TYPE_DIB
  497. * @see #getAllPictures()
  498. */
  499. @Override
  500. public int addPicture(byte[] pictureData, int format) {
  501. int imageNumber = getAllPictures().size() + 1;
  502. XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], this.xssfFactory, imageNumber, true).getDocumentPart();
  503. try (OutputStream out = img.getPackagePart().getOutputStream()) {
  504. out.write(pictureData);
  505. } catch (IOException e) {
  506. throw new POIXMLException(e);
  507. }
  508. pictures.add(img);
  509. return imageNumber - 1;
  510. }
  511. /**
  512. * Adds a picture to the workbook.
  513. *
  514. * @param is The sream to read image from
  515. * @param format The format of the picture.
  516. *
  517. * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
  518. * @see Workbook#PICTURE_TYPE_EMF
  519. * @see Workbook#PICTURE_TYPE_WMF
  520. * @see Workbook#PICTURE_TYPE_PICT
  521. * @see Workbook#PICTURE_TYPE_JPEG
  522. * @see Workbook#PICTURE_TYPE_PNG
  523. * @see Workbook#PICTURE_TYPE_DIB
  524. * @see #getAllPictures()
  525. */
  526. public int addPicture(InputStream is, int format) throws IOException {
  527. int imageNumber = getAllPictures().size() + 1;
  528. XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], this.xssfFactory, imageNumber, true).getDocumentPart();
  529. try (OutputStream out = img.getPackagePart().getOutputStream()) {
  530. IOUtils.copy(is, out);
  531. }
  532. pictures.add(img);
  533. return imageNumber - 1;
  534. }
  535. /**
  536. * Create an XSSFSheet from an existing sheet in the XSSFWorkbook.
  537. * The cloned sheet is a deep copy of the original.
  538. *
  539. * @param sheetNum The index of the sheet to clone
  540. * @return XSSFSheet representing the cloned sheet.
  541. * @throws IllegalArgumentException if the sheet index in invalid
  542. * @throws POIXMLException if there were errors when cloning
  543. */
  544. @Override
  545. public XSSFSheet cloneSheet(int sheetNum) {
  546. return cloneSheet(sheetNum, null);
  547. }
  548. @Override
  549. public void close() throws IOException {
  550. try {
  551. super.close();
  552. } finally {
  553. IOUtils.closeQuietly(sharedStringSource);
  554. }
  555. }
  556. /**
  557. * Create an XSSFSheet from an existing sheet in the XSSFWorkbook.
  558. * The cloned sheet is a deep copy of the original but with a new given
  559. * name.
  560. *
  561. * @param sheetNum The index of the sheet to clone
  562. * @param newName The name to set for the newly created sheet
  563. * @return XSSFSheet representing the cloned sheet.
  564. * @throws IllegalArgumentException if the sheet index or the sheet
  565. * name is invalid
  566. * @throws POIXMLException if there were errors when cloning
  567. */
  568. public XSSFSheet cloneSheet(int sheetNum, String newName) {
  569. validateSheetIndex(sheetNum);
  570. XSSFSheet srcSheet = sheets.get(sheetNum);
  571. if (newName == null) {
  572. String srcName = srcSheet.getSheetName();
  573. newName = getUniqueSheetName(srcName);
  574. } else {
  575. validateSheetName(newName);
  576. }
  577. XSSFSheet clonedSheet = createSheet(newName);
  578. // copy sheet's relations
  579. List<RelationPart> rels = srcSheet.getRelationParts();
  580. // if the sheet being cloned has a drawing then remember it and re-create it too
  581. XSSFDrawing dg = null;
  582. for(RelationPart rp : rels) {
  583. POIXMLDocumentPart r = rp.getDocumentPart();
  584. // do not copy the drawing relationship, it will be re-created
  585. if(r instanceof XSSFDrawing) {
  586. dg = (XSSFDrawing)r;
  587. continue;
  588. }
  589. addRelation(rp, clonedSheet);
  590. }
  591. try {
  592. for(PackageRelationship pr : srcSheet.getPackagePart().getRelationships()) {
  593. if (pr.getTargetMode() == TargetMode.EXTERNAL) {
  594. clonedSheet.getPackagePart().addExternalRelationship
  595. (pr.getTargetURI().toASCIIString(), pr.getRelationshipType(), pr.getId());
  596. }
  597. }
  598. } catch (InvalidFormatException e) {
  599. throw new POIXMLException("Failed to clone sheet", e);
  600. }
  601. try (UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream()) {
  602. srcSheet.write(out);
  603. try (InputStream bis = out.toInputStream()) {
  604. clonedSheet.read(bis);
  605. }
  606. } catch (IOException e){
  607. throw new POIXMLException("Failed to clone sheet", e);
  608. }
  609. CTWorksheet ct = clonedSheet.getCTWorksheet();
  610. if(ct.isSetLegacyDrawing()) {
  611. LOG.atWarn().log("Cloning sheets with comments is not yet supported.");
  612. ct.unsetLegacyDrawing();
  613. }
  614. if (ct.isSetPageSetup()) {
  615. LOG.atWarn().log("Cloning sheets with page setup is not yet supported.");
  616. ct.unsetPageSetup();
  617. }
  618. clonedSheet.setSelected(false);
  619. // clone the sheet drawing along with its relationships
  620. if (dg != null) {
  621. if(ct.isSetDrawing()) {
  622. // unset the existing reference to the drawing,
  623. // so that subsequent call of clonedSheet.createDrawingPatriarch() will create a new one
  624. ct.unsetDrawing();
  625. }
  626. XSSFDrawing clonedDg = clonedSheet.createDrawingPatriarch();
  627. // copy drawing contents
  628. clonedDg.getCTDrawing().set(dg.getCTDrawing().copy());
  629. // Clone drawing relations
  630. XSSFDrawing drawingPatriarch = srcSheet.getDrawingPatriarch();
  631. if (drawingPatriarch != null) {
  632. List<RelationPart> srcRels = drawingPatriarch.getRelationParts();
  633. for (RelationPart rp : srcRels) {
  634. POIXMLDocumentPart r = rp.getDocumentPart();
  635. if (r instanceof XSSFChart) {
  636. // Replace chart relation part with new relationship, cloning the chart's content
  637. RelationPart chartPart = clonedDg.createChartRelationPart();
  638. XSSFChart chart = chartPart.getDocumentPart();
  639. chart.importContent((XSSFChart) r);
  640. chart.replaceReferences(clonedSheet);
  641. } else {
  642. addRelation(rp, clonedDg);
  643. }
  644. }
  645. }
  646. }
  647. return clonedSheet;
  648. }
  649. /**
  650. * Modified in POI 5.1.0 to only log issues with unknown relationship types
  651. * - see https://bz.apache.org/bugzilla/show_bug.cgi?id=64759
  652. *
  653. * @since 3.14-Beta1
  654. */
  655. private static boolean addRelation(RelationPart rp, POIXMLDocumentPart target) {
  656. PackageRelationship rel = rp.getRelationship();
  657. if (rel.getTargetMode() == TargetMode.EXTERNAL) {
  658. target.getPackagePart().addRelationship(
  659. rel.getTargetURI(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
  660. } else {
  661. XSSFRelation xssfRel = XSSFRelation.getInstance(rel.getRelationshipType());
  662. if (xssfRel == null) {
  663. // Don't copy all relations blindly, but only the ones we know about
  664. LOG.atWarn().log("Can't clone sheet relationship (some data will be lost in the cloned sheet) - unknown relation type found: {}", rel.getRelationshipType());
  665. return false;
  666. }
  667. target.addRelation(rel.getId(), xssfRel, rp.getDocumentPart());
  668. }
  669. return true;
  670. }
  671. /**
  672. * Generate a valid sheet name based on the existing one. Used when cloning sheets.
  673. *
  674. * @param srcName the original sheet name to
  675. * @return clone sheet name
  676. */
  677. private String getUniqueSheetName(String srcName) {
  678. int uniqueIndex = 2;
  679. String baseName = srcName;
  680. int bracketPos = srcName.lastIndexOf('(');
  681. if (bracketPos > 0 && srcName.endsWith(")")) {
  682. String suffix = srcName.substring(bracketPos + 1, srcName.length() - ")".length());
  683. try {
  684. uniqueIndex = Integer.parseInt(suffix.trim());
  685. uniqueIndex++;
  686. baseName = srcName.substring(0, bracketPos).trim();
  687. } catch (NumberFormatException e) {
  688. // contents of brackets not numeric
  689. }
  690. }
  691. while (true) {
  692. // Try and find the next sheet name that is unique
  693. String index = Integer.toString(uniqueIndex++);
  694. String name;
  695. if (baseName.length() + index.length() + 2 < MAX_SENSITIVE_SHEET_NAME_LEN) {
  696. name = baseName + " (" + index + ")";
  697. } else {
  698. name = baseName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN - index.length() - 2) + "(" + index + ")";
  699. }
  700. //If the sheet name is unique, then set it otherwise move on to the next number.
  701. if (getSheetIndex(name) == -1) {
  702. return name;
  703. }
  704. }
  705. }
  706. /**
  707. * Create a new XSSFCellStyle and add it to the workbook's style table
  708. *
  709. * @return the new XSSFCellStyle object
  710. */
  711. @Override
  712. public XSSFCellStyle createCellStyle() {
  713. return stylesSource.createCellStyle();
  714. }
  715. /**
  716. * Returns the workbook's data format table (a factory for creating data format strings).
  717. *
  718. * @return the XSSFDataFormat object
  719. * @see DataFormat
  720. */
  721. @Override
  722. public XSSFDataFormat createDataFormat() {
  723. if (formatter == null) {
  724. formatter = new XSSFDataFormat(stylesSource);
  725. }
  726. return formatter;
  727. }
  728. /**
  729. * Create a new Font and add it to the workbook's font table
  730. *
  731. * @return new font object
  732. */
  733. @Override
  734. public XSSFFont createFont() {
  735. XSSFFont font = new XSSFFont();
  736. font.registerTo(stylesSource);
  737. return font;
  738. }
  739. @Override
  740. public XSSFName createName() {
  741. CTDefinedName ctName = CTDefinedName.Factory.newInstance();
  742. ctName.setName("");
  743. return createAndStoreName(ctName);
  744. }
  745. private XSSFName createAndStoreName(CTDefinedName ctName) {
  746. XSSFName name = new XSSFName(ctName, this);
  747. namedRanges.add(name);
  748. namedRangesByName.put(ctName.getName().toLowerCase(Locale.ENGLISH), name);
  749. return name;
  750. }
  751. /**
  752. * Create an XSSFSheet for this workbook, adds it to the sheets and returns
  753. * the high level representation. Use this to create new sheets.
  754. *
  755. * @return XSSFSheet representing the new sheet.
  756. */
  757. @Override
  758. public XSSFSheet createSheet() {
  759. String sheetname = "Sheet" + (sheets.size());
  760. int idx = 0;
  761. while(getSheet(sheetname) != null) {
  762. sheetname = "Sheet" + idx;
  763. idx++;
  764. }
  765. return createSheet(sheetname);
  766. }
  767. /**
  768. * Create a new sheet for this Workbook and return the high level representation.
  769. * Use this to create new sheets.
  770. * <p>
  771. * Note that Excel allows sheet names up to 31 chars in length but other applications
  772. * (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars,
  773. * others - truncate such names to 31 character.
  774. * <p>
  775. * POI's SpreadsheetAPI silently truncates the input argument to 31 characters.
  776. * Example:
  777. *
  778. * <pre>{@code
  779. * Sheet sheet = workbook.createSheet("My very long sheet name which is longer than 31 chars"); // will be truncated
  780. * assert 31 == sheet.getSheetName().length();
  781. * assert "My very long sheet name which i" == sheet.getSheetName();
  782. * }</pre>
  783. *
  784. * Except the 31-character constraint, Excel applies some other rules:
  785. * <p>
  786. * Sheet name MUST be unique in the workbook and MUST NOT contain the any of the following characters:
  787. * <ul>
  788. * <li> 0x0000 </li>
  789. * <li> 0x0003 </li>
  790. * <li> colon (:) </li>
  791. * <li> backslash (\) </li>
  792. * <li> asterisk (*) </li>
  793. * <li> question mark (?) </li>
  794. * <li> forward slash (/) </li>
  795. * <li> opening square bracket ([) </li>
  796. * <li> closing square bracket (]) </li>
  797. * </ul>
  798. * The string MUST NOT begin or end with the single quote (') character.
  799. * <p>
  800. * See {@link WorkbookUtil#createSafeSheetName(String nameProposal)}
  801. * for a safe way to create valid names
  802. *
  803. * @param sheetname sheetname to set for the sheet.
  804. * @return Sheet representing the new sheet.
  805. * @throws IllegalArgumentException if the name is null or invalid
  806. * or workbook already contains a sheet with this name
  807. * @see WorkbookUtil#createSafeSheetName(String nameProposal)
  808. */
  809. @Override
  810. public XSSFSheet createSheet(String sheetname) {
  811. if (sheetname == null) {
  812. throw new IllegalArgumentException("sheetName must not be null");
  813. }
  814. validateSheetName(sheetname);
  815. // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
  816. // Issue a WARNING though in order to prevent a situation, where the provided long sheet name is
  817. // not accessible due to the trimming while we are not even aware of the reason and continue to use
  818. // the long name in generated formulas
  819. if(sheetname.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
  820. String trimmedSheetname = sheetname.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
  821. // we still need to warn about the trimming as the original sheet name won't be available
  822. // e.g. when referenced by formulas
  823. LOG.atWarn().log("Sheet '{}' will be added with a trimmed name '{}' for MS Excel compliance.",
  824. sheetname, trimmedSheetname);
  825. sheetname = trimmedSheetname;
  826. }
  827. WorkbookUtil.validateSheetName(sheetname);
  828. CTSheet sheet = addSheet(sheetname);
  829. int sheetNumber = 1;
  830. outerloop:
  831. while(true) {
  832. for(XSSFSheet sh : sheets) {
  833. sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
  834. }
  835. // Bug 57165: We also need to check that the resulting file name is not already taken
  836. // this can happen when moving/cloning sheets
  837. String sheetName = XSSFRelation.WORKSHEET.getFileName(sheetNumber);
  838. for(POIXMLDocumentPart relation : getRelations()) {
  839. if(relation.getPackagePart() != null &&
  840. sheetName.equals(relation.getPackagePart().getPartName().getName())) {
  841. // name is taken => try next one
  842. sheetNumber++;
  843. continue outerloop;
  844. }
  845. }
  846. // no duplicate found => use this one
  847. break;
  848. }
  849. RelationPart rp = createRelationship(XSSFRelation.WORKSHEET, this.xssfFactory, sheetNumber, false);
  850. XSSFSheet wrapper = rp.getDocumentPart();
  851. wrapper.sheet = sheet;
  852. sheet.setId(rp.getRelationship().getId());
  853. sheet.setSheetId(sheetNumber);
  854. if (sheets.isEmpty()) {
  855. wrapper.setSelected(true);
  856. }
  857. sheets.add(wrapper);
  858. return wrapper;
  859. }
  860. private void validateSheetName(final String sheetName) throws IllegalArgumentException {
  861. if (containsSheet( sheetName, sheets.size() )) {
  862. throw new IllegalArgumentException("The workbook already contains a sheet named '" + sheetName + "'");
  863. }
  864. }
  865. protected XSSFDialogsheet createDialogsheet(String sheetname, CTDialogsheet dialogsheet) {
  866. XSSFSheet sheet = createSheet(sheetname);
  867. return new XSSFDialogsheet(sheet);
  868. }
  869. private CTSheet addSheet(String sheetname) {
  870. CTSheet sheet = workbook.getSheets().addNewSheet();
  871. sheet.setName(sheetname);
  872. return sheet;
  873. }
  874. /**
  875. * Finds a font that matches the one with the supplied attributes
  876. *
  877. * @return the font with the matched attributes or {@code null}
  878. */
  879. @Override
  880. public XSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
  881. return stylesSource.findFont(bold, color, fontHeight, name, italic, strikeout, typeOffset, underline);
  882. }
  883. /**
  884. * Convenience method to get the active sheet. The active sheet is is the sheet
  885. * which is currently displayed when the workbook is viewed in Excel.
  886. * 'Selected' sheet(s) is a distinct concept.
  887. */
  888. @Override
  889. public int getActiveSheetIndex() {
  890. //activeTab (Active Sheet Index) Specifies an unsignedInt
  891. //that contains the index to the active sheet in this book view.
  892. return (int)workbook.getBookViews().getWorkbookViewArray(0).getActiveTab();
  893. }
  894. /**
  895. * Gets all pictures from the Workbook.
  896. *
  897. * @return the list of pictures (a list of {@link XSSFPictureData} objects.)
  898. * @see #addPicture(byte[], int)
  899. */
  900. @Override
  901. public List<XSSFPictureData> getAllPictures() {
  902. if(pictures == null){
  903. List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
  904. pictures = new ArrayList<>(mediaParts.size());
  905. for(PackagePart part : mediaParts){
  906. pictures.add(new XSSFPictureData(part));
  907. }
  908. }
  909. return pictures; //YK: should return Collections.unmodifiableList(pictures);
  910. }
  911. /**
  912. * Get the cell style object at the given index
  913. *
  914. * @param idx index within the set of styles
  915. * @return XSSFCellStyle object at the index
  916. */
  917. @Override
  918. public XSSFCellStyle getCellStyleAt(int idx) {
  919. return stylesSource.getStyleAt(idx);
  920. }
  921. @Override
  922. public XSSFFont getFontAt(int idx) {
  923. return stylesSource.getFontAt(idx);
  924. }
  925. /**
  926. * Get the first named range with the given name.
  927. *
  928. * Note: names of named ranges are not unique as they are scoped by sheet.
  929. * {@link #getNames(String name)} returns all named ranges with the given name.
  930. *
  931. * @param name named range name
  932. * @return XSSFName with the given name.
  933. * {@code null} is returned no named range could be found.
  934. */
  935. @Override
  936. public XSSFName getName(String name) {
  937. Collection<XSSFName> list = getNames(name);
  938. if (list.isEmpty()) {
  939. return null;
  940. }
  941. return list.iterator().next();
  942. }
  943. /**
  944. * Get the named ranges with the given name.
  945. * <i>Note:</i>Excel named ranges are case-insensitive and
  946. * this method performs a case-insensitive search.
  947. *
  948. * @param name named range name
  949. * @return list of XSSFNames with the given name. An empty list if no named ranges could be found
  950. */
  951. @Override
  952. public List<XSSFName> getNames(String name) {
  953. return Collections.unmodifiableList(namedRangesByName.get(name.toLowerCase(Locale.ENGLISH)));
  954. }
  955. /**
  956. * Get the named range at the given index. No longer public and only used in tests.
  957. *
  958. * @param nameIndex the index of the named range
  959. * @return the XSSFName at the given index
  960. */
  961. @Deprecated
  962. XSSFName getNameAt(int nameIndex) {
  963. int nNames = namedRanges.size();
  964. if (nNames < 1) {
  965. throw new IllegalStateException("There are no defined names in this workbook");
  966. }
  967. if (nameIndex < 0 || nameIndex > nNames) {
  968. throw new IllegalArgumentException("Specified name index " + nameIndex
  969. + " is outside the allowable range (0.." + (nNames-1) + ").");
  970. }
  971. return namedRanges.get(nameIndex);
  972. }
  973. /**
  974. * Get a list of all the named ranges in the workbook.
  975. *
  976. * @return list of XSSFNames in the workbook
  977. */
  978. @Override
  979. public List<XSSFName> getAllNames() {
  980. return Collections.unmodifiableList(namedRanges);
  981. }
  982. /**
  983. * Gets the named range index by name. No longer public and only used in tests.
  984. *
  985. * @param name named range name
  986. * @return named range index. {@code -1} is returned if no named ranges could be found.
  987. *
  988. * @deprecated 3.16. New projects should avoid accessing named ranges by index.
  989. * Use {@link #getName(String)} instead.
  990. */
  991. @Deprecated
  992. int getNameIndex(String name) {
  993. XSSFName nm = getName(name);
  994. if (nm != null) {
  995. return namedRanges.indexOf(nm);
  996. }
  997. return -1;
  998. }
  999. /**
  1000. * Get the number of styles the workbook contains
  1001. *
  1002. * @return count of cell styles
  1003. */
  1004. @Override
  1005. public int getNumCellStyles() {
  1006. return stylesSource.getNumCellStyles();
  1007. }
  1008. @Override
  1009. public int getNumberOfFonts() {
  1010. return stylesSource.getFonts().size();
  1011. }
  1012. @Override
  1013. @Deprecated
  1014. @Removal(version = "6.0.0")
  1015. public int getNumberOfFontsAsInt() {
  1016. return getNumberOfFonts();
  1017. }
  1018. /**
  1019. * Get the number of named ranges in the this workbook
  1020. *
  1021. * @return number of named ranges
  1022. */
  1023. @Override
  1024. public int getNumberOfNames() {
  1025. return namedRanges.size();
  1026. }
  1027. /**
  1028. * Get the number of worksheets in the this workbook
  1029. *
  1030. * @return number of worksheets
  1031. */
  1032. @Override
  1033. public int getNumberOfSheets() {
  1034. return sheets.size();
  1035. }
  1036. /**
  1037. * Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.
  1038. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  1039. * @return String Null if no print area has been defined
  1040. */
  1041. @Override
  1042. public String getPrintArea(int sheetIndex) {
  1043. XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
  1044. if (name == null) {
  1045. return null;
  1046. }
  1047. //adding one here because 0 indicates a global named region; doesnt make sense for print areas
  1048. return name.getRefersToFormula();
  1049. }
  1050. /**
  1051. * Get sheet with the given name (case insensitive match)
  1052. *
  1053. * @param name of the sheet
  1054. * @return XSSFSheet with the name provided or {@code null} if it does not exist
  1055. */
  1056. @Override
  1057. public XSSFSheet getSheet(String name) {
  1058. for (XSSFSheet sheet : sheets) {
  1059. if (name.equalsIgnoreCase(sheet.getSheetName())) {
  1060. return sheet;
  1061. }
  1062. }
  1063. return null;
  1064. }
  1065. /**
  1066. * Get the XSSFSheet object at the given index.
  1067. *
  1068. * @param index of the sheet number (0-based physical &amp; logical)
  1069. * @return XSSFSheet at the provided index
  1070. * @throws IllegalArgumentException if the index is out of range (index
  1071. * &lt; 0 || index &gt;= getNumberOfSheets()).
  1072. */
  1073. @Override
  1074. public XSSFSheet getSheetAt(int index) {
  1075. validateSheetIndex(index);
  1076. return sheets.get(index);
  1077. }
  1078. /**
  1079. * Returns the index of the sheet by his name (case insensitive match)
  1080. *
  1081. * @param name the sheet name
  1082. * @return index of the sheet (0 based) or {@code -1} if not found
  1083. */
  1084. @Override
  1085. public int getSheetIndex(String name) {
  1086. int idx = 0;
  1087. for (XSSFSheet sh : sheets) {
  1088. if (name.equalsIgnoreCase(sh.getSheetName())) {
  1089. return idx;
  1090. }
  1091. idx++;
  1092. }
  1093. return -1;
  1094. }
  1095. /**
  1096. * Returns the index of the given sheet
  1097. *
  1098. * @param sheet the sheet to look up
  1099. * @return index of the sheet (0 based). {@code -1} if not found
  1100. */
  1101. @Override
  1102. public int getSheetIndex(Sheet sheet) {
  1103. int idx = 0;
  1104. for(XSSFSheet sh : sheets){
  1105. if(sh == sheet) {
  1106. return idx;
  1107. }
  1108. idx++;
  1109. }
  1110. return -1;
  1111. }
  1112. /**
  1113. * Get the sheet name
  1114. *
  1115. * @param sheetIx Number
  1116. * @return Sheet name
  1117. */
  1118. @Override
  1119. public String getSheetName(int sheetIx) {
  1120. validateSheetIndex(sheetIx);
  1121. return sheets.get(sheetIx).getSheetName();
  1122. }
  1123. /**
  1124. * Returns an iterator of the sheets in the workbook
  1125. * in sheet order. Includes hidden and very hidden sheets.
  1126. *
  1127. * Note: remove() is not supported on this iterator.
  1128. * Use {@link #removeSheetAt(int)} to remove sheets instead.
  1129. *
  1130. * @return an iterator of the sheets.
  1131. */
  1132. @Override
  1133. public Iterator<Sheet> sheetIterator() {
  1134. return new SheetIterator<>();
  1135. }
  1136. /**
  1137. * Alias for {@link #sheetIterator()} to allow
  1138. * foreach loops
  1139. *
  1140. * Note: remove() is not supported on this iterator.
  1141. * Use {@link #removeSheetAt(int)} to remove sheets instead.
  1142. *
  1143. * @return an iterator of the sheets.
  1144. */
  1145. @Override
  1146. public Iterator<Sheet> iterator() {
  1147. return sheetIterator();
  1148. }
  1149. /**
  1150. * Returns a spliterator of the sheets in the workbook
  1151. * in sheet order. Includes hidden and very hidden sheets.
  1152. *
  1153. * @return a spliterator of the sheets.
  1154. *
  1155. * @since POI 5.2.0
  1156. */
  1157. @Override
  1158. @SuppressWarnings("unchecked")
  1159. public Spliterator<Sheet> spliterator() {
  1160. return (Spliterator<Sheet>)(Spliterator<? extends Sheet>) sheets.spliterator();
  1161. }
  1162. private final class SheetIterator<T extends Sheet> implements Iterator<T> {
  1163. final private Iterator<T> it;
  1164. @SuppressWarnings("unchecked")
  1165. public SheetIterator() {
  1166. it = (Iterator<T>) sheets.iterator();
  1167. }
  1168. @Override
  1169. public boolean hasNext() {
  1170. return it.hasNext();
  1171. }
  1172. @Override
  1173. public T next() throws NoSuchElementException {
  1174. return it.next();
  1175. }
  1176. /**
  1177. * Unexpected behavior may occur if sheets are reordered after iterator
  1178. * has been created. Support for the remove method may be added in the future
  1179. * if someone can figure out a reliable implementation.
  1180. */
  1181. @Override
  1182. public void remove() throws IllegalStateException {
  1183. throw new UnsupportedOperationException("remove method not supported on XSSFWorkbook.iterator(). "+
  1184. "Use Sheet.removeSheetAt(int) instead.");
  1185. }
  1186. }
  1187. /**
  1188. * Are we a normal workbook (.xlsx), or a
  1189. * macro enabled workbook (.xlsm)?
  1190. */
  1191. public boolean isMacroEnabled() {
  1192. return getPackagePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
  1193. }
  1194. /**
  1195. * @param name the name to remove.
  1196. *
  1197. * @throws IllegalArgumentException if the named range is not a part of this XSSFWorkbook
  1198. */
  1199. @Override
  1200. public void removeName(Name name) {
  1201. if (!namedRangesByName.removeMapping(name.getNameName().toLowerCase(Locale.ENGLISH), name)
  1202. || !namedRanges.remove(name)) {
  1203. throw new IllegalArgumentException("Name was not found: " + name);
  1204. }
  1205. }
  1206. void updateName(XSSFName name, String oldName) {
  1207. if (!namedRangesByName.removeMapping(oldName.toLowerCase(Locale.ENGLISH), name)) {
  1208. throw new IllegalArgumentException("Name was not found: " + name);
  1209. }
  1210. namedRangesByName.put(name.getNameName().toLowerCase(Locale.ENGLISH), name);
  1211. }
  1212. /**
  1213. * Delete the printarea for the sheet specified
  1214. *
  1215. * @param sheetIndex 0-based sheet index (0 = First Sheet)
  1216. */
  1217. @Override
  1218. public void removePrintArea(int sheetIndex) {
  1219. XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
  1220. if (name != null) {
  1221. removeName(name);
  1222. }
  1223. }
  1224. /**
  1225. * Removes sheet at the given index.<p>
  1226. *
  1227. * Care must be taken if the removed sheet is the currently active or only selected sheet in
  1228. * the workbook. There are a few situations when Excel must have a selection and/or active
  1229. * sheet. (For example when printing - see Bug 40414).<br>
  1230. *
  1231. * This method makes sure that if the removed sheet was active, another sheet will become
  1232. * active in its place. Furthermore, if the removed sheet was the only selected sheet, another
  1233. * sheet will become selected. The newly active/selected sheet will have the same index, or
  1234. * one less if the removed sheet was the last in the workbook.
  1235. *
  1236. * @param index of the sheet (0-based)
  1237. */
  1238. @Override
  1239. public void removeSheetAt(int index) {
  1240. validateSheetIndex(index);
  1241. onSheetDelete(index);
  1242. XSSFSheet sheet = getSheetAt(index);
  1243. removeRelation(sheet);
  1244. sheets.remove(index);
  1245. // only set new sheet if there are still some left
  1246. if(sheets.size() == 0) {
  1247. return;
  1248. }
  1249. // the index of the closest remaining sheet to the one just deleted
  1250. int newSheetIndex = index;
  1251. if (newSheetIndex >= sheets.size()) {
  1252. newSheetIndex = sheets.size()-1;
  1253. }
  1254. // adjust active sheet
  1255. int active = getActiveSheetIndex();
  1256. if(active == index) {
  1257. // removed sheet was the active one, reset active sheet if there is still one left now
  1258. setActiveSheet(newSheetIndex);
  1259. } else if (active > index) {
  1260. // removed sheet was below the active one => active is one less now
  1261. setActiveSheet(active-1);
  1262. }
  1263. }
  1264. /**
  1265. * Gracefully remove references to the sheet being deleted
  1266. *
  1267. * @param index the 0-based index of the sheet to delete
  1268. */
  1269. private void onSheetDelete(int index) {
  1270. // remove all sheet relations
  1271. final XSSFSheet sheet = getSheetAt(index);
  1272. sheet.onSheetDelete();
  1273. //delete the CTSheet reference from workbook.xml
  1274. workbook.getSheets().removeSheet(index);
  1275. //calculation chain is auxiliary, remove it as it may contain orphan references to deleted cells
  1276. if(calcChain != null) {
  1277. removeRelation(calcChain);
  1278. calcChain = null;
  1279. }
  1280. //adjust indices of names ranges
  1281. List<XSSFName> toRemove = new ArrayList<>();
  1282. for (XSSFName nm : namedRanges) {
  1283. CTDefinedName ct = nm.getCTName();
  1284. if(!ct.isSetLocalSheetId()) {
  1285. continue;
  1286. }
  1287. if (ct.getLocalSheetId() == index) {
  1288. toRemove.add(nm);
  1289. } else if (ct.getLocalSheetId() > index){
  1290. // Bump down by one, so still points at the same sheet
  1291. ct.setLocalSheetId(ct.getLocalSheetId()-1);
  1292. }
  1293. }
  1294. for (XSSFName nm : toRemove) {
  1295. removeName(nm);
  1296. }
  1297. }
  1298. /**
  1299. * Retrieves the current policy on what to do when
  1300. * getting missing or blank cells from a row.
  1301. * The default is to return blank and null cells.
  1302. * {@link MissingCellPolicy}
  1303. */
  1304. @Override
  1305. public MissingCellPolicy getMissingCellPolicy() {
  1306. return _missingCellPolicy;
  1307. }
  1308. /**
  1309. * Sets the policy on what to do when
  1310. * getting missing or blank cells from a row.
  1311. * This will then apply to all calls to
  1312. * {@link Row#getCell(int)}}. See
  1313. * {@link MissingCellPolicy}
  1314. */
  1315. @Override
  1316. public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
  1317. _missingCellPolicy = missingCellPolicy;
  1318. }
  1319. /**
  1320. * Convenience method to set the active sheet. The active sheet is is the sheet
  1321. * which is currently displayed when the workbook is viewed in Excel.
  1322. * 'Selected' sheet(s) is a distinct concept.
  1323. */
  1324. @Override
  1325. public void setActiveSheet(int index) {
  1326. validateSheetIndex(index);
  1327. for (CTBookView arrayBook : workbook.getBookViews().getWorkbookViewArray()) {
  1328. arrayBook.setActiveTab(index);
  1329. }
  1330. }
  1331. /**
  1332. * Validate sheet index
  1333. *
  1334. * @param index the index to validate
  1335. * @throws IllegalArgumentException if the index is out of range (index
  1336. * &lt; 0 || index &gt;= getNumberOfSheets()).
  1337. */
  1338. private void validateSheetIndex(int index) {
  1339. int lastSheetIx = sheets.size() - 1;
  1340. if (index < 0 || index > lastSheetIx) {
  1341. String range = "(0.." + lastSheetIx + ")";
  1342. if (lastSheetIx == -1) {
  1343. range = "(no sheets)";
  1344. }
  1345. throw new IllegalArgumentException("Sheet index ("
  1346. + index +") is out of range " + range);
  1347. }
  1348. }
  1349. /**
  1350. * Gets the first tab that is displayed in the list of tabs in excel.
  1351. *
  1352. * @return integer that contains the index to the active sheet in this book view.
  1353. */
  1354. @Override
  1355. public int getFirstVisibleTab() {
  1356. CTBookViews bookViews = workbook.getBookViews();
  1357. CTBookView bookView = bookViews.getWorkbookViewArray(0);
  1358. return (short) bookView.getFirstSheet();
  1359. }
  1360. /**
  1361. * Sets the first tab that is displayed in the list of tabs in excel.
  1362. *
  1363. * @param index integer that contains the index to the active sheet in this book view.
  1364. */
  1365. @Override
  1366. public void setFirstVisibleTab(int index) {
  1367. CTBookViews bookViews = workbook.getBookViews();
  1368. CTBookView bookView= bookViews.getWorkbookViewArray(0);
  1369. bookView.setFirstSheet(index);
  1370. }
  1371. /**
  1372. * Sets the printarea for the sheet provided
  1373. * <p>
  1374. * i.e. Reference = $A$1:$B$2
  1375. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  1376. * @param reference Valid name Reference for the Print Area
  1377. */
  1378. @Override
  1379. public void setPrintArea(int sheetIndex, String reference) {
  1380. XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
  1381. if (name == null) {
  1382. name = createBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
  1383. }
  1384. //short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex);
  1385. //name.setExternSheetNumber(externSheetIndex);
  1386. String[] parts = COMMA_PATTERN.split(reference);
  1387. StringBuilder sb = new StringBuilder(32);
  1388. for (int i = 0; i < parts.length; i++) {
  1389. if(i>0) {
  1390. sb.append(',');
  1391. }
  1392. SheetNameFormatter.appendFormat(sb, getSheetName(sheetIndex));
  1393. sb.append('!');
  1394. sb.append(parts[i]);
  1395. }
  1396. name.setRefersToFormula(sb.toString());
  1397. }
  1398. /**
  1399. * For the Convenience of Java Programmers maintaining pointers.
  1400. * @see #setPrintArea(int, String)
  1401. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  1402. * @param startColumn Column to begin printarea
  1403. * @param endColumn Column to end the printarea
  1404. * @param startRow Row to begin the printarea
  1405. * @param endRow Row to end the printarea
  1406. */
  1407. @Override
  1408. public void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) {
  1409. String reference=getReferencePrintArea(getSheetName(sheetIndex), startColumn, endColumn, startRow, endRow);
  1410. setPrintArea(sheetIndex, reference);
  1411. }
  1412. private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) {
  1413. //windows excel example: Sheet1!$C$3:$E$4
  1414. CellReference colRef = new CellReference(sheetName, startR, startC, true, true);
  1415. CellReference colRef2 = new CellReference(sheetName, endR, endC, true, true);
  1416. return "$" + colRef.getCellRefParts()[2] + "$" + colRef.getCellRefParts()[1] + ":$" + colRef2.getCellRefParts()[2] + "$" + colRef2.getCellRefParts()[1];
  1417. }
  1418. XSSFName getBuiltInName(String builtInCode, int sheetNumber) {
  1419. for (XSSFName name : namedRangesByName.get(builtInCode.toLowerCase(Locale.ENGLISH))) {
  1420. if (name.getSheetIndex() == sheetNumber) {
  1421. return name;
  1422. }
  1423. }
  1424. return null;
  1425. }
  1426. /**
  1427. * Generates a NameRecord to represent a built-in region
  1428. *
  1429. * @return a new NameRecord
  1430. * @throws IllegalArgumentException if sheetNumber is invalid
  1431. * @throws POIXMLException if such a name already exists in the workbook
  1432. */
  1433. XSSFName createBuiltInName(String builtInName, int sheetNumber) {
  1434. validateSheetIndex(sheetNumber);
  1435. CTDefinedNames names = workbook.getDefinedNames() == null ? workbook.addNewDefinedNames() : workbook.getDefinedNames();
  1436. CTDefinedName nameRecord = names.addNewDefinedName();
  1437. nameRecord.setName(builtInName);
  1438. nameRecord.setLocalSheetId(sheetNumber);
  1439. if (getBuiltInName(builtInName, sheetNumber) != null) {
  1440. throw new POIXMLException("Builtin (" + builtInName
  1441. + ") already exists for sheet (" + sheetNumber + ")");
  1442. }
  1443. return createAndStoreName(nameRecord);
  1444. }
  1445. /**
  1446. * We only set one sheet as selected for compatibility with HSSF.
  1447. */
  1448. @Override
  1449. public void setSelectedTab(int index) {
  1450. int idx = 0;
  1451. for (XSSFSheet sh : sheets) {
  1452. sh.setSelected(idx == index);
  1453. idx++;
  1454. }
  1455. }
  1456. /**
  1457. * Set the sheet name.
  1458. *
  1459. * @param sheetIndex sheet number (0 based)
  1460. * @param sheetname the new sheet name
  1461. * @throws IllegalArgumentException if the name is null or invalid
  1462. * or workbook already contains a sheet with this name
  1463. * @see #createSheet(String)
  1464. * @see WorkbookUtil#createSafeSheetName(String nameProposal)
  1465. */
  1466. @Override
  1467. public void setSheetName(int sheetIndex, String sheetname) {
  1468. if (sheetname == null) {
  1469. throw new IllegalArgumentException( "sheetName must not be null" );
  1470. }
  1471. validateSheetIndex(sheetIndex);
  1472. String oldSheetName = getSheetName(sheetIndex);
  1473. // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
  1474. if(sheetname.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
  1475. sheetname = sheetname.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
  1476. }
  1477. WorkbookUtil.validateSheetName(sheetname);
  1478. // Do nothing if no change
  1479. if (sheetname.equals(oldSheetName)) {
  1480. return;
  1481. }
  1482. // Check it isn't already taken
  1483. if (containsSheet(sheetname, sheetIndex )) {
  1484. throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
  1485. }
  1486. // Update references to the name
  1487. XSSFFormulaUtils utils = new XSSFFormulaUtils(this);
  1488. utils.updateSheetName(sheetIndex, oldSheetName, sheetname);
  1489. workbook.getSheets().getSheetArray(sheetIndex).setName(sheetname);
  1490. }
  1491. /**
  1492. * sets the order of appearance for a given sheet.
  1493. *
  1494. * @param sheetname the name of the sheet to reorder
  1495. * @param pos the position that we want to insert the sheet into (0 based)
  1496. */
  1497. @Override
  1498. public void setSheetOrder(String sheetname, int pos) {
  1499. int idx = getSheetIndex(sheetname);
  1500. sheets.add(pos, sheets.remove(idx));
  1501. // Reorder CTSheets
  1502. CTSheets ct = workbook.getSheets();
  1503. XmlObject cts = ct.getSheetArray(idx).copy();
  1504. workbook.getSheets().removeSheet(idx);
  1505. CTSheet newcts = ct.insertNewSheet(pos);
  1506. newcts.set(cts);
  1507. //notify sheets
  1508. CTSheet[] sheetArray = ct.getSheetArray();
  1509. for(int i=0; i < sheetArray.length; i++) {
  1510. sheets.get(i).sheet = sheetArray[i];
  1511. }
  1512. updateNamedRangesAfterSheetReorder(idx, pos);
  1513. updateActiveSheetAfterSheetReorder(idx, pos);
  1514. }
  1515. /**
  1516. * update sheet-scoped named ranges in this workbook after changing the sheet order
  1517. * of a sheet at oldIndex to newIndex.
  1518. * Sheets between these indices will move left or right by 1.
  1519. *
  1520. * @param oldIndex the original index of the re-ordered sheet
  1521. * @param newIndex the new index of the re-ordered sheet
  1522. */
  1523. private void updateNamedRangesAfterSheetReorder(int oldIndex, int newIndex) {
  1524. // update sheet index of sheet-scoped named ranges
  1525. for (final XSSFName name : namedRanges) {
  1526. final int i = name.getSheetIndex();
  1527. // name has sheet-level scope
  1528. if (i != -1) {
  1529. // name refers to this sheet
  1530. if (i == oldIndex) {
  1531. name.setSheetIndex(newIndex);
  1532. }
  1533. // if oldIndex > newIndex then this sheet moved left and sheets between newIndex and oldIndex moved right
  1534. else if (newIndex <= i && i < oldIndex) {
  1535. name.setSheetIndex(i+1);
  1536. }
  1537. // if oldIndex < newIndex then this sheet moved right and sheets between oldIndex and newIndex moved left
  1538. else if (oldIndex < i && i <= newIndex) {
  1539. name.setSheetIndex(i-1);
  1540. }
  1541. }
  1542. }
  1543. }
  1544. private void updateActiveSheetAfterSheetReorder(int oldIndex, int newIndex) {
  1545. // adjust active sheet if necessary
  1546. int active = getActiveSheetIndex();
  1547. if(active == oldIndex) {
  1548. // moved sheet was the active one
  1549. setActiveSheet(newIndex);
  1550. } else if ((active < oldIndex && active < newIndex) ||
  1551. (active > oldIndex && active > newIndex)) {
  1552. // not affected
  1553. } else if (newIndex > oldIndex) {
  1554. // moved sheet was below before and is above now => active is one less
  1555. setActiveSheet(active-1);
  1556. } else {
  1557. // remaining case: moved sheet was higher than active before and is lower now => active is one more
  1558. setActiveSheet(active+1);
  1559. }
  1560. }
  1561. /**
  1562. * marshal named ranges from the {@link #namedRanges} collection to the underlying CTWorkbook bean
  1563. */
  1564. private void saveNamedRanges(){
  1565. // Named ranges
  1566. if(namedRanges.size() > 0) {
  1567. CTDefinedNames names = CTDefinedNames.Factory.newInstance();
  1568. CTDefinedName[] nr = new CTDefinedName[namedRanges.size()];
  1569. int i = 0;
  1570. for(XSSFName name : namedRanges) {
  1571. nr[i] = name.getCTName();
  1572. i++;
  1573. }
  1574. names.setDefinedNameArray(nr);
  1575. if(workbook.isSetDefinedNames()) {
  1576. workbook.unsetDefinedNames();
  1577. }
  1578. workbook.setDefinedNames(names);
  1579. // Re-process the named ranges
  1580. reprocessNamedRanges();
  1581. } else {
  1582. if(workbook.isSetDefinedNames()) {
  1583. workbook.unsetDefinedNames();
  1584. }
  1585. }
  1586. }
  1587. private void reprocessNamedRanges() {
  1588. namedRangesByName = new ArrayListValuedHashMap<>();
  1589. namedRanges = new ArrayList<>();
  1590. if(workbook.isSetDefinedNames()) {
  1591. for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
  1592. createAndStoreName(ctName);
  1593. }
  1594. }
  1595. }
  1596. private void saveCalculationChain(){
  1597. if(calcChain != null){
  1598. int count = calcChain.getCTCalcChain().sizeOfCArray();
  1599. if(count == 0){
  1600. removeRelation(calcChain);
  1601. calcChain = null;
  1602. }
  1603. }
  1604. }
  1605. @Override
  1606. protected void commit() throws IOException {
  1607. saveNamedRanges();
  1608. saveCalculationChain();
  1609. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  1610. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
  1611. PackagePart part = getPackagePart();
  1612. try (OutputStream out = part.getOutputStream()) {
  1613. workbook.save(out, xmlOptions);
  1614. }
  1615. }
  1616. /**
  1617. * Returns SharedStringsTable - tha cache of string for this workbook
  1618. *
  1619. * @return the shared string table
  1620. */
  1621. @Internal
  1622. public SharedStringsTable getSharedStringSource() {
  1623. return this.sharedStringSource;
  1624. }
  1625. /**
  1626. * Return a object representing a collection of shared objects used for styling content,
  1627. * e.g. fonts, cell styles, colors, etc.
  1628. */
  1629. public StylesTable getStylesSource() {
  1630. return this.stylesSource;
  1631. }
  1632. /**
  1633. * Returns the Theme of current workbook.
  1634. */
  1635. public ThemesTable getTheme() {
  1636. if (stylesSource == null) {
  1637. return null;
  1638. }
  1639. return stylesSource.getTheme();
  1640. }
  1641. /**
  1642. * Returns an object that handles instantiating concrete
  1643. * classes of the various instances for XSSF.
  1644. */
  1645. @Override
  1646. public XSSFCreationHelper getCreationHelper() {
  1647. if(_creationHelper == null) {
  1648. _creationHelper = new XSSFCreationHelper(this);
  1649. }
  1650. return _creationHelper;
  1651. }
  1652. /**
  1653. * Determines whether a workbook contains the provided sheet name.
  1654. * For the purpose of comparison, long names are truncated to 31 chars.
  1655. *
  1656. * @param name the name to test (case insensitive match)
  1657. * @param excludeSheetIdx the sheet to exclude from the check or -1 to include all sheets in the check.
  1658. * @return true if the sheet contains the name, false otherwise.
  1659. */
  1660. private boolean containsSheet(String name, int excludeSheetIdx) {
  1661. CTSheet[] ctSheetArray = workbook.getSheets().getSheetArray();
  1662. if (name.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
  1663. name = name.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
  1664. }
  1665. for (int i = 0; i < ctSheetArray.length; i++) {
  1666. String ctName = ctSheetArray[i].getName();
  1667. if (ctName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
  1668. ctName = ctName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
  1669. }
  1670. if (excludeSheetIdx != i && name.equalsIgnoreCase(ctName)) {
  1671. return true;
  1672. }
  1673. }
  1674. return false;
  1675. }
  1676. /**
  1677. * Gets a boolean value that indicates whether the date systems used in the workbook starts in 1904.
  1678. * <p>
  1679. * The default value is false, meaning that the workbook uses the 1900 date system,
  1680. * where 1/1/1900 is the first day in the system..
  1681. * </p>
  1682. * @return true if the date systems used in the workbook starts in 1904
  1683. */
  1684. @Internal
  1685. @Override
  1686. public boolean isDate1904() {
  1687. CTWorkbookPr workbookPr = workbook.getWorkbookPr();
  1688. return workbookPr != null && workbookPr.getDate1904();
  1689. }
  1690. /**
  1691. * Get the document's embedded files.
  1692. */
  1693. @Override
  1694. public List<PackagePart> getAllEmbeddedParts() throws OpenXML4JException {
  1695. List<PackagePart> embedds = new LinkedList<>();
  1696. for(XSSFSheet sheet : sheets){
  1697. // Get the embeddings for the workbook
  1698. for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation())) {
  1699. embedds.add( sheet.getPackagePart().getRelatedPart(rel) );
  1700. }
  1701. for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation())) {
  1702. embedds.add( sheet.getPackagePart().getRelatedPart(rel) );
  1703. }
  1704. }
  1705. return embedds;
  1706. }
  1707. @Override
  1708. @NotImplemented
  1709. public boolean isHidden() {
  1710. throw new RuntimeException("Not implemented yet");
  1711. }
  1712. @Override
  1713. @NotImplemented
  1714. public void setHidden(boolean hiddenFlag) {
  1715. throw new RuntimeException("Not implemented yet");
  1716. }
  1717. @Override
  1718. public boolean isSheetHidden(int sheetIx) {
  1719. validateSheetIndex(sheetIx);
  1720. CTSheet ctSheet = sheets.get(sheetIx).sheet;
  1721. return ctSheet.getState() == STSheetState.HIDDEN;
  1722. }
  1723. @Override
  1724. public boolean isSheetVeryHidden(int sheetIx) {
  1725. validateSheetIndex(sheetIx);
  1726. CTSheet ctSheet = sheets.get(sheetIx).sheet;
  1727. return ctSheet.getState() == STSheetState.VERY_HIDDEN;
  1728. }
  1729. @Override
  1730. public SheetVisibility getSheetVisibility(int sheetIx) {
  1731. validateSheetIndex(sheetIx);
  1732. final CTSheet ctSheet = sheets.get(sheetIx).sheet;
  1733. final STSheetState.Enum state = ctSheet.getState();
  1734. if (state == STSheetState.VISIBLE) {
  1735. return SheetVisibility.VISIBLE;
  1736. }
  1737. if (state == STSheetState.HIDDEN) {
  1738. return SheetVisibility.HIDDEN;
  1739. }
  1740. if (state == STSheetState.VERY_HIDDEN) {
  1741. return SheetVisibility.VERY_HIDDEN;
  1742. }
  1743. throw new IllegalArgumentException("This should never happen");
  1744. }
  1745. @Override
  1746. public void setSheetHidden(int sheetIx, boolean hidden) {
  1747. setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
  1748. }
  1749. @Override
  1750. public void setSheetVisibility(int sheetIx, SheetVisibility visibility) {
  1751. validateSheetIndex(sheetIx);
  1752. final CTSheet ctSheet = sheets.get(sheetIx).sheet;
  1753. switch (visibility) {
  1754. case VISIBLE:
  1755. ctSheet.setState(STSheetState.VISIBLE);
  1756. break;
  1757. case HIDDEN:
  1758. ctSheet.setState(STSheetState.HIDDEN);
  1759. break;
  1760. case VERY_HIDDEN:
  1761. ctSheet.setState(STSheetState.VERY_HIDDEN);
  1762. break;
  1763. default:
  1764. throw new IllegalArgumentException("This should never happen");
  1765. }
  1766. }
  1767. /**
  1768. * Fired when a formula is deleted from this workbook,
  1769. * for example when calling cell.setCellFormula(null)
  1770. *
  1771. * @see XSSFCell#setCellFormula(String)
  1772. */
  1773. protected void onDeleteFormula(XSSFCell cell){
  1774. if(calcChain != null) {
  1775. int sheetId = (int)cell.getSheet().sheet.getSheetId();
  1776. calcChain.removeItem(sheetId, cell.getReference());
  1777. }
  1778. }
  1779. /**
  1780. * Return the {@link CalculationChain} object for this workbook
  1781. * <p>
  1782. * The calculation chain object specifies the order in which the cells in a workbook were last calculated
  1783. * </p>
  1784. *
  1785. * @return the {@code CalculationChain} object or {@code null} if not defined
  1786. */
  1787. @Internal
  1788. public CalculationChain getCalculationChain() {
  1789. return calcChain;
  1790. }
  1791. /**
  1792. * Returns the list of {@link ExternalLinksTable} object for this workbook
  1793. *
  1794. * <p>The external links table specifies details of named ranges etc
  1795. * that are referenced from other workbooks, along with the last seen
  1796. * values of what they point to.</p>
  1797. *
  1798. * <p>Note that Excel uses index 0 for the current workbook, so the first
  1799. * External Links in a formula would be '[1]Foo' which corresponds to
  1800. * entry 0 in this list.</p>
  1801. * @return the {@code ExternalLinksTable} list, which may be empty
  1802. */
  1803. @Internal
  1804. public List<ExternalLinksTable> getExternalLinksTable() {
  1805. return externalLinks;
  1806. }
  1807. /**
  1808. *
  1809. * @return a collection of custom XML mappings defined in this workbook
  1810. */
  1811. public Collection<XSSFMap> getCustomXMLMappings(){
  1812. return mapInfo == null ? new ArrayList<>() : mapInfo.getAllXSSFMaps();
  1813. }
  1814. /**
  1815. *
  1816. * @return the helper class used to query the custom XML mapping defined in this workbook
  1817. */
  1818. @Internal
  1819. public MapInfo getMapInfo(){
  1820. return mapInfo;
  1821. }
  1822. /**
  1823. * Adds the External Link Table part and relations required to allow formulas
  1824. * referencing the specified external workbook to be added to this one. Allows
  1825. * formulas such as "[MyOtherWorkbook.xlsx]Sheet3!$A$5" to be added to the
  1826. * file, for workbooks not already linked / referenced.
  1827. * <p>
  1828. * This support is still regarded as in beta and may change
  1829. * <p>
  1830. * see https://bz.apache.org/bugzilla/show_bug.cgi?id=57184
  1831. *
  1832. * @param name The name the workbook will be referenced as in formulas
  1833. * @param workbook The open workbook to fetch the link required information from
  1834. * @return index position for external workbook
  1835. * @since POI 5.1.0
  1836. */
  1837. @Beta
  1838. @Override
  1839. public int linkExternalWorkbook(String name, Workbook workbook) {
  1840. int externalLinkIdx=-1;
  1841. if (!getCreationHelper().getReferencedWorkbooks().containsKey(name)){
  1842. externalLinkIdx = this.getNextPartNumber(XSSFRelation.EXTERNAL_LINKS,
  1843. this.getPackagePart().getPackage().getPartsByContentType(XSSFRelation.EXTERNAL_LINKS.getContentType()).size() + 1);
  1844. POIXMLDocumentPart.RelationPart rp = this.createRelationship(XSSFRelation.EXTERNAL_LINKS, xssfFactory, externalLinkIdx, false);
  1845. ExternalLinksTable linksTable = rp.getDocumentPart();
  1846. linksTable.setLinkedFileName(name);
  1847. this.getExternalLinksTable().add(linksTable);
  1848. CTExternalReference ctExternalReference = this.getCTWorkbook().addNewExternalReferences().addNewExternalReference();
  1849. ctExternalReference.setId(rp.getRelationship().getId());
  1850. } else {
  1851. List<RelationPart> relationParts = getRelationParts();
  1852. for (RelationPart relationPart : relationParts) {
  1853. if (relationPart.getDocumentPart() instanceof ExternalLinksTable) {
  1854. ExternalLinksTable linksTable = relationPart.getDocumentPart();
  1855. String linkedFileName = linksTable.getLinkedFileName();
  1856. if(linkedFileName.equals(name)){
  1857. String s = relationPart.getRelationship().getTargetURI().toString();
  1858. String s2 = XSSFRelation.EXTERNAL_LINKS.getDefaultFileName();
  1859. String numStr = s.substring(s2.indexOf('#'), s2.indexOf('.'));
  1860. externalLinkIdx = Integer.parseInt(numStr);
  1861. break;
  1862. }
  1863. }
  1864. }
  1865. }
  1866. XSSFCreationHelper creationHelper = getCreationHelper();
  1867. creationHelper.addExternalWorkbook(name,workbook);
  1868. return externalLinkIdx;
  1869. }
  1870. /**
  1871. * Specifies a boolean value that indicates whether structure of workbook is locked. <br>
  1872. * A value true indicates the structure of the workbook is locked. Worksheets in the workbook can't be moved,
  1873. * deleted, hidden, unhidden, or renamed, and new worksheets can't be inserted.<br>
  1874. * A value of false indicates the structure of the workbook is not locked.<br>
  1875. *
  1876. * @return true if structure of workbook is locked
  1877. */
  1878. public boolean isStructureLocked() {
  1879. return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockStructure();
  1880. }
  1881. /**
  1882. * Specifies a boolean value that indicates whether the windows that comprise the workbook are locked. <br>
  1883. * A value of true indicates the workbook windows are locked. Windows are the same size and position each time the
  1884. * workbook is opened.<br>
  1885. * A value of false indicates the workbook windows are not locked.
  1886. *
  1887. * @return true if windows that comprise the workbook are locked
  1888. */
  1889. public boolean isWindowsLocked() {
  1890. return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockWindows();
  1891. }
  1892. /**
  1893. * Specifies a boolean value that indicates whether the workbook is locked for revisions.
  1894. *
  1895. * @return true if the workbook is locked for revisions.
  1896. */
  1897. public boolean isRevisionLocked() {
  1898. return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockRevision();
  1899. }
  1900. /**
  1901. * Locks the structure of workbook.
  1902. */
  1903. public void lockStructure() {
  1904. safeGetWorkbookProtection().setLockStructure(true);
  1905. }
  1906. /**
  1907. * Unlocks the structure of workbook.
  1908. */
  1909. public void unLockStructure() {
  1910. safeGetWorkbookProtection().setLockStructure(false);
  1911. }
  1912. /**
  1913. * Locks the windows that comprise the workbook.
  1914. */
  1915. public void lockWindows() {
  1916. safeGetWorkbookProtection().setLockWindows(true);
  1917. }
  1918. /**
  1919. * Unlocks the windows that comprise the workbook.
  1920. */
  1921. public void unLockWindows() {
  1922. safeGetWorkbookProtection().setLockWindows(false);
  1923. }
  1924. /**
  1925. * Locks the workbook for revisions.
  1926. */
  1927. public void lockRevision() {
  1928. safeGetWorkbookProtection().setLockRevision(true);
  1929. }
  1930. /**
  1931. * Unlocks the workbook for revisions.
  1932. */
  1933. public void unLockRevision() {
  1934. safeGetWorkbookProtection().setLockRevision(false);
  1935. }
  1936. /**
  1937. * Sets the workbook password.
  1938. *
  1939. * @param password if null, the password will be removed
  1940. * @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
  1941. * otherwise the given algorithm is used for calculating the hash password (Excel 2013)
  1942. */
  1943. public void setWorkbookPassword(String password, HashAlgorithm hashAlgo) {
  1944. if (password == null && !workbookProtectionPresent()) {
  1945. return;
  1946. }
  1947. setPassword(safeGetWorkbookProtection(), password, hashAlgo, "workbook");
  1948. }
  1949. /**
  1950. * Validate the password against the stored hash, the hashing method will be determined
  1951. * by the existing password attributes
  1952. * @return true, if the hashes match (... though original password may differ ...)
  1953. */
  1954. public boolean validateWorkbookPassword(String password) {
  1955. if (!workbookProtectionPresent()) {
  1956. return (password == null);
  1957. }
  1958. return validatePassword(safeGetWorkbookProtection(), password, "workbook");
  1959. }
  1960. /**
  1961. * Sets the revisions password.
  1962. *
  1963. * @param password if null, the password will be removed
  1964. * @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
  1965. * otherwise the given algorithm is used for calculating the hash password (Excel 2013)
  1966. */
  1967. public void setRevisionsPassword(String password, HashAlgorithm hashAlgo) {
  1968. if (password == null && !workbookProtectionPresent()) {
  1969. return;
  1970. }
  1971. setPassword(safeGetWorkbookProtection(), password, hashAlgo, "revisions");
  1972. }
  1973. /**
  1974. * Validate the password against the stored hash, the hashing method will be determined
  1975. * by the existing password attributes
  1976. * @return true if the hashes match (... though original password may differ ...)
  1977. */
  1978. public boolean validateRevisionsPassword(String password) {
  1979. if (!workbookProtectionPresent()) {
  1980. return (password == null);
  1981. }
  1982. return validatePassword(safeGetWorkbookProtection(), password, "revisions");
  1983. }
  1984. /**
  1985. * Removes the workbook protection settings
  1986. */
  1987. public void unLock() {
  1988. if (workbookProtectionPresent()) {
  1989. workbook.unsetWorkbookProtection();
  1990. }
  1991. }
  1992. private boolean workbookProtectionPresent() {
  1993. return workbook.isSetWorkbookProtection();
  1994. }
  1995. private CTWorkbookProtection safeGetWorkbookProtection() {
  1996. if (!workbookProtectionPresent()){
  1997. return workbook.addNewWorkbookProtection();
  1998. }
  1999. return workbook.getWorkbookProtection();
  2000. }
  2001. /**
  2002. *
  2003. * Returns the locator of user-defined functions.
  2004. * <p>
  2005. * The default instance extends the built-in functions with the Excel Analysis Tool Pack.
  2006. * To set / evaluate custom functions you need to register them as follows:
  2007. *
  2008. *
  2009. *
  2010. * </p>
  2011. * @return wrapped instance of UDFFinder that allows seeking functions both by index and name
  2012. */
  2013. /*package*/ UDFFinder getUDFFinder() {
  2014. return _udfFinder;
  2015. }
  2016. /**
  2017. * Register a new toolpack in this workbook.
  2018. *
  2019. * @param toolpack the toolpack to register
  2020. */
  2021. @Override
  2022. public void addToolPack(UDFFinder toolpack){
  2023. _udfFinder.add(toolpack);
  2024. }
  2025. /**
  2026. * Whether the application shall perform a full recalculation when the workbook is opened.
  2027. * <p>
  2028. * Typically you want to force formula recalculation when you modify cell formulas or values
  2029. * of a workbook previously created by Excel. When set to true, this flag will tell Excel
  2030. * that it needs to recalculate all formulas in the workbook the next time the file is opened.
  2031. * </p>
  2032. * <p>
  2033. * Note, that recalculation updates cached formula results and, thus, modifies the workbook.
  2034. * Depending on the version, Excel may prompt you with "Do you want to save the changes in <em>filename</em>?"
  2035. * on close.
  2036. * </p>
  2037. *
  2038. * @param value true if the application will perform a full recalculation of
  2039. * workbook values when the workbook is opened
  2040. * @since 3.8
  2041. */
  2042. @Override
  2043. public void setForceFormulaRecalculation(boolean value){
  2044. CTWorkbook ctWorkbook = getCTWorkbook();
  2045. CTCalcPr calcPr = ctWorkbook.isSetCalcPr() ? ctWorkbook.getCalcPr() : ctWorkbook.addNewCalcPr();
  2046. // when set to true, will tell Excel that it needs to recalculate all formulas
  2047. // in the workbook the next time the file is opened.
  2048. calcPr.setFullCalcOnLoad(value);
  2049. if(value && calcPr.getCalcMode() == STCalcMode.MANUAL) {
  2050. calcPr.setCalcMode(STCalcMode.AUTO);
  2051. }
  2052. }
  2053. /**
  2054. * Whether Excel will be asked to recalculate all formulas when the workbook is opened.
  2055. *
  2056. * @since 3.8
  2057. */
  2058. @Override
  2059. public boolean getForceFormulaRecalculation(){
  2060. CTWorkbook ctWorkbook = getCTWorkbook();
  2061. CTCalcPr calcPr = ctWorkbook.getCalcPr();
  2062. return calcPr != null && calcPr.isSetFullCalcOnLoad() && calcPr.getFullCalcOnLoad();
  2063. }
  2064. /**
  2065. * Add pivotCache to the workbook
  2066. */
  2067. @Beta
  2068. protected CTPivotCache addPivotCache(String rId) {
  2069. CTWorkbook ctWorkbook = getCTWorkbook();
  2070. CTPivotCaches caches;
  2071. if (ctWorkbook.isSetPivotCaches()) {
  2072. caches = ctWorkbook.getPivotCaches();
  2073. } else {
  2074. caches = ctWorkbook.addNewPivotCaches();
  2075. }
  2076. CTPivotCache cache = caches.addNewPivotCache();
  2077. int tableId = getPivotTables().size()+1;
  2078. cache.setCacheId(tableId);
  2079. cache.setId(rId);
  2080. if(pivotCaches == null) {
  2081. pivotCaches = new ArrayList<>();
  2082. }
  2083. pivotCaches.add(cache);
  2084. return cache;
  2085. }
  2086. @Beta
  2087. public List<XSSFPivotTable> getPivotTables() {
  2088. return pivotTables;
  2089. }
  2090. @Beta
  2091. protected void setPivotTables(List<XSSFPivotTable> pivotTables) {
  2092. this.pivotTables = pivotTables;
  2093. }
  2094. public XSSFWorkbookType getWorkbookType() {
  2095. return isMacroEnabled() ? XSSFWorkbookType.XLSM : XSSFWorkbookType.XLSX;
  2096. }
  2097. /**
  2098. * Sets whether the workbook will be an .xlsx or .xlsm (macro-enabled) file.
  2099. */
  2100. public void setWorkbookType(XSSFWorkbookType type) {
  2101. try {
  2102. getPackagePart().setContentType(type.getContentType());
  2103. } catch (InvalidFormatException e) {
  2104. throw new POIXMLException(e);
  2105. }
  2106. }
  2107. /**
  2108. * Adds a vbaProject.bin file to the workbook. This will change the workbook
  2109. * type if necessary.
  2110. *
  2111. * @throws IOException If copying data from the stream fails.
  2112. */
  2113. public void setVBAProject(InputStream vbaProjectStream) throws IOException {
  2114. if (!isMacroEnabled()) {
  2115. setWorkbookType(XSSFWorkbookType.XLSM);
  2116. }
  2117. PackagePartName ppName;
  2118. try {
  2119. ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
  2120. } catch (InvalidFormatException e) {
  2121. throw new POIXMLException(e);
  2122. }
  2123. OPCPackage opc = getPackage();
  2124. OutputStream outputStream;
  2125. if (!opc.containPart(ppName)) {
  2126. POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, this.xssfFactory);
  2127. outputStream = relationship.getPackagePart().getOutputStream();
  2128. } else {
  2129. PackagePart part = opc.getPart(ppName);
  2130. outputStream = part.getOutputStream();
  2131. }
  2132. try {
  2133. IOUtils.copy(vbaProjectStream, outputStream);
  2134. } finally {
  2135. IOUtils.closeQuietly(outputStream);
  2136. }
  2137. }
  2138. /**
  2139. * Adds a vbaProject.bin file taken from another, given workbook to this one.
  2140. * @throws IOException If copying the VBAProject stream fails.
  2141. * @throws InvalidFormatException If an error occurs while handling parts of the XSSF format
  2142. */
  2143. public void setVBAProject(XSSFWorkbook macroWorkbook) throws IOException, InvalidFormatException {
  2144. if (!macroWorkbook.isMacroEnabled()) {
  2145. return;
  2146. }
  2147. InputStream vbaProjectStream = XSSFRelation.VBA_MACROS.getContents(macroWorkbook.getCorePart());
  2148. if (vbaProjectStream != null) {
  2149. setVBAProject(vbaProjectStream);
  2150. }
  2151. }
  2152. /**
  2153. * Returns the spreadsheet version (EXCLE2007) of this workbook
  2154. *
  2155. * @return EXCEL2007 SpreadsheetVersion enum
  2156. * @since 3.14 beta 2
  2157. */
  2158. @Override
  2159. public SpreadsheetVersion getSpreadsheetVersion() {
  2160. return SpreadsheetVersion.EXCEL2007;
  2161. }
  2162. /**
  2163. * Returns the data table with the given name (case insensitive).
  2164. *
  2165. * @param name the data table name (case-insensitive)
  2166. * @return The Data table in the workbook named {@code name}, or {@code null} if no table is named {@code name}.
  2167. * @since 3.15 beta 2
  2168. */
  2169. public XSSFTable getTable(String name) {
  2170. if (name != null && sheets != null) {
  2171. for (XSSFSheet sheet : sheets) {
  2172. for (XSSFTable tbl : sheet.getTables()) {
  2173. if (name.equalsIgnoreCase(tbl.getName())) {
  2174. return tbl;
  2175. }
  2176. }
  2177. }
  2178. }
  2179. return null;
  2180. }
  2181. @Override
  2182. public int addOlePackage(byte[] oleData, String label, String fileName, String command)
  2183. throws IOException {
  2184. final XSSFRelation rel = XSSFRelation.OLEEMBEDDINGS;
  2185. // find an unused part name
  2186. OPCPackage opc = getPackage();
  2187. PackagePartName pnOLE;
  2188. int oleId;
  2189. try {
  2190. oleId = opc.getUnusedPartIndex(rel.getDefaultFileName());
  2191. pnOLE = PackagingURIHelper.createPartName(rel.getFileName(oleId));
  2192. } catch (InvalidFormatException e) {
  2193. throw new IOException("ole object name not recognized", e);
  2194. }
  2195. PackagePart pp = opc.createPart( pnOLE, rel.getContentType() );
  2196. Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
  2197. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(oleData.length+500)) {
  2198. ole10.writeOut(bos);
  2199. try (POIFSFileSystem poifs = new POIFSFileSystem()) {
  2200. DirectoryNode root = poifs.getRoot();
  2201. root.createDocument(Ole10Native.OLE10_NATIVE, bos.toInputStream());
  2202. root.setStorageClsid(ClassIDPredefined.OLE_V1_PACKAGE.getClassID());
  2203. // TODO: generate CombObj stream
  2204. try (OutputStream os = pp.getOutputStream()) {
  2205. poifs.writeFilesystem(os);
  2206. }
  2207. }
  2208. }
  2209. return oleId;
  2210. }
  2211. /**
  2212. * Whether a call to {@link XSSFCell#setCellFormula(String)} will validate the formula or not.
  2213. *
  2214. * @param value true if the application will validate the formula is correct
  2215. * @since 3.17
  2216. */
  2217. public void setCellFormulaValidation(final boolean value) {
  2218. this.cellFormulaValidation = value;
  2219. }
  2220. /**
  2221. * Whether a call to {@link XSSFCell#setCellFormula(String)} will validate the formula or not.
  2222. *
  2223. * @since 3.17
  2224. */
  2225. public boolean getCellFormulaValidation() {
  2226. return this.cellFormulaValidation;
  2227. }
  2228. @Override
  2229. public XSSFEvaluationWorkbook createEvaluationWorkbook() {
  2230. return XSSFEvaluationWorkbook.create(this);
  2231. }
  2232. }