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.

HSSFWorkbook.java 80KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  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.hssf.usermodel;
  16. import static org.apache.logging.log4j.util.Unbox.box;
  17. import static org.apache.poi.hssf.model.InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME;
  18. import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES;
  19. import java.io.BufferedOutputStream;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.io.OutputStreamWriter;
  28. import java.io.PrintWriter;
  29. import java.nio.charset.Charset;
  30. import java.security.GeneralSecurityException;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.HashMap;
  36. import java.util.HashSet;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.NoSuchElementException;
  41. import java.util.Set;
  42. import java.util.regex.Pattern;
  43. import org.apache.commons.codec.digest.DigestUtils;
  44. import org.apache.logging.log4j.LogManager;
  45. import org.apache.logging.log4j.Logger;
  46. import org.apache.poi.EncryptedDocumentException;
  47. import org.apache.poi.POIDocument;
  48. import org.apache.poi.ddf.EscherBSERecord;
  49. import org.apache.poi.ddf.EscherBitmapBlip;
  50. import org.apache.poi.ddf.EscherBlipRecord;
  51. import org.apache.poi.ddf.EscherMetafileBlip;
  52. import org.apache.poi.ddf.EscherRecord;
  53. import org.apache.poi.hpsf.ClassID;
  54. import org.apache.poi.hpsf.ClassIDPredefined;
  55. import org.apache.poi.hpsf.DocumentSummaryInformation;
  56. import org.apache.poi.hpsf.SummaryInformation;
  57. import org.apache.poi.hssf.OldExcelFormatException;
  58. import org.apache.poi.hssf.model.DrawingManager2;
  59. import org.apache.poi.hssf.model.HSSFFormulaParser;
  60. import org.apache.poi.hssf.model.InternalSheet;
  61. import org.apache.poi.hssf.model.InternalSheet.UnsupportedBOFType;
  62. import org.apache.poi.hssf.model.InternalWorkbook;
  63. import org.apache.poi.hssf.model.RecordStream;
  64. import org.apache.poi.hssf.model.WorkbookRecordList;
  65. import org.apache.poi.hssf.record.AbstractEscherHolderRecord;
  66. import org.apache.poi.hssf.record.BackupRecord;
  67. import org.apache.poi.hssf.record.BoundSheetRecord;
  68. import org.apache.poi.hssf.record.DrawingGroupRecord;
  69. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  70. import org.apache.poi.hssf.record.FilePassRecord;
  71. import org.apache.poi.hssf.record.FontRecord;
  72. import org.apache.poi.hssf.record.FormatRecord;
  73. import org.apache.poi.hssf.record.LabelRecord;
  74. import org.apache.poi.hssf.record.LabelSSTRecord;
  75. import org.apache.poi.hssf.record.NameRecord;
  76. import org.apache.poi.hssf.record.RecalcIdRecord;
  77. import org.apache.poi.hssf.record.Record;
  78. import org.apache.poi.hssf.record.RecordFactory;
  79. import org.apache.poi.hssf.record.SSTRecord;
  80. import org.apache.poi.hssf.record.UnknownRecord;
  81. import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
  82. import org.apache.poi.hssf.record.common.UnicodeString;
  83. import org.apache.poi.hssf.record.crypto.Biff8DecryptingStream;
  84. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  85. import org.apache.poi.poifs.crypt.ChunkedCipherOutputStream;
  86. import org.apache.poi.poifs.crypt.Decryptor;
  87. import org.apache.poi.poifs.crypt.EncryptionInfo;
  88. import org.apache.poi.poifs.crypt.EncryptionMode;
  89. import org.apache.poi.poifs.crypt.EncryptionVerifier;
  90. import org.apache.poi.poifs.crypt.Encryptor;
  91. import org.apache.poi.poifs.filesystem.DirectoryEntry;
  92. import org.apache.poi.poifs.filesystem.DirectoryNode;
  93. import org.apache.poi.poifs.filesystem.DocumentNode;
  94. import org.apache.poi.poifs.filesystem.EntryUtils;
  95. import org.apache.poi.poifs.filesystem.FileMagic;
  96. import org.apache.poi.poifs.filesystem.FilteringDirectoryNode;
  97. import org.apache.poi.poifs.filesystem.Ole10Native;
  98. import org.apache.poi.poifs.filesystem.POIFSDocument;
  99. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  100. import org.apache.poi.ss.SpreadsheetVersion;
  101. import org.apache.poi.ss.formula.FormulaShifter;
  102. import org.apache.poi.ss.formula.FormulaType;
  103. import org.apache.poi.ss.formula.SheetNameFormatter;
  104. import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
  105. import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
  106. import org.apache.poi.ss.formula.udf.UDFFinder;
  107. import org.apache.poi.ss.usermodel.Name;
  108. import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
  109. import org.apache.poi.ss.usermodel.Sheet;
  110. import org.apache.poi.ss.usermodel.SheetVisibility;
  111. import org.apache.poi.ss.usermodel.Workbook;
  112. import org.apache.poi.ss.util.CellReference;
  113. import org.apache.poi.ss.util.WorkbookUtil;
  114. import org.apache.poi.util.Configurator;
  115. import org.apache.poi.util.HexDump;
  116. import org.apache.poi.util.IOUtils;
  117. import org.apache.poi.util.Internal;
  118. import org.apache.poi.util.LittleEndian;
  119. import org.apache.poi.util.LittleEndianByteArrayInputStream;
  120. import org.apache.poi.util.LittleEndianByteArrayOutputStream;
  121. import org.apache.poi.util.Removal;
  122. /**
  123. * High level representation of a workbook. This is the first object most users
  124. * will construct whether they are reading or writing a workbook. It is also the
  125. * top level object for creating new sheets/etc.
  126. *
  127. * @see InternalWorkbook
  128. * @see HSSFSheet
  129. */
  130. @SuppressWarnings("WeakerAccess")
  131. public final class HSSFWorkbook extends POIDocument implements Workbook {
  132. //arbitrarily selected; may need to increase
  133. private static final int MAX_RECORD_LENGTH = 100_000;
  134. private static final int MAX_IMAGE_LENGTH = 50_000_000;
  135. private static final Pattern COMMA_PATTERN = Pattern.compile(",");
  136. /**
  137. * The maximum number of cell styles in a .xls workbook.
  138. * The 'official' limit is 4,000, but POI allows a slightly larger number.
  139. * This extra delta takes into account built-in styles that are automatically
  140. * <p>
  141. * See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx
  142. */
  143. private static final int MAX_STYLES = 4030;
  144. /**
  145. * used for compile-time performance/memory optimization. This determines the
  146. * initial capacity for the sheet collection. Its currently set to 3.
  147. * Changing it in this release will decrease performance
  148. * since you're never allowed to have more or less than three sheets!
  149. */
  150. public static final int INITIAL_CAPACITY = Configurator.getIntValue("HSSFWorkbook.SheetInitialCapacity", 3);
  151. /**
  152. * this is the reference to the low level Workbook object
  153. */
  154. private InternalWorkbook workbook;
  155. /**
  156. * this holds the HSSFSheet objects attached to this workbook
  157. */
  158. protected List<HSSFSheet> _sheets;
  159. /**
  160. * this holds the HSSFName objects attached to this workbook
  161. */
  162. private ArrayList<HSSFName> names;
  163. /**
  164. * this holds the HSSFFont objects attached to this workbook.
  165. * We only create these from the low level records as required.
  166. */
  167. private Map<Integer, HSSFFont> fonts;
  168. /**
  169. * holds whether or not to preserve other nodes in the POIFS. Used
  170. * for macros and embedded objects.
  171. */
  172. private boolean preserveNodes;
  173. /**
  174. * Used to keep track of the data formatter so that all
  175. * createDataFormatter calls return the same one for a given
  176. * book. This ensures that updates from one places is visible
  177. * someplace else.
  178. */
  179. private HSSFDataFormat formatter;
  180. /**
  181. * The policy to apply in the event of missing or
  182. * blank cells when fetching from a row.
  183. * See {@link MissingCellPolicy}
  184. */
  185. private MissingCellPolicy missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK;
  186. private static final Logger LOGGER = LogManager.getLogger(HSSFWorkbook.class);
  187. /**
  188. * The locator of user-defined functions.
  189. * By default includes functions from the Excel Analysis Toolpack
  190. */
  191. private UDFFinder _udfFinder = new IndexedUDFFinder(AggregatingUDFFinder.DEFAULT);
  192. public static HSSFWorkbook create(InternalWorkbook book) {
  193. return new HSSFWorkbook(book);
  194. }
  195. /**
  196. * Creates new HSSFWorkbook from scratch (start here!)
  197. */
  198. public HSSFWorkbook() {
  199. this(InternalWorkbook.createWorkbook());
  200. }
  201. private HSSFWorkbook(InternalWorkbook book) {
  202. super((DirectoryNode) null);
  203. workbook = book;
  204. _sheets = new ArrayList<>(INITIAL_CAPACITY);
  205. names = new ArrayList<>(INITIAL_CAPACITY);
  206. }
  207. /**
  208. * Given a POI POIFSFileSystem object, read in its Workbook along
  209. * with all related nodes, and populate the high and low level models.
  210. * <p>This calls {@link #HSSFWorkbook(POIFSFileSystem, boolean)} with
  211. * preserve nodes set to true.
  212. *
  213. * @throws IOException if the stream cannot be read
  214. * @see #HSSFWorkbook(POIFSFileSystem, boolean)
  215. * @see POIFSFileSystem
  216. */
  217. public HSSFWorkbook(POIFSFileSystem fs) throws IOException {
  218. this(fs, true);
  219. }
  220. /**
  221. * Given a POI POIFSFileSystem object, read in its Workbook and populate
  222. * the high and low level models. If you're reading in a workbook... start here!
  223. *
  224. * @param fs the POI filesystem that contains the Workbook stream.
  225. * @param preserveNodes whether to preserve other nodes, such as
  226. * macros. This takes more memory, so only say yes if you
  227. * need to. If set, will store all of the POIFSFileSystem
  228. * in memory
  229. * @throws IOException if the stream cannot be read
  230. * @see POIFSFileSystem
  231. */
  232. public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)
  233. throws IOException {
  234. this(fs.getRoot(), fs, preserveNodes);
  235. }
  236. public static String getWorkbookDirEntryName(DirectoryNode directory) {
  237. for (String wbName : WORKBOOK_DIR_ENTRY_NAMES) {
  238. if (directory.hasEntry(wbName)) {
  239. return wbName;
  240. }
  241. }
  242. // check for an encrypted .xlsx file - they get OLE2 wrapped
  243. if (directory.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
  244. throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " +
  245. "It must be decrypted before use by XSSF, it cannot be used by HSSF");
  246. }
  247. // check for previous version of file format
  248. if (directory.hasEntry(OLD_WORKBOOK_DIR_ENTRY_NAME)) {
  249. throw new OldExcelFormatException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. "
  250. + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)");
  251. }
  252. // throw more useful exceptions for known wrong file-extensions
  253. if (directory.hasEntry("WordDocument")) {
  254. throw new IllegalArgumentException("The document is really a DOC file");
  255. }
  256. throw new IllegalArgumentException("The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. "
  257. + "Is it really an excel file? Had: " + directory.getEntryNames());
  258. }
  259. /**
  260. * given a POI POIFSFileSystem object, and a specific directory
  261. * within it, read in its Workbook and populate the high and
  262. * low level models. If you're reading in a workbook...start here.
  263. *
  264. * @param directory the POI filesystem directory to process from
  265. * @param fs the POI filesystem that contains the Workbook stream.
  266. * @param preserveNodes whether to preserve other nodes, such as
  267. * macros. This takes more memory, so only say yes if you
  268. * need to. If set, will store all of the POIFSFileSystem
  269. * in memory
  270. * @throws IOException if the stream cannot be read
  271. * @see POIFSFileSystem
  272. */
  273. public HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes)
  274. throws IOException {
  275. this(directory, preserveNodes);
  276. }
  277. /**
  278. * given a POI POIFSFileSystem object, and a specific directory
  279. * within it, read in its Workbook and populate the high and
  280. * low level models. If you're reading in a workbook...start here.
  281. *
  282. * @param directory the POI filesystem directory to process from
  283. * @param preserveNodes whether to preserve other nodes, such as
  284. * macros. This takes more memory, so only say yes if you
  285. * need to. If set, will store all of the POIFSFileSystem
  286. * in memory
  287. * @throws IOException if the stream cannot be read
  288. * @see POIFSFileSystem
  289. */
  290. public HSSFWorkbook(DirectoryNode directory, boolean preserveNodes)
  291. throws IOException {
  292. super(directory);
  293. String workbookName = getWorkbookDirEntryName(directory);
  294. this.preserveNodes = preserveNodes;
  295. // If we're not preserving nodes, don't track the
  296. // POIFS any more
  297. if (!preserveNodes) {
  298. clearDirectory();
  299. }
  300. _sheets = new ArrayList<>(INITIAL_CAPACITY);
  301. names = new ArrayList<>(INITIAL_CAPACITY);
  302. // Grab the data from the workbook stream, however
  303. // it happens to be spelled.
  304. InputStream stream = directory.createDocumentInputStream(workbookName);
  305. List<Record> records = RecordFactory.createRecords(stream);
  306. workbook = InternalWorkbook.createWorkbook(records);
  307. setPropertiesFromWorkbook(workbook);
  308. int recOffset = workbook.getNumRecords();
  309. // convert all LabelRecord records to LabelSSTRecord
  310. convertLabelRecords(records, recOffset);
  311. RecordStream rs = new RecordStream(records, recOffset);
  312. while (rs.hasNext()) {
  313. try {
  314. InternalSheet sheet = InternalSheet.createSheet(rs);
  315. _sheets.add(new HSSFSheet(this, sheet));
  316. } catch (UnsupportedBOFType eb) {
  317. // Hopefully there's a supported one after this!
  318. LOGGER.atWarn().log("Unsupported BOF found of type {}", box(eb.getType()));
  319. }
  320. }
  321. for (int i = 0; i < workbook.getNumNames(); ++i) {
  322. NameRecord nameRecord = workbook.getNameRecord(i);
  323. HSSFName name = new HSSFName(this, nameRecord, workbook.getNameCommentRecord(nameRecord));
  324. names.add(name);
  325. }
  326. }
  327. /**
  328. * Companion to HSSFWorkbook(POIFSFileSystem), this constructs the
  329. * POI filesystem around your {@link InputStream}, including all nodes.
  330. * <p>This calls {@link #HSSFWorkbook(InputStream, boolean)} with
  331. * preserve nodes set to true.
  332. *
  333. * @throws IOException if the stream cannot be read
  334. * @see #HSSFWorkbook(InputStream, boolean)
  335. * @see #HSSFWorkbook(POIFSFileSystem)
  336. * @see POIFSFileSystem
  337. */
  338. public HSSFWorkbook(InputStream s) throws IOException {
  339. this(s, true);
  340. }
  341. /**
  342. * Companion to HSSFWorkbook(POIFSFileSystem), this constructs the
  343. * POI filesystem around your {@link InputStream}.
  344. *
  345. * @param s the POI filesystem that contains the Workbook stream.
  346. * @param preserveNodes whether to preserve other nodes, such as
  347. * macros. This takes more memory, so only say yes if you
  348. * need to.
  349. * @throws IOException if the stream cannot be read
  350. * @see POIFSFileSystem
  351. * @see #HSSFWorkbook(POIFSFileSystem)
  352. */
  353. @SuppressWarnings("resource") // POIFSFileSystem always closes the stream
  354. public HSSFWorkbook(InputStream s, boolean preserveNodes)
  355. throws IOException {
  356. this(new POIFSFileSystem(s).getRoot(), preserveNodes);
  357. }
  358. /**
  359. * used internally to set the workbook properties.
  360. */
  361. private void setPropertiesFromWorkbook(InternalWorkbook book) {
  362. this.workbook = book;
  363. // none currently
  364. }
  365. /**
  366. * This is basically a kludge to deal with the now obsolete Label records. If
  367. * you have to read in a sheet that contains Label records, be aware that the rest
  368. * of the API doesn't deal with them, the low level structure only provides read-only
  369. * semi-immutable structures (the sets are there for interface conformance with NO
  370. * Implementation). In short, you need to call this function passing it a reference
  371. * to the Workbook object. All labels will be converted to LabelSST records and their
  372. * contained strings will be written to the Shared String table (SSTRecord) within
  373. * the Workbook.
  374. *
  375. * @param records a collection of sheet's records.
  376. * @param offset the offset to search at
  377. * @see LabelRecord
  378. * @see LabelSSTRecord
  379. * @see SSTRecord
  380. */
  381. private void convertLabelRecords(List<Record> records, int offset) {
  382. LOGGER.atDebug().log("convertLabelRecords called");
  383. for (int k = offset; k < records.size(); k++) {
  384. Record rec = records.get(k);
  385. if (rec.getSid() == LabelRecord.sid) {
  386. LabelRecord oldrec = (LabelRecord) rec;
  387. records.remove(k);
  388. LabelSSTRecord newrec = new LabelSSTRecord();
  389. int stringid =
  390. workbook.addSSTString(new UnicodeString(oldrec.getValue()));
  391. newrec.setRow(oldrec.getRow());
  392. newrec.setColumn(oldrec.getColumn());
  393. newrec.setXFIndex(oldrec.getXFIndex());
  394. newrec.setSSTIndex(stringid);
  395. records.add(k, newrec);
  396. }
  397. }
  398. LOGGER.atDebug().log("convertLabelRecords exit");
  399. }
  400. /**
  401. * Retrieves the current policy on what to do when
  402. * getting missing or blank cells from a row.
  403. * The default is to return blank and null cells.
  404. * {@link MissingCellPolicy}
  405. */
  406. @Override
  407. public MissingCellPolicy getMissingCellPolicy() {
  408. return missingCellPolicy;
  409. }
  410. /**
  411. * Sets the policy on what to do when
  412. * getting missing or blank cells from a row.
  413. * This will then apply to all calls to
  414. * {@link HSSFRow#getCell(int)}}. See
  415. * {@link MissingCellPolicy}.
  416. * Note that this has no effect on any
  417. * iterators, only on when fetching Cells
  418. * by their column index.
  419. */
  420. @Override
  421. public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
  422. this.missingCellPolicy = missingCellPolicy;
  423. }
  424. /**
  425. * sets the order of appearance for a given sheet.
  426. *
  427. * @param sheetname the name of the sheet to reorder
  428. * @param pos the position that we want to insert the sheet into (0 based)
  429. */
  430. @Override
  431. public void setSheetOrder(String sheetname, int pos) {
  432. int oldSheetIndex = getSheetIndex(sheetname);
  433. _sheets.add(pos, _sheets.remove(oldSheetIndex));
  434. workbook.setSheetOrder(sheetname, pos);
  435. FormulaShifter shifter = FormulaShifter.createForSheetShift(oldSheetIndex, pos);
  436. for (HSSFSheet sheet : _sheets) {
  437. sheet.getSheet().updateFormulasAfterCellShift(shifter, /* not used */ -1);
  438. }
  439. workbook.updateNamesAfterCellShift(shifter);
  440. updateNamedRangesAfterSheetReorder(oldSheetIndex, pos);
  441. updateActiveSheetAfterSheetReorder(oldSheetIndex, pos);
  442. }
  443. /**
  444. * copy-pasted from XSSFWorkbook#updateNamedRangesAfterSheetReorder(int, int)
  445. * <p>
  446. * update sheet-scoped named ranges in this workbook after changing the sheet order
  447. * of a sheet at oldIndex to newIndex.
  448. * Sheets between these indices will move left or right by 1.
  449. *
  450. * @param oldIndex the original index of the re-ordered sheet
  451. * @param newIndex the new index of the re-ordered sheet
  452. */
  453. private void updateNamedRangesAfterSheetReorder(int oldIndex, int newIndex) {
  454. // update sheet index of sheet-scoped named ranges
  455. for (final HSSFName name : names) {
  456. final int i = name.getSheetIndex();
  457. // name has sheet-level scope
  458. if (i != -1) {
  459. // name refers to this sheet
  460. if (i == oldIndex) {
  461. name.setSheetIndex(newIndex);
  462. }
  463. // if oldIndex > newIndex then this sheet moved left and sheets between newIndex and oldIndex moved right
  464. else if (newIndex <= i && i < oldIndex) {
  465. name.setSheetIndex(i + 1);
  466. }
  467. // if oldIndex < newIndex then this sheet moved right and sheets between oldIndex and newIndex moved left
  468. else if (oldIndex < i && i <= newIndex) {
  469. name.setSheetIndex(i - 1);
  470. }
  471. }
  472. }
  473. }
  474. private void updateActiveSheetAfterSheetReorder(int oldIndex, int newIndex) {
  475. // adjust active sheet if necessary
  476. int active = getActiveSheetIndex();
  477. if (active == oldIndex) {
  478. // moved sheet was the active one
  479. setActiveSheet(newIndex);
  480. } else if ((active < oldIndex && active < newIndex) ||
  481. (active > oldIndex && active > newIndex)) {
  482. // not affected
  483. } else if (newIndex > oldIndex) {
  484. // moved sheet was below before and is above now => active is one less
  485. setActiveSheet(active - 1);
  486. } else {
  487. // remaining case: moved sheet was higher than active before and is lower now => active is one more
  488. setActiveSheet(active + 1);
  489. }
  490. }
  491. private void validateSheetIndex(int index) {
  492. int lastSheetIx = _sheets.size() - 1;
  493. if (index < 0 || index > lastSheetIx) {
  494. String range = "(0.." + lastSheetIx + ")";
  495. if (lastSheetIx == -1) {
  496. range = "(no sheets)";
  497. }
  498. throw new IllegalArgumentException("Sheet index ("
  499. + index + ") is out of range " + range);
  500. }
  501. }
  502. /**
  503. * Selects a single sheet. This may be different to
  504. * the 'active' sheet (which is the sheet with focus).
  505. */
  506. @Override
  507. public void setSelectedTab(int index) {
  508. validateSheetIndex(index);
  509. int nSheets = _sheets.size();
  510. for (int i = 0; i < nSheets; i++) {
  511. getSheetAt(i).setSelected(i == index);
  512. }
  513. workbook.getWindowOne().setNumSelectedTabs((short) 1);
  514. }
  515. /**
  516. * Selects multiple sheets as a group. This is distinct from
  517. * the 'active' sheet (which is the sheet with focus).
  518. * Unselects sheets that are not in <code>indexes</code>.
  519. *
  520. * @param indexes Array of sheets to select, the index is 0-based.
  521. */
  522. public void setSelectedTabs(int[] indexes) {
  523. Collection<Integer> list = new ArrayList<>(indexes.length);
  524. for (int index : indexes) {
  525. list.add(index);
  526. }
  527. setSelectedTabs(list);
  528. }
  529. /**
  530. * Selects multiple sheets as a group. This is distinct from
  531. * the 'active' sheet (which is the sheet with focus).
  532. * Unselects sheets that are not in <code>indexes</code>.
  533. *
  534. * @param indexes Collection of sheets to select, the index is 0-based.
  535. */
  536. public void setSelectedTabs(Collection<Integer> indexes) {
  537. for (int index : indexes) {
  538. validateSheetIndex(index);
  539. }
  540. // ignore duplicates
  541. Set<Integer> set = new HashSet<>(indexes);
  542. int nSheets = _sheets.size();
  543. for (int i = 0; i < nSheets; i++) {
  544. boolean bSelect = set.contains(i);
  545. getSheetAt(i).setSelected(bSelect);
  546. }
  547. // this is true only if all values in set were valid sheet indexes (between 0 and nSheets-1, inclusive)
  548. short nSelected = (short) set.size();
  549. workbook.getWindowOne().setNumSelectedTabs(nSelected);
  550. }
  551. /**
  552. * Gets the selected sheets (if more than one, Excel calls these a [Group]).
  553. *
  554. * @return indices of selected sheets
  555. */
  556. public Collection<Integer> getSelectedTabs() {
  557. Collection<Integer> indexes = new ArrayList<>();
  558. int nSheets = _sheets.size();
  559. for (int i = 0; i < nSheets; i++) {
  560. HSSFSheet sheet = getSheetAt(i);
  561. if (sheet.isSelected()) {
  562. indexes.add(i);
  563. }
  564. }
  565. return Collections.unmodifiableCollection(indexes);
  566. }
  567. /**
  568. * Convenience method to set the active sheet. The active sheet is is the sheet
  569. * which is currently displayed when the workbook is viewed in Excel.
  570. * 'Selected' sheet(s) is a distinct concept.
  571. */
  572. @Override
  573. public void setActiveSheet(int index) {
  574. validateSheetIndex(index);
  575. int nSheets = _sheets.size();
  576. for (int i = 0; i < nSheets; i++) {
  577. getSheetAt(i).setActive(i == index);
  578. }
  579. workbook.getWindowOne().setActiveSheetIndex(index);
  580. }
  581. /**
  582. * gets the tab whose data is actually seen when the sheet is opened.
  583. * This may be different from the "selected sheet" since excel seems to
  584. * allow you to show the data of one sheet when another is seen "selected"
  585. * in the tabs (at the bottom).
  586. *
  587. * @see HSSFSheet#setSelected(boolean)
  588. */
  589. @Override
  590. public int getActiveSheetIndex() {
  591. return workbook.getWindowOne().getActiveSheetIndex();
  592. }
  593. /**
  594. * sets the first tab that is displayed in the list of tabs
  595. * in excel. This method does <b>not</b> hide, select or focus sheets.
  596. * It just sets the scroll position in the tab-bar.
  597. *
  598. * @param index the sheet index of the tab that will become the first in the tab-bar
  599. */
  600. @Override
  601. public void setFirstVisibleTab(int index) {
  602. workbook.getWindowOne().setFirstVisibleTab(index);
  603. }
  604. /**
  605. * sets the first tab that is displayed in the list of tabs in excel.
  606. */
  607. @Override
  608. public int getFirstVisibleTab() {
  609. return workbook.getWindowOne().getFirstVisibleTab();
  610. }
  611. /**
  612. * Set the sheet name.
  613. *
  614. * @param sheetIx number (0 based)
  615. * @throws IllegalArgumentException if the name is null or invalid
  616. * or workbook already contains a sheet with this name
  617. * @see #createSheet(String)
  618. * @see WorkbookUtil#createSafeSheetName(String nameProposal)
  619. */
  620. @Override
  621. public void setSheetName(int sheetIx, String name) {
  622. if (name == null) {
  623. throw new IllegalArgumentException("sheetName must not be null");
  624. }
  625. if (workbook.doesContainsSheetName(name, sheetIx)) {
  626. throw new IllegalArgumentException("The workbook already contains a sheet named '" + name + "'");
  627. }
  628. validateSheetIndex(sheetIx);
  629. workbook.setSheetName(sheetIx, name);
  630. }
  631. /**
  632. * @return Sheet name for the specified index
  633. */
  634. @Override
  635. public String getSheetName(int sheetIndex) {
  636. validateSheetIndex(sheetIndex);
  637. return workbook.getSheetName(sheetIndex);
  638. }
  639. @Override
  640. public boolean isHidden() {
  641. return workbook.getWindowOne().getHidden();
  642. }
  643. @Override
  644. public void setHidden(boolean hiddenFlag) {
  645. workbook.getWindowOne().setHidden(hiddenFlag);
  646. }
  647. @Override
  648. public boolean isSheetHidden(int sheetIx) {
  649. validateSheetIndex(sheetIx);
  650. return workbook.isSheetHidden(sheetIx);
  651. }
  652. @Override
  653. public boolean isSheetVeryHidden(int sheetIx) {
  654. validateSheetIndex(sheetIx);
  655. return workbook.isSheetVeryHidden(sheetIx);
  656. }
  657. @Override
  658. public SheetVisibility getSheetVisibility(int sheetIx) {
  659. return workbook.getSheetVisibility(sheetIx);
  660. }
  661. @Override
  662. public void setSheetHidden(int sheetIx, boolean hidden) {
  663. setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
  664. }
  665. @Override
  666. public void setSheetVisibility(int sheetIx, SheetVisibility visibility) {
  667. validateSheetIndex(sheetIx);
  668. workbook.setSheetHidden(sheetIx, visibility);
  669. }
  670. /**
  671. * Returns the index of the sheet by his name
  672. *
  673. * @param name the sheet name
  674. * @return index of the sheet (0 based)
  675. */
  676. @Override
  677. public int getSheetIndex(String name) {
  678. return workbook.getSheetIndex(name);
  679. }
  680. /**
  681. * Returns the index of the given sheet
  682. *
  683. * @param sheet the sheet to look up
  684. * @return index of the sheet (0 based). <tt>-1</tt> if not found
  685. */
  686. @Override
  687. public int getSheetIndex(Sheet sheet) {
  688. return _sheets.indexOf(sheet);
  689. }
  690. /**
  691. * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
  692. * the high level representation. Use this to create new sheets.
  693. *
  694. * @return HSSFSheet representing the new sheet.
  695. */
  696. @Override
  697. public HSSFSheet createSheet() {
  698. HSSFSheet sheet = new HSSFSheet(this);
  699. _sheets.add(sheet);
  700. workbook.setSheetName(_sheets.size() - 1, "Sheet" + (_sheets.size() - 1));
  701. boolean isOnlySheet = _sheets.size() == 1;
  702. sheet.setSelected(isOnlySheet);
  703. sheet.setActive(isOnlySheet);
  704. return sheet;
  705. }
  706. /**
  707. * create an HSSFSheet from an existing sheet in the HSSFWorkbook.
  708. *
  709. * @return HSSFSheet representing the cloned sheet.
  710. */
  711. @Override
  712. public HSSFSheet cloneSheet(int sheetIndex) {
  713. validateSheetIndex(sheetIndex);
  714. HSSFSheet srcSheet = _sheets.get(sheetIndex);
  715. String srcName = workbook.getSheetName(sheetIndex);
  716. HSSFSheet clonedSheet = srcSheet.cloneSheet(this);
  717. clonedSheet.setSelected(false);
  718. clonedSheet.setActive(false);
  719. String name = getUniqueSheetName(srcName);
  720. int newSheetIndex = _sheets.size();
  721. _sheets.add(clonedSheet);
  722. workbook.setSheetName(newSheetIndex, name);
  723. // Check this sheet has an autofilter, (which has a built-in NameRecord at workbook level)
  724. int filterDbNameIndex = findExistingBuiltinNameRecordIdx(sheetIndex, NameRecord.BUILTIN_FILTER_DB);
  725. if (filterDbNameIndex != -1) {
  726. NameRecord newNameRecord = workbook.cloneFilter(filterDbNameIndex, newSheetIndex);
  727. HSSFName newName = new HSSFName(this, newNameRecord);
  728. names.add(newName);
  729. }
  730. // TODO - maybe same logic required for other/all built-in name records
  731. // workbook.cloneDrawings(clonedSheet.getSheet());
  732. return clonedSheet;
  733. }
  734. private String getUniqueSheetName(String srcName) {
  735. int uniqueIndex = 2;
  736. String baseName = srcName;
  737. int bracketPos = srcName.lastIndexOf('(');
  738. if (bracketPos > 0 && srcName.endsWith(")")) {
  739. String suffix = srcName.substring(bracketPos + 1, srcName.length() - ")".length());
  740. try {
  741. uniqueIndex = Integer.parseInt(suffix.trim());
  742. uniqueIndex++;
  743. baseName = srcName.substring(0, bracketPos).trim();
  744. } catch (NumberFormatException e) {
  745. // contents of brackets not numeric
  746. }
  747. }
  748. while (true) {
  749. // Try and find the next sheet name that is unique
  750. String index = Integer.toString(uniqueIndex++);
  751. String name;
  752. if (baseName.length() + index.length() + 2 < 31) {
  753. name = baseName + " (" + index + ")";
  754. } else {
  755. name = baseName.substring(0, 31 - index.length() - 2) + "(" + index + ")";
  756. }
  757. //If the sheet name is unique, then set it otherwise move on to the next number.
  758. if (workbook.getSheetIndex(name) == -1) {
  759. return name;
  760. }
  761. }
  762. }
  763. /**
  764. * Create a new sheet for this Workbook and return the high level representation.
  765. * Use this to create new sheets.
  766. *
  767. * <p>
  768. * Note that Excel allows sheet names up to 31 chars in length but other applications
  769. * (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars,
  770. * others - truncate such names to 31 character.
  771. * </p>
  772. * <p>
  773. * POI's SpreadsheetAPI silently truncates the input argument to 31 characters.
  774. * Example:
  775. *
  776. * <pre><code>
  777. * Sheet sheet = workbook.createSheet("My very long sheet name which is longer than 31 chars"); // will be truncated
  778. * assert 31 == sheet.getSheetName().length();
  779. * assert "My very long sheet name which i" == sheet.getSheetName();
  780. * </code></pre>
  781. * </p>
  782. * <p>
  783. * Except the 31-character constraint, Excel applies some other rules:
  784. * <p>
  785. * Sheet name MUST be unique in the workbook and MUST NOT contain the any of the following characters:
  786. * <ul>
  787. * <li> 0x0000 </li>
  788. * <li> 0x0003 </li>
  789. * <li> colon (:) </li>
  790. * <li> backslash (\) </li>
  791. * <li> asterisk (*) </li>
  792. * <li> question mark (?) </li>
  793. * <li> forward slash (/) </li>
  794. * <li> opening square bracket ([) </li>
  795. * <li> closing square bracket (]) </li>
  796. * </ul>
  797. * The string MUST NOT begin or end with the single quote (') character.
  798. * </p>
  799. *
  800. * @param sheetname sheetname to set for the sheet.
  801. * @return Sheet representing the new sheet.
  802. * @throws IllegalArgumentException if the name is null or invalid
  803. * or workbook already contains a sheet with this name
  804. * @see WorkbookUtil#createSafeSheetName(String nameProposal)
  805. */
  806. @Override
  807. public HSSFSheet createSheet(String sheetname) {
  808. if (sheetname == null) {
  809. throw new IllegalArgumentException("sheetName must not be null");
  810. }
  811. if (workbook.doesContainsSheetName(sheetname, _sheets.size())) {
  812. throw new IllegalArgumentException("The workbook already contains a sheet named '" + sheetname + "'");
  813. }
  814. HSSFSheet sheet = new HSSFSheet(this);
  815. workbook.setSheetName(_sheets.size(), sheetname);
  816. _sheets.add(sheet);
  817. boolean isOnlySheet = _sheets.size() == 1;
  818. sheet.setSelected(isOnlySheet);
  819. sheet.setActive(isOnlySheet);
  820. return sheet;
  821. }
  822. /**
  823. * Returns an iterator of the sheets in the workbook
  824. * in sheet order. Includes hidden and very hidden sheets.
  825. *
  826. * @return an iterator of the sheets.
  827. */
  828. @Override
  829. public Iterator<Sheet> sheetIterator() {
  830. return new SheetIterator<>();
  831. }
  832. /**
  833. * Alias for {@link #sheetIterator()} to allow
  834. * foreach loops
  835. */
  836. @Override
  837. public Iterator<Sheet> iterator() {
  838. return sheetIterator();
  839. }
  840. private final class SheetIterator<T extends Sheet> implements Iterator<T> {
  841. final private Iterator<T> it;
  842. private T cursor;
  843. @SuppressWarnings("unchecked")
  844. public SheetIterator() {
  845. it = (Iterator<T>) _sheets.iterator();
  846. }
  847. @Override
  848. public boolean hasNext() {
  849. return it.hasNext();
  850. }
  851. @Override
  852. public T next() throws NoSuchElementException {
  853. cursor = it.next();
  854. return cursor;
  855. }
  856. /**
  857. * Unexpected behavior may occur if sheets are reordered after iterator
  858. * has been created. Support for the remove method may be added in the future
  859. * if someone can figure out a reliable implementation.
  860. */
  861. @Override
  862. public void remove() throws IllegalStateException {
  863. throw new UnsupportedOperationException("remove method not supported on HSSFWorkbook.iterator(). " +
  864. "Use Sheet.removeSheetAt(int) instead.");
  865. }
  866. }
  867. /**
  868. * get the number of spreadsheets in the workbook (this will be three after serialization)
  869. *
  870. * @return number of sheets
  871. */
  872. @Override
  873. public int getNumberOfSheets() {
  874. return _sheets.size();
  875. }
  876. private HSSFSheet[] getSheets() {
  877. HSSFSheet[] result = new HSSFSheet[_sheets.size()];
  878. _sheets.toArray(result);
  879. return result;
  880. }
  881. /**
  882. * Get the HSSFSheet object at the given index.
  883. *
  884. * @param index of the sheet number (0-based physical &amp; logical)
  885. * @return HSSFSheet at the provided index
  886. * @throws IllegalArgumentException if the index is out of range (index
  887. * &lt; 0 || index &gt;= getNumberOfSheets()).
  888. */
  889. @Override
  890. public HSSFSheet getSheetAt(int index) {
  891. validateSheetIndex(index);
  892. return _sheets.get(index);
  893. }
  894. /**
  895. * Get sheet with the given name (case insensitive match)
  896. *
  897. * @param name of the sheet
  898. * @return HSSFSheet with the name provided or <code>null</code> if it does not exist
  899. */
  900. @Override
  901. public HSSFSheet getSheet(String name) {
  902. HSSFSheet retval = null;
  903. for (int k = 0; k < _sheets.size(); k++) {
  904. String sheetname = workbook.getSheetName(k);
  905. if (sheetname.equalsIgnoreCase(name)) {
  906. retval = _sheets.get(k);
  907. }
  908. }
  909. return retval;
  910. }
  911. /**
  912. * Removes sheet at the given index.<p>
  913. * <p>
  914. * Care must be taken if the removed sheet is the currently active or only selected sheet in
  915. * the workbook. There are a few situations when Excel must have a selection and/or active
  916. * sheet. (For example when printing - see Bug 40414).<br>
  917. * <p>
  918. * This method makes sure that if the removed sheet was active, another sheet will become
  919. * active in its place. Furthermore, if the removed sheet was the only selected sheet, another
  920. * sheet will become selected. The newly active/selected sheet will have the same index, or
  921. * one less if the removed sheet was the last in the workbook.
  922. *
  923. * @param index of the sheet (0-based)
  924. */
  925. @Override
  926. public void removeSheetAt(int index) {
  927. validateSheetIndex(index);
  928. boolean wasSelected = getSheetAt(index).isSelected();
  929. _sheets.remove(index);
  930. workbook.removeSheet(index);
  931. // set the remaining active/selected sheet
  932. int nSheets = _sheets.size();
  933. if (nSheets < 1) {
  934. // nothing more to do if there are no sheets left
  935. return;
  936. }
  937. // the index of the closest remaining sheet to the one just deleted
  938. int newSheetIndex = index;
  939. if (newSheetIndex >= nSheets) {
  940. newSheetIndex = nSheets - 1;
  941. }
  942. if (wasSelected) {
  943. boolean someOtherSheetIsStillSelected = false;
  944. for (int i = 0; i < nSheets; i++) {
  945. if (getSheetAt(i).isSelected()) {
  946. someOtherSheetIsStillSelected = true;
  947. break;
  948. }
  949. }
  950. if (!someOtherSheetIsStillSelected) {
  951. setSelectedTab(newSheetIndex);
  952. }
  953. }
  954. // adjust active sheet
  955. int active = getActiveSheetIndex();
  956. if (active == index) {
  957. // removed sheet was the active one, reset active sheet if there is still one left now
  958. setActiveSheet(newSheetIndex);
  959. } else if (active > index) {
  960. // removed sheet was below the active one => active is one less now
  961. setActiveSheet(active - 1);
  962. }
  963. }
  964. /**
  965. * determine whether the Excel GUI will backup the workbook when saving.
  966. *
  967. * @param backupValue true to indicate a backup will be performed.
  968. */
  969. public void setBackupFlag(boolean backupValue) {
  970. BackupRecord backupRecord = workbook.getBackupRecord();
  971. backupRecord.setBackup(backupValue ? (short) 1
  972. : (short) 0);
  973. }
  974. /**
  975. * determine whether the Excel GUI will backup the workbook when saving.
  976. *
  977. * @return the current setting for backups.
  978. */
  979. public boolean getBackupFlag() {
  980. BackupRecord backupRecord = workbook.getBackupRecord();
  981. return backupRecord.getBackup() != 0;
  982. }
  983. int findExistingBuiltinNameRecordIdx(int sheetIndex, byte builtinCode) {
  984. for (int defNameIndex = 0; defNameIndex < names.size(); defNameIndex++) {
  985. NameRecord r = workbook.getNameRecord(defNameIndex);
  986. if (r == null) {
  987. throw new RuntimeException("Unable to find all defined names to iterate over");
  988. }
  989. if (!r.isBuiltInName() || r.getBuiltInName() != builtinCode) {
  990. continue;
  991. }
  992. if (r.getSheetNumber() - 1 == sheetIndex) {
  993. return defNameIndex;
  994. }
  995. }
  996. return -1;
  997. }
  998. HSSFName createBuiltInName(byte builtinCode, int sheetIndex) {
  999. NameRecord nameRecord =
  1000. workbook.createBuiltInName(builtinCode, sheetIndex + 1);
  1001. HSSFName newName = new HSSFName(this, nameRecord, null);
  1002. names.add(newName);
  1003. return newName;
  1004. }
  1005. HSSFName getBuiltInName(byte builtinCode, int sheetIndex) {
  1006. int index = findExistingBuiltinNameRecordIdx(sheetIndex, builtinCode);
  1007. return (index < 0) ? null : names.get(index);
  1008. }
  1009. /**
  1010. * create a new Font and add it to the workbook's font table
  1011. *
  1012. * @return new font object
  1013. */
  1014. @Override
  1015. public HSSFFont createFont() {
  1016. /*FontRecord font =*/
  1017. workbook.createNewFont();
  1018. int fontindex = getNumberOfFonts() - 1;
  1019. if (fontindex > 3) {
  1020. fontindex++; // THERE IS NO FOUR!!
  1021. }
  1022. if (fontindex >= Short.MAX_VALUE) {
  1023. throw new IllegalArgumentException("Maximum number of fonts was exceeded");
  1024. }
  1025. // Ask getFontAt() to build it for us,
  1026. // so it gets properly cached
  1027. return getFontAt(fontindex);
  1028. }
  1029. /**
  1030. * Finds a font that matches the one with the supplied attributes
  1031. */
  1032. @Override
  1033. public HSSFFont findFont(boolean bold, short color, short fontHeight,
  1034. String name, boolean italic, boolean strikeout,
  1035. short typeOffset, byte underline) {
  1036. int numberOfFonts = getNumberOfFonts();
  1037. for (int i = 0; i <= numberOfFonts; i++) {
  1038. // Remember - there is no 4!
  1039. if (i == 4) {
  1040. continue;
  1041. }
  1042. HSSFFont hssfFont = getFontAt(i);
  1043. if (hssfFont.getBold() == bold
  1044. && hssfFont.getColor() == color
  1045. && hssfFont.getFontHeight() == fontHeight
  1046. && hssfFont.getFontName().equals(name)
  1047. && hssfFont.getItalic() == italic
  1048. && hssfFont.getStrikeout() == strikeout
  1049. && hssfFont.getTypeOffset() == typeOffset
  1050. && hssfFont.getUnderline() == underline) {
  1051. return hssfFont;
  1052. }
  1053. }
  1054. return null;
  1055. }
  1056. @Override
  1057. public int getNumberOfFonts() {
  1058. return workbook.getNumberOfFontRecords();
  1059. }
  1060. @Override
  1061. @Deprecated
  1062. @Removal(version="6.0.0")
  1063. public int getNumberOfFontsAsInt() {
  1064. return getNumberOfFonts();
  1065. }
  1066. @Override
  1067. public HSSFFont getFontAt(int idx) {
  1068. if (fonts == null) {
  1069. fonts = new HashMap<>();
  1070. }
  1071. // So we don't confuse users, give them back
  1072. // the same object every time, but create
  1073. // them lazily
  1074. Integer sIdx = idx;
  1075. if (fonts.containsKey(sIdx)) {
  1076. return fonts.get(sIdx);
  1077. }
  1078. FontRecord font = workbook.getFontRecordAt(idx);
  1079. HSSFFont retval = new HSSFFont(idx, font);
  1080. fonts.put(sIdx, retval);
  1081. return retval;
  1082. }
  1083. /**
  1084. * Reset the fonts cache, causing all new calls
  1085. * to getFontAt() to create new objects.
  1086. * Should only be called after deleting fonts,
  1087. * and that's not something you should normally do
  1088. */
  1089. void resetFontCache() {
  1090. fonts = new HashMap<>();
  1091. }
  1092. /**
  1093. * Create a new Cell style and add it to the workbook's style table.
  1094. * You can define up to 4000 unique styles in a .xls workbook.
  1095. *
  1096. * @return the new Cell Style object
  1097. * @throws IllegalStateException if the number of cell styles exceeded the limit for this type of Workbook.
  1098. */
  1099. @Override
  1100. public HSSFCellStyle createCellStyle() {
  1101. if (workbook.getNumExFormats() == MAX_STYLES) {
  1102. throw new IllegalStateException("The maximum number of cell styles was exceeded. " +
  1103. "You can define up to 4000 styles in a .xls workbook");
  1104. }
  1105. ExtendedFormatRecord xfr = workbook.createCellXF();
  1106. short index = (short) (getNumCellStyles() - 1);
  1107. return new HSSFCellStyle(index, xfr, this);
  1108. }
  1109. /**
  1110. * get the number of styles the workbook contains
  1111. *
  1112. * @return count of cell styles
  1113. */
  1114. @Override
  1115. public int getNumCellStyles() {
  1116. return workbook.getNumExFormats();
  1117. }
  1118. /**
  1119. * get the cell style object at the given index
  1120. *
  1121. * @param idx index within the set of styles
  1122. * @return HSSFCellStyle object at the index
  1123. */
  1124. @Override
  1125. public HSSFCellStyle getCellStyleAt(int idx) {
  1126. ExtendedFormatRecord xfr = workbook.getExFormatAt(idx);
  1127. return new HSSFCellStyle((short) idx, xfr, this);
  1128. }
  1129. /**
  1130. * Closes the underlying {@link POIFSFileSystem} from which
  1131. * the Workbook was read, if any.
  1132. *
  1133. * <p>Once this has been called, no further
  1134. * operations, updates or reads should be performed on the
  1135. * Workbook.
  1136. */
  1137. @Override
  1138. public void close() throws IOException {
  1139. super.close();
  1140. }
  1141. /**
  1142. * Write out this workbook to the currently open {@link File} via the
  1143. * writeable {@link POIFSFileSystem} it was opened as.
  1144. *
  1145. * <p>This will fail (with an {@link IllegalStateException} if the
  1146. * Workbook was opened read-only, opened from an {@link InputStream}
  1147. * instead of a File, or if this is not the root document. For those cases,
  1148. * you must use {@link #write(OutputStream)} or {@link #write(File)} to
  1149. * write to a brand new document.
  1150. */
  1151. @Override
  1152. public void write() throws IOException {
  1153. validateInPlaceWritePossible();
  1154. final DirectoryNode dir = getDirectory();
  1155. // Update the Workbook stream in the file
  1156. DocumentNode workbookNode = (DocumentNode) dir.getEntry(
  1157. getWorkbookDirEntryName(dir));
  1158. POIFSDocument workbookDoc = new POIFSDocument(workbookNode);
  1159. workbookDoc.replaceContents(new ByteArrayInputStream(getBytes()));
  1160. // Update the properties streams in the file
  1161. writeProperties();
  1162. // Sync with the File on disk
  1163. dir.getFileSystem().writeFilesystem();
  1164. }
  1165. /**
  1166. * Method write - write out this workbook to a new {@link File}. Constructs
  1167. * a new POI POIFSFileSystem, passes in the workbook binary representation and
  1168. * writes it out. If the file exists, it will be replaced, otherwise a new one
  1169. * will be created.
  1170. * <p>
  1171. * Note that you cannot write to the currently open File using this method.
  1172. * If you opened your Workbook from a File, you <i>must</i> use the {@link #write()}
  1173. * method instead!
  1174. *
  1175. * @param newFile The new File you wish to write the XLS to
  1176. * @throws IOException if anything can't be written.
  1177. * @see POIFSFileSystem
  1178. */
  1179. @Override
  1180. public void write(File newFile) throws IOException {
  1181. try (POIFSFileSystem fs = POIFSFileSystem.create(newFile)) {
  1182. write(fs);
  1183. fs.writeFilesystem();
  1184. }
  1185. }
  1186. /**
  1187. * Method write - write out this workbook to an {@link OutputStream}. Constructs
  1188. * a new POI POIFSFileSystem, passes in the workbook binary representation and
  1189. * writes it out.
  1190. * <p>
  1191. * If {@code stream} is a {@link FileOutputStream} on a networked drive
  1192. * or has a high cost/latency associated with each written byte,
  1193. * consider wrapping the OutputStream in a {@link BufferedOutputStream}
  1194. * to improve write performance.
  1195. *
  1196. * @param stream - the java OutputStream you wish to write the XLS to
  1197. * @throws IOException if anything can't be written.
  1198. * @see POIFSFileSystem
  1199. */
  1200. @Override
  1201. public void write(OutputStream stream) throws IOException {
  1202. try (POIFSFileSystem fs = new POIFSFileSystem()) {
  1203. write(fs);
  1204. fs.writeFilesystem(stream);
  1205. }
  1206. }
  1207. /**
  1208. * Writes the workbook out to a brand new, empty POIFS
  1209. */
  1210. private void write(POIFSFileSystem fs) throws IOException {
  1211. // For tracking what we've written out, used if we're
  1212. // going to be preserving nodes
  1213. List<String> excepts = new ArrayList<>(1);
  1214. // Write out the Workbook stream
  1215. fs.createDocument(new ByteArrayInputStream(getBytes()), "Workbook");
  1216. // Write out our HPFS properties, if we have them
  1217. writeProperties(fs, excepts);
  1218. if (preserveNodes) {
  1219. // Don't write out the old Workbook, we'll be doing our new one
  1220. // If the file had an "incorrect" name for the workbook stream,
  1221. // don't write the old one as we'll use the correct name shortly
  1222. excepts.addAll(Arrays.asList(WORKBOOK_DIR_ENTRY_NAMES));
  1223. // summary information has been already written via writeProperties and might go in a
  1224. // different stream, if the file is cryptoapi encrypted
  1225. excepts.addAll(Arrays.asList(
  1226. DocumentSummaryInformation.DEFAULT_STREAM_NAME,
  1227. SummaryInformation.DEFAULT_STREAM_NAME,
  1228. getEncryptedPropertyStreamName()
  1229. ));
  1230. // Copy over all the other nodes to our new poifs
  1231. EntryUtils.copyNodes(
  1232. new FilteringDirectoryNode(getDirectory(), excepts)
  1233. , new FilteringDirectoryNode(fs.getRoot(), excepts)
  1234. );
  1235. // YK: preserve StorageClsid, it is important for embedded workbooks,
  1236. // see Bugzilla 47920
  1237. fs.getRoot().setStorageClsid(getDirectory().getStorageClsid());
  1238. }
  1239. }
  1240. /**
  1241. * Totals the sizes of all sheet records and eventually serializes them
  1242. */
  1243. private static final class SheetRecordCollector implements RecordVisitor {
  1244. private List<Record> _list;
  1245. private int _totalSize;
  1246. public SheetRecordCollector() {
  1247. _totalSize = 0;
  1248. _list = new ArrayList<>(128);
  1249. }
  1250. public int getTotalSize() {
  1251. return _totalSize;
  1252. }
  1253. @Override
  1254. public void visitRecord(Record r) {
  1255. _list.add(r);
  1256. _totalSize += r.getRecordSize();
  1257. }
  1258. public int serialize(int offset, byte[] data) {
  1259. int result = 0;
  1260. for (Record rec : _list) {
  1261. result += rec.serialize(offset + result, data);
  1262. }
  1263. return result;
  1264. }
  1265. }
  1266. /**
  1267. * Method getBytes - get the bytes of just the HSSF portions of the XLS file.
  1268. * Use this to construct a POI POIFSFileSystem yourself.
  1269. *
  1270. * @return byte[] array containing the binary representation of this workbook and all contained
  1271. * sheets, rows, cells, etc.
  1272. */
  1273. public byte[] getBytes() {
  1274. LOGGER.atDebug().log("HSSFWorkbook.getBytes()");
  1275. HSSFSheet[] sheets = getSheets();
  1276. int nSheets = sheets.length;
  1277. updateEncryptionInfo();
  1278. // before getting the workbook size we must tell the sheets that
  1279. // serialization is about to occur.
  1280. workbook.preSerialize();
  1281. for (HSSFSheet sheet : sheets) {
  1282. sheet.getSheet().preSerialize();
  1283. sheet.preSerialize();
  1284. }
  1285. int totalsize = workbook.getSize();
  1286. // pre-calculate all the sheet sizes and set BOF indexes
  1287. SheetRecordCollector[] srCollectors = new SheetRecordCollector[nSheets];
  1288. for (int k = 0; k < nSheets; k++) {
  1289. workbook.setSheetBof(k, totalsize);
  1290. SheetRecordCollector src = new SheetRecordCollector();
  1291. sheets[k].getSheet().visitContainedRecords(src, totalsize);
  1292. totalsize += src.getTotalSize();
  1293. srCollectors[k] = src;
  1294. }
  1295. byte[] retval = new byte[totalsize];
  1296. int pos = workbook.serialize(0, retval);
  1297. for (int k = 0; k < nSheets; k++) {
  1298. SheetRecordCollector src = srCollectors[k];
  1299. int serializedSize = src.serialize(pos, retval);
  1300. if (serializedSize != src.getTotalSize()) {
  1301. // Wrong offset values have been passed in the call to setSheetBof() above.
  1302. // For books with more than one sheet, this discrepancy would cause excel
  1303. // to report errors and loose data while reading the workbook
  1304. throw new IllegalStateException("Actual serialized sheet size (" + serializedSize
  1305. + ") differs from pre-calculated size (" + src.getTotalSize()
  1306. + ") for sheet (" + k + ")");
  1307. // TODO - add similar sanity check to ensure that Sheet.serializeIndexRecord() does not write mis-aligned offsets either
  1308. }
  1309. pos += serializedSize;
  1310. }
  1311. encryptBytes(retval);
  1312. return retval;
  1313. }
  1314. @SuppressWarnings("resource")
  1315. void encryptBytes(byte[] buf) {
  1316. EncryptionInfo ei = getEncryptionInfo();
  1317. if (ei == null) {
  1318. return;
  1319. }
  1320. Encryptor enc = ei.getEncryptor();
  1321. int initialOffset = 0;
  1322. LittleEndianByteArrayInputStream plain = new LittleEndianByteArrayInputStream(buf, 0); // NOSONAR
  1323. LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0); // NOSONAR
  1324. enc.setChunkSize(Biff8DecryptingStream.RC4_REKEYING_INTERVAL);
  1325. byte[] tmp = new byte[1024];
  1326. try {
  1327. ChunkedCipherOutputStream os = enc.getDataStream(leos, initialOffset);
  1328. int totalBytes = 0;
  1329. while (totalBytes < buf.length) {
  1330. IOUtils.readFully(plain, tmp, 0, 4);
  1331. final int sid = LittleEndian.getUShort(tmp, 0);
  1332. final int len = LittleEndian.getUShort(tmp, 2);
  1333. boolean isPlain = Biff8DecryptingStream.isNeverEncryptedRecord(sid);
  1334. os.setNextRecordSize(len, isPlain);
  1335. os.writePlain(tmp, 0, 4);
  1336. if (sid == BoundSheetRecord.sid) {
  1337. // special case for the field_1_position_of_BOF (=lbPlyPos) field of
  1338. // the BoundSheet8 record which must be unencrypted
  1339. byte[] bsrBuf = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH);
  1340. plain.readFully(bsrBuf);
  1341. os.writePlain(bsrBuf, 0, 4);
  1342. os.write(bsrBuf, 4, len - 4);
  1343. } else {
  1344. int todo = len;
  1345. while (todo > 0) {
  1346. int nextLen = Math.min(todo, tmp.length);
  1347. plain.readFully(tmp, 0, nextLen);
  1348. if (isPlain) {
  1349. os.writePlain(tmp, 0, nextLen);
  1350. } else {
  1351. os.write(tmp, 0, nextLen);
  1352. }
  1353. todo -= nextLen;
  1354. }
  1355. }
  1356. totalBytes += 4 + len;
  1357. }
  1358. os.close();
  1359. } catch (Exception e) {
  1360. throw new EncryptedDocumentException(e);
  1361. }
  1362. }
  1363. @Internal
  1364. public InternalWorkbook getWorkbook() {
  1365. return workbook;
  1366. }
  1367. @Override
  1368. public int getNumberOfNames() {
  1369. return names.size();
  1370. }
  1371. @Override
  1372. public HSSFName getName(String name) {
  1373. int nameIndex = getNameIndex(name);
  1374. if (nameIndex < 0) {
  1375. return null;
  1376. }
  1377. return names.get(nameIndex);
  1378. }
  1379. @Override
  1380. public List<HSSFName> getNames(String name) {
  1381. List<HSSFName> nameList = new ArrayList<>();
  1382. for (HSSFName nr : names) {
  1383. if (nr.getNameName().equals(name)) {
  1384. nameList.add(nr);
  1385. }
  1386. }
  1387. return Collections.unmodifiableList(nameList);
  1388. }
  1389. HSSFName getNameAt(int nameIndex) {
  1390. int nNames = names.size();
  1391. if (nNames < 1) {
  1392. throw new IllegalStateException("There are no defined names in this workbook");
  1393. }
  1394. if (nameIndex < 0 || nameIndex > nNames) {
  1395. throw new IllegalArgumentException("Specified name index " + nameIndex
  1396. + " is outside the allowable range (0.." + (nNames - 1) + ").");
  1397. }
  1398. return names.get(nameIndex);
  1399. }
  1400. @Override
  1401. public List<HSSFName> getAllNames() {
  1402. return Collections.unmodifiableList(names);
  1403. }
  1404. public NameRecord getNameRecord(int nameIndex) {
  1405. return getWorkbook().getNameRecord(nameIndex);
  1406. }
  1407. /**
  1408. * gets the named range name
  1409. *
  1410. * @param index the named range index (0 based)
  1411. * @return named range name
  1412. */
  1413. public String getNameName(int index) {
  1414. return getNameAt(index).getNameName();
  1415. }
  1416. /**
  1417. * Sets the printarea for the sheet provided
  1418. * <p>
  1419. * i.e. Reference = $A$1:$B$2
  1420. *
  1421. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  1422. * @param reference Valid name Reference for the Print Area
  1423. */
  1424. @Override
  1425. public void setPrintArea(int sheetIndex, String reference) {
  1426. NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
  1427. if (name == null) {
  1428. name = workbook.createBuiltInName(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
  1429. // adding one here because 0 indicates a global named region; doesn't make sense for print areas
  1430. }
  1431. String[] parts = COMMA_PATTERN.split(reference);
  1432. StringBuilder sb = new StringBuilder(32);
  1433. for (int i = 0; i < parts.length; i++) {
  1434. if (i > 0) {
  1435. sb.append(",");
  1436. }
  1437. SheetNameFormatter.appendFormat(sb, getSheetName(sheetIndex));
  1438. sb.append("!");
  1439. sb.append(parts[i]);
  1440. }
  1441. name.setNameDefinition(HSSFFormulaParser.parse(sb.toString(), this, FormulaType.NAMEDRANGE, sheetIndex));
  1442. }
  1443. /**
  1444. * For the Convenience of Java Programmers maintaining pointers.
  1445. *
  1446. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  1447. * @param startColumn Column to begin printarea
  1448. * @param endColumn Column to end the printarea
  1449. * @param startRow Row to begin the printarea
  1450. * @param endRow Row to end the printarea
  1451. * @see #setPrintArea(int, String)
  1452. */
  1453. @Override
  1454. public void setPrintArea(int sheetIndex, int startColumn, int endColumn,
  1455. int startRow, int endRow) {
  1456. //using absolute references because they don't get copied and pasted anyway
  1457. CellReference cell = new CellReference(startRow, startColumn, true, true);
  1458. String reference = cell.formatAsString();
  1459. cell = new CellReference(endRow, endColumn, true, true);
  1460. reference = reference + ":" + cell.formatAsString();
  1461. setPrintArea(sheetIndex, reference);
  1462. }
  1463. /**
  1464. * Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified.
  1465. *
  1466. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
  1467. * @return String Null if no print area has been defined
  1468. */
  1469. @Override
  1470. public String getPrintArea(int sheetIndex) {
  1471. NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
  1472. //adding one here because 0 indicates a global named region; doesn't make sense for print areas
  1473. if (name == null) {
  1474. return null;
  1475. }
  1476. return HSSFFormulaParser.toFormulaString(this, name.getNameDefinition());
  1477. }
  1478. /**
  1479. * Delete the printarea for the sheet specified
  1480. *
  1481. * @param sheetIndex Zero-based sheet index (0 = First Sheet)
  1482. */
  1483. @Override
  1484. public void removePrintArea(int sheetIndex) {
  1485. getWorkbook().removeBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1);
  1486. }
  1487. /**
  1488. * creates a new named range and add it to the model
  1489. *
  1490. * @return named range high level
  1491. */
  1492. @Override
  1493. public HSSFName createName() {
  1494. NameRecord nameRecord = workbook.createName();
  1495. HSSFName newName = new HSSFName(this, nameRecord);
  1496. names.add(newName);
  1497. return newName;
  1498. }
  1499. int getNameIndex(String name) {
  1500. for (int k = 0; k < names.size(); k++) {
  1501. String nameName = getNameName(k);
  1502. if (nameName.equalsIgnoreCase(name)) {
  1503. return k;
  1504. }
  1505. }
  1506. return -1;
  1507. }
  1508. /**
  1509. * As {@link #getNameIndex(String)} is not necessarily unique
  1510. * (name + sheet index is unique), this method is more accurate.
  1511. *
  1512. * @param name the name whose index in the list of names of this workbook
  1513. * should be looked up.
  1514. * @return an index value >= 0 if the name was found; -1, if the name was
  1515. * not found
  1516. */
  1517. int getNameIndex(HSSFName name) {
  1518. for (int k = 0; k < names.size(); k++) {
  1519. if (name == names.get(k)) {
  1520. return k;
  1521. }
  1522. }
  1523. return -1;
  1524. }
  1525. void removeName(int index) {
  1526. names.remove(index);
  1527. workbook.removeName(index);
  1528. }
  1529. /**
  1530. * Returns the instance of HSSFDataFormat for this workbook.
  1531. *
  1532. * @return the HSSFDataFormat object
  1533. * @see FormatRecord
  1534. * @see Record
  1535. */
  1536. @Override
  1537. public HSSFDataFormat createDataFormat() {
  1538. if (formatter == null) {
  1539. formatter = new HSSFDataFormat(workbook);
  1540. }
  1541. return formatter;
  1542. }
  1543. /**
  1544. * Remove a name.
  1545. *
  1546. * @param name the name to remove.
  1547. */
  1548. @Override
  1549. public void removeName(Name name) {
  1550. int index = getNameIndex((HSSFName) name);
  1551. removeName(index);
  1552. }
  1553. public HSSFPalette getCustomPalette() {
  1554. return new HSSFPalette(workbook.getCustomPalette());
  1555. }
  1556. /**
  1557. * Test only. Do not use
  1558. */
  1559. public void insertChartRecord() {
  1560. int loc = workbook.findFirstRecordLocBySid(SSTRecord.sid);
  1561. byte[] data = {
  1562. (byte) 0x0F, (byte) 0x00, (byte) 0x00, (byte) 0xF0, (byte) 0x52,
  1563. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1564. (byte) 0x06, (byte) 0xF0, (byte) 0x18, (byte) 0x00, (byte) 0x00,
  1565. (byte) 0x00, (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x00,
  1566. (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02,
  1567. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00,
  1568. (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
  1569. (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00,
  1570. (byte) 0x33, (byte) 0x00, (byte) 0x0B, (byte) 0xF0, (byte) 0x12,
  1571. (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xBF, (byte) 0x00,
  1572. (byte) 0x08, (byte) 0x00, (byte) 0x08, (byte) 0x00, (byte) 0x81,
  1573. (byte) 0x01, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x08,
  1574. (byte) 0xC0, (byte) 0x01, (byte) 0x40, (byte) 0x00, (byte) 0x00,
  1575. (byte) 0x08, (byte) 0x40, (byte) 0x00, (byte) 0x1E, (byte) 0xF1,
  1576. (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0D,
  1577. (byte) 0x00, (byte) 0x00, (byte) 0x08, (byte) 0x0C, (byte) 0x00,
  1578. (byte) 0x00, (byte) 0x08, (byte) 0x17, (byte) 0x00, (byte) 0x00,
  1579. (byte) 0x08, (byte) 0xF7, (byte) 0x00, (byte) 0x00, (byte) 0x10,
  1580. };
  1581. UnknownRecord r = new UnknownRecord((short) 0x00EB, data);
  1582. workbook.getRecords().add(loc, r);
  1583. }
  1584. /**
  1585. * Spits out a list of all the drawing records in the workbook.
  1586. */
  1587. public void dumpDrawingGroupRecords(boolean fat) {
  1588. DrawingGroupRecord r = (DrawingGroupRecord) workbook.findFirstRecordBySid(DrawingGroupRecord.sid);
  1589. if (r == null) {
  1590. return;
  1591. }
  1592. r.decode();
  1593. List<EscherRecord> escherRecords = r.getEscherRecords();
  1594. PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()));
  1595. for (EscherRecord escherRecord : escherRecords) {
  1596. if (fat) {
  1597. System.out.println(escherRecord);
  1598. } else {
  1599. escherRecord.display(w, 0);
  1600. }
  1601. }
  1602. w.flush();
  1603. }
  1604. void initDrawings() {
  1605. DrawingManager2 mgr = workbook.findDrawingGroup();
  1606. if (mgr != null) {
  1607. for (HSSFSheet sh : _sheets) {
  1608. sh.getDrawingPatriarch();
  1609. }
  1610. } else {
  1611. workbook.createDrawingGroup();
  1612. }
  1613. }
  1614. /**
  1615. * Adds a picture to the workbook.
  1616. *
  1617. * @param pictureData The bytes of the picture
  1618. * @param format The format of the picture. One of <code>PICTURE_TYPE_*</code>
  1619. * @return the index to this picture (1 based).
  1620. * @see #PICTURE_TYPE_WMF
  1621. * @see #PICTURE_TYPE_EMF
  1622. * @see #PICTURE_TYPE_PICT
  1623. * @see #PICTURE_TYPE_PNG
  1624. * @see #PICTURE_TYPE_JPEG
  1625. * @see #PICTURE_TYPE_DIB
  1626. */
  1627. @SuppressWarnings("fallthrough")
  1628. @Override
  1629. public int addPicture(byte[] pictureData, int format) {
  1630. initDrawings();
  1631. byte[] uid = DigestUtils.md5(pictureData);
  1632. EscherBlipRecord blipRecord;
  1633. int blipSize;
  1634. short escherTag;
  1635. switch (format) {
  1636. case PICTURE_TYPE_WMF:
  1637. // remove first 22 bytes if file starts with the WMF placeable header
  1638. if (FileMagic.valueOf(pictureData) == FileMagic.WMF) {
  1639. pictureData = IOUtils.safelyClone(pictureData, 22, pictureData.length - 22, MAX_IMAGE_LENGTH);
  1640. }
  1641. // fall through
  1642. case PICTURE_TYPE_EMF:
  1643. EscherMetafileBlip blipRecordMeta = new EscherMetafileBlip();
  1644. blipRecord = blipRecordMeta;
  1645. blipRecordMeta.setUID(uid);
  1646. blipRecordMeta.setPictureData(pictureData);
  1647. // taken from libre office export, it won't open, if this is left to 0
  1648. blipRecordMeta.setFilter((byte) -2);
  1649. blipSize = blipRecordMeta.getCompressedSize() + 58;
  1650. escherTag = 0;
  1651. break;
  1652. default:
  1653. EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip();
  1654. blipRecord = blipRecordBitmap;
  1655. blipRecordBitmap.setUID(uid);
  1656. blipRecordBitmap.setMarker((byte) 0xFF);
  1657. blipRecordBitmap.setPictureData(pictureData);
  1658. blipSize = pictureData.length + 25;
  1659. escherTag = (short) 0xFF;
  1660. break;
  1661. }
  1662. blipRecord.setRecordId((short) (EscherBlipRecord.RECORD_ID_START + format));
  1663. switch (format) {
  1664. case PICTURE_TYPE_EMF:
  1665. blipRecord.setOptions(HSSFPictureData.MSOBI_EMF);
  1666. break;
  1667. case PICTURE_TYPE_WMF:
  1668. blipRecord.setOptions(HSSFPictureData.MSOBI_WMF);
  1669. break;
  1670. case PICTURE_TYPE_PICT:
  1671. blipRecord.setOptions(HSSFPictureData.MSOBI_PICT);
  1672. break;
  1673. case PICTURE_TYPE_PNG:
  1674. blipRecord.setOptions(HSSFPictureData.MSOBI_PNG);
  1675. break;
  1676. case PICTURE_TYPE_JPEG:
  1677. blipRecord.setOptions(HSSFPictureData.MSOBI_JPEG);
  1678. break;
  1679. case PICTURE_TYPE_DIB:
  1680. blipRecord.setOptions(HSSFPictureData.MSOBI_DIB);
  1681. break;
  1682. default:
  1683. throw new IllegalStateException("Unexpected picture format: " + format);
  1684. }
  1685. EscherBSERecord r = new EscherBSERecord();
  1686. r.setRecordId(EscherBSERecord.RECORD_ID);
  1687. r.setOptions((short) (0x0002 | (format << 4)));
  1688. r.setBlipTypeMacOS((byte) format);
  1689. r.setBlipTypeWin32((byte) format);
  1690. r.setUid(uid);
  1691. r.setTag(escherTag);
  1692. r.setSize(blipSize);
  1693. r.setRef(0);
  1694. r.setOffset(0);
  1695. r.setBlipRecord(blipRecord);
  1696. return workbook.addBSERecord(r);
  1697. }
  1698. /**
  1699. * Gets all pictures from the Workbook.
  1700. *
  1701. * @return the list of pictures (a list of {@link HSSFPictureData} objects.)
  1702. */
  1703. @Override
  1704. public List<HSSFPictureData> getAllPictures() {
  1705. // The drawing group record always exists at the top level, so we won't need to do this recursively.
  1706. List<HSSFPictureData> pictures = new ArrayList<>();
  1707. for (Record r : workbook.getRecords()) {
  1708. if (r instanceof AbstractEscherHolderRecord) {
  1709. ((AbstractEscherHolderRecord) r).decode();
  1710. List<EscherRecord> escherRecords = ((AbstractEscherHolderRecord) r).getEscherRecords();
  1711. searchForPictures(escherRecords, pictures);
  1712. }
  1713. }
  1714. return Collections.unmodifiableList(pictures);
  1715. }
  1716. /**
  1717. * Performs a recursive search for pictures in the given list of escher records.
  1718. *
  1719. * @param escherRecords the escher records.
  1720. * @param pictures the list to populate with the pictures.
  1721. */
  1722. private void searchForPictures(List<EscherRecord> escherRecords, List<HSSFPictureData> pictures) {
  1723. for (EscherRecord escherRecord : escherRecords) {
  1724. if (escherRecord instanceof EscherBSERecord) {
  1725. EscherBlipRecord blip = ((EscherBSERecord) escherRecord).getBlipRecord();
  1726. if (blip != null) {
  1727. // TODO: Some kind of structure.
  1728. HSSFPictureData picture = new HSSFPictureData(blip);
  1729. pictures.add(picture);
  1730. }
  1731. }
  1732. // Recursive call.
  1733. searchForPictures(escherRecord.getChildRecords(), pictures);
  1734. }
  1735. }
  1736. static Map<String, ClassID> getOleMap() {
  1737. Map<String, ClassID> olemap = new HashMap<>();
  1738. olemap.put("PowerPoint Document", ClassIDPredefined.POWERPOINT_V8.getClassID());
  1739. for (String str : WORKBOOK_DIR_ENTRY_NAMES) {
  1740. olemap.put(str, ClassIDPredefined.EXCEL_V7_WORKBOOK.getClassID());
  1741. }
  1742. // ... to be continued
  1743. return olemap;
  1744. }
  1745. /**
  1746. * Adds an OLE package manager object with the given POIFS to the sheet
  1747. *
  1748. * @param poiData an POIFS containing the embedded document, to be added
  1749. * @param label the label of the payload
  1750. * @param fileName the original filename
  1751. * @param command the command to open the payload
  1752. * @return the index of the added ole object
  1753. * @throws IOException if the object can't be embedded
  1754. */
  1755. public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command)
  1756. throws IOException {
  1757. DirectoryNode root = poiData.getRoot();
  1758. Map<String, ClassID> olemap = getOleMap();
  1759. for (Map.Entry<String, ClassID> entry : olemap.entrySet()) {
  1760. if (root.hasEntry(entry.getKey())) {
  1761. root.setStorageClsid(entry.getValue());
  1762. break;
  1763. }
  1764. }
  1765. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  1766. poiData.writeFilesystem(bos);
  1767. return addOlePackage(bos.toByteArray(), label, fileName, command);
  1768. }
  1769. @Override
  1770. public int addOlePackage(byte[] oleData, String label, String fileName, String command)
  1771. throws IOException {
  1772. // check if we were created by POIFS otherwise create a new dummy POIFS for storing the package data
  1773. if (initDirectory()) {
  1774. preserveNodes = true;
  1775. }
  1776. // get free MBD-Node
  1777. int storageId = 0;
  1778. DirectoryEntry oleDir = null;
  1779. do {
  1780. String storageStr = "MBD" + HexDump.toHex(++storageId);
  1781. if (!getDirectory().hasEntry(storageStr)) {
  1782. oleDir = getDirectory().createDirectory(storageStr);
  1783. oleDir.setStorageClsid(ClassIDPredefined.OLE_V1_PACKAGE.getClassID());
  1784. }
  1785. } while (oleDir == null);
  1786. Ole10Native.createOleMarkerEntry(oleDir);
  1787. Ole10Native oleNative = new Ole10Native(label, fileName, command, oleData);
  1788. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  1789. oleNative.writeOut(bos);
  1790. oleDir.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray()));
  1791. return storageId;
  1792. }
  1793. /**
  1794. * Adds the LinkTable records required to allow formulas referencing
  1795. * the specified external workbook to be added to this one. Allows
  1796. * formulas such as "[MyOtherWorkbook]Sheet3!$A$5" to be added to the
  1797. * file, for workbooks not already referenced.
  1798. *
  1799. * @param name The name the workbook will be referenced as in formulas
  1800. * @param workbook The open workbook to fetch the link required information from
  1801. */
  1802. @Override
  1803. public int linkExternalWorkbook(String name, Workbook workbook) {
  1804. return this.workbook.linkExternalWorkbook(name, workbook);
  1805. }
  1806. /**
  1807. * Is the workbook protected with a password (not encrypted)?
  1808. */
  1809. public boolean isWriteProtected() {
  1810. return this.workbook.isWriteProtected();
  1811. }
  1812. /**
  1813. * protect a workbook with a password (not encypted, just sets writeprotect
  1814. * flags and the password.
  1815. *
  1816. * @param password to set
  1817. */
  1818. public void writeProtectWorkbook(String password, String username) {
  1819. this.workbook.writeProtectWorkbook(password, username);
  1820. }
  1821. /**
  1822. * removes the write protect flag
  1823. */
  1824. public void unwriteProtectWorkbook() {
  1825. this.workbook.unwriteProtectWorkbook();
  1826. }
  1827. /**
  1828. * Gets all embedded OLE2 objects from the Workbook.
  1829. *
  1830. * @return the list of embedded objects (a list of {@link HSSFObjectData} objects.)
  1831. */
  1832. public List<HSSFObjectData> getAllEmbeddedObjects() {
  1833. List<HSSFObjectData> objects = new ArrayList<>();
  1834. for (HSSFSheet sheet : _sheets) {
  1835. getAllEmbeddedObjects(sheet, objects);
  1836. }
  1837. return Collections.unmodifiableList(objects);
  1838. }
  1839. /**
  1840. * Gets all embedded OLE2 objects from the Workbook.
  1841. *
  1842. * @param sheet embedded object attached to
  1843. * @param objects the list of embedded objects to populate.
  1844. */
  1845. private void getAllEmbeddedObjects(HSSFSheet sheet, List<HSSFObjectData> objects) {
  1846. HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
  1847. if (null == patriarch) {
  1848. return;
  1849. }
  1850. getAllEmbeddedObjects(patriarch, objects);
  1851. }
  1852. /**
  1853. * Recursively iterates a shape container to get all embedded objects.
  1854. *
  1855. * @param parent the parent.
  1856. * @param objects the list of embedded objects to populate.
  1857. */
  1858. private void getAllEmbeddedObjects(HSSFShapeContainer parent, List<HSSFObjectData> objects) {
  1859. for (HSSFShape shape : parent.getChildren()) {
  1860. if (shape instanceof HSSFObjectData) {
  1861. objects.add((HSSFObjectData) shape);
  1862. } else if (shape instanceof HSSFShapeContainer) {
  1863. getAllEmbeddedObjects((HSSFShapeContainer) shape, objects);
  1864. }
  1865. }
  1866. }
  1867. @Override
  1868. public HSSFCreationHelper getCreationHelper() {
  1869. return new HSSFCreationHelper(this);
  1870. }
  1871. /**
  1872. * Returns the locator of user-defined functions.
  1873. * The default instance extends the built-in functions with the Analysis Tool Pack
  1874. *
  1875. * @return the locator of user-defined functions
  1876. */
  1877. /*package*/ UDFFinder getUDFFinder() {
  1878. return _udfFinder;
  1879. }
  1880. /**
  1881. * Register a new toolpack in this workbook.
  1882. *
  1883. * @param toopack the toolpack to register
  1884. */
  1885. @Override
  1886. public void addToolPack(UDFFinder toopack) {
  1887. AggregatingUDFFinder udfs = (AggregatingUDFFinder) _udfFinder;
  1888. udfs.add(toopack);
  1889. }
  1890. /**
  1891. * Whether the application shall perform a full recalculation when the workbook is opened.
  1892. * <p>
  1893. * Typically you want to force formula recalculation when you modify cell formulas or values
  1894. * of a workbook previously created by Excel. When set to true, this flag will tell Excel
  1895. * that it needs to recalculate all formulas in the workbook the next time the file is opened.
  1896. * </p>
  1897. * <p>
  1898. * Note, that recalculation updates cached formula results and, thus, modifies the workbook.
  1899. * Depending on the version, Excel may prompt you with "Do you want to save the changes in <em>filename</em>?"
  1900. * on close.
  1901. * </p>
  1902. *
  1903. * @param value true if the application will perform a full recalculation of
  1904. * workbook values when the workbook is opened
  1905. * @since 3.8
  1906. */
  1907. @Override
  1908. public void setForceFormulaRecalculation(boolean value) {
  1909. InternalWorkbook iwb = getWorkbook();
  1910. RecalcIdRecord recalc = iwb.getRecalcId();
  1911. recalc.setEngineId(0);
  1912. }
  1913. /**
  1914. * Whether Excel will be asked to recalculate all formulas when the workbook is opened.
  1915. *
  1916. * @since 3.8
  1917. */
  1918. @Override
  1919. public boolean getForceFormulaRecalculation() {
  1920. InternalWorkbook iwb = getWorkbook();
  1921. RecalcIdRecord recalc = (RecalcIdRecord) iwb.findFirstRecordBySid(RecalcIdRecord.sid);
  1922. return recalc != null && recalc.getEngineId() != 0;
  1923. }
  1924. /**
  1925. * Changes an external referenced file to another file.
  1926. * A formula in Excel which references a cell in another file is saved in two parts:
  1927. * The referenced file is stored in an reference table. the row/cell information is saved separate.
  1928. * This method invocation will only change the reference in the lookup-table itself.
  1929. *
  1930. * @param oldUrl The old URL to search for and which is to be replaced
  1931. * @param newUrl The URL replacement
  1932. * @return true if the oldUrl was found and replaced with newUrl. Otherwise false
  1933. */
  1934. public boolean changeExternalReference(String oldUrl, String newUrl) {
  1935. return workbook.changeExternalReference(oldUrl, newUrl);
  1936. }
  1937. @Internal
  1938. public InternalWorkbook getInternalWorkbook() {
  1939. return workbook;
  1940. }
  1941. /**
  1942. * Returns the spreadsheet version (EXCLE97) of this workbook
  1943. *
  1944. * @return EXCEL97 SpreadsheetVersion enum
  1945. * @since 3.14 beta 2
  1946. */
  1947. @Override
  1948. public SpreadsheetVersion getSpreadsheetVersion() {
  1949. return SpreadsheetVersion.EXCEL97;
  1950. }
  1951. @Override
  1952. public EncryptionInfo getEncryptionInfo() {
  1953. FilePassRecord fpr = (FilePassRecord) workbook.findFirstRecordBySid(FilePassRecord.sid);
  1954. return (fpr != null) ? fpr.getEncryptionInfo() : null;
  1955. }
  1956. private void updateEncryptionInfo() {
  1957. // make sure, that we've read all the streams ...
  1958. readProperties();
  1959. FilePassRecord fpr = (FilePassRecord) workbook.findFirstRecordBySid(FilePassRecord.sid);
  1960. String password = Biff8EncryptionKey.getCurrentUserPassword();
  1961. WorkbookRecordList wrl = workbook.getWorkbookRecordList();
  1962. if (password == null) {
  1963. if (fpr != null) {
  1964. // need to remove password data
  1965. wrl.remove(fpr);
  1966. }
  1967. } else {
  1968. // create password record
  1969. if (fpr == null) {
  1970. fpr = new FilePassRecord(EncryptionMode.cryptoAPI);
  1971. wrl.add(1, fpr);
  1972. }
  1973. // check if the password has been changed
  1974. EncryptionInfo ei = fpr.getEncryptionInfo();
  1975. EncryptionVerifier ver = ei.getVerifier();
  1976. byte[] encVer = ver.getEncryptedVerifier();
  1977. Decryptor dec = ei.getDecryptor();
  1978. Encryptor enc = ei.getEncryptor();
  1979. try {
  1980. if (encVer == null || !dec.verifyPassword(password)) {
  1981. enc.confirmPassword(password);
  1982. } else {
  1983. byte[] verifier = dec.getVerifier();
  1984. byte[] salt = ver.getSalt();
  1985. enc.confirmPassword(password, null, null, verifier, salt, null);
  1986. }
  1987. } catch (GeneralSecurityException e) {
  1988. throw new EncryptedDocumentException("can't validate/update encryption setting", e);
  1989. }
  1990. }
  1991. }
  1992. @Override
  1993. public HSSFEvaluationWorkbook createEvaluationWorkbook() {
  1994. return HSSFEvaluationWorkbook.create(this);
  1995. }
  1996. }