/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.hssf.usermodel; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Stack; import org.apache.poi.POIDocument; import org.apache.poi.ddf.EscherBSERecord; import org.apache.poi.ddf.EscherBitmapBlip; import org.apache.poi.ddf.EscherBlipRecord; import org.apache.poi.ddf.EscherRecord; import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.AbstractEscherHolderRecord; import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.DrawingGroupRecord; import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord; import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.FontRecord; import org.apache.poi.hssf.record.LabelRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.NameRecord; import org.apache.poi.hssf.record.ObjRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RecordFactory; import org.apache.poi.hssf.record.SSTRecord; import org.apache.poi.hssf.record.UnicodeString; import org.apache.poi.hssf.record.UnknownRecord; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.formula.Area3DPtg; import org.apache.poi.hssf.record.formula.MemFuncPtg; import org.apache.poi.hssf.record.formula.NameXPtg; import org.apache.poi.hssf.record.formula.UnionPtg; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.hssf.util.SheetReferences; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; /** * High level representation of a workbook. This is the first object most users * will construct whether they are reading or writing a workbook. It is also the * top level object for creating new sheets/etc. * * @see org.apache.poi.hssf.model.Workbook * @see org.apache.poi.hssf.usermodel.HSSFSheet * @author Andrew C. Oliver (acoliver at apache dot org) * @author Glen Stampoultzis (glens at apache.org) * @author Shawn Laubach (slaubach at apache dot org) * @version 2.0-pre */ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.usermodel.Workbook { private static final int MAX_ROW = 0xFFFF; private static final short MAX_COLUMN = (short)0x00FF; private static final int DEBUG = POILogger.DEBUG; /** * used for compile-time performance/memory optimization. This determines the * initial capacity for the sheet collection. Its currently set to 3. * Changing it in this release will decrease performance * since you're never allowed to have more or less than three sheets! */ public final static int INITIAL_CAPACITY = 3; /** * this is the reference to the low level Workbook object */ private Workbook workbook; /** * this holds the HSSFSheet objects attached to this workbook */ protected List _sheets; /** * this holds the HSSFName objects attached to this workbook */ private ArrayList names; /** * this holds the HSSFFont objects attached to this workbook. * We only create these from the low level records as required. */ private Hashtable fonts; /** * holds whether or not to preserve other nodes in the POIFS. Used * for macros and embedded objects. */ private boolean preserveNodes; /** * Used to keep track of the data formatter so that all * createDataFormatter calls return the same one for a given * book. This ensures that updates from one places is visible * someplace else. */ private HSSFDataFormat formatter; /** * The policy to apply in the event of missing or * blank cells when fetching from a row. * See {@link MissingCellPolicy} */ private MissingCellPolicy missingCellPolicy = HSSFRow.RETURN_NULL_AND_BLANK; /** Extended windows meta file */ public static final int PICTURE_TYPE_EMF = 2; /** Windows Meta File */ public static final int PICTURE_TYPE_WMF = 3; /** Mac PICT format */ public static final int PICTURE_TYPE_PICT = 4; /** JPEG format */ public static final int PICTURE_TYPE_JPEG = 5; /** PNG format */ public static final int PICTURE_TYPE_PNG = 6; /** Device independant bitmap */ public static final int PICTURE_TYPE_DIB = 7; private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class); /** * Creates new HSSFWorkbook from scratch (start here!) * */ public HSSFWorkbook() { this(Workbook.createWorkbook()); } protected HSSFWorkbook( Workbook book ) { super(null, null); workbook = book; _sheets = new ArrayList( INITIAL_CAPACITY ); names = new ArrayList( INITIAL_CAPACITY ); } public HSSFWorkbook(POIFSFileSystem fs) throws IOException { this(fs,true); } /** * given a POI POIFSFileSystem object, read in its Workbook and populate the high and * low level models. If you're reading in a workbook...start here. * * @param fs the POI filesystem that contains the Workbook stream. * @param preserveNodes whether to preseve other nodes, such as * macros. This takes more memory, so only say yes if you * need to. If set, will store all of the POIFSFileSystem * in memory * @see org.apache.poi.poifs.filesystem.POIFSFileSystem * @exception IOException if the stream cannot be read */ public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes) throws IOException { this(fs.getRoot(), fs, preserveNodes); } /** * Normally, the Workbook will be in a POIFS Stream * called "Workbook". However, some weird XLS generators use "WORKBOOK" */ private static final String[] WORKBOOK_DIR_ENTRY_NAMES = { "Workbook", // as per BIFF8 spec "WORKBOOK", }; private static String getWorkbookDirEntryName(DirectoryNode directory) { String[] potentialNames = WORKBOOK_DIR_ENTRY_NAMES; for (int i = 0; i < potentialNames.length; i++) { String wbName = potentialNames[i]; try { directory.getEntry(wbName); return wbName; } catch (FileNotFoundException e) { // continue - to try other options } } // check for previous version of file format try { directory.getEntry("Book"); throw new IllegalArgumentException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. " + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)"); } catch (FileNotFoundException e) { // fall through } throw new IllegalArgumentException("The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. " + "Is it really an excel file?"); } /** * given a POI POIFSFileSystem object, and a specific directory * within it, read in its Workbook and populate the high and * low level models. If you're reading in a workbook...start here. * * @param directory the POI filesystem directory to process from * @param fs the POI filesystem that contains the Workbook stream. * @param preserveNodes whether to preseve other nodes, such as * macros. This takes more memory, so only say yes if you * need to. If set, will store all of the POIFSFileSystem * in memory * @see org.apache.poi.poifs.filesystem.POIFSFileSystem * @exception IOException if the stream cannot be read */ public HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes) throws IOException { super(directory, fs); String workbookName = getWorkbookDirEntryName(directory); this.preserveNodes = preserveNodes; // If we're not preserving nodes, don't track the // POIFS any more if(! preserveNodes) { this.filesystem = null; this.directory = null; } _sheets = new ArrayList(INITIAL_CAPACITY); names = new ArrayList(INITIAL_CAPACITY); // Grab the data from the workbook stream, however // it happens to be spelled. InputStream stream = directory.createDocumentInputStream(workbookName); List records = RecordFactory.createRecords(stream); workbook = Workbook.createWorkbook(records); setPropertiesFromWorkbook(workbook); int recOffset = workbook.getNumRecords(); int sheetNum = 0; // convert all LabelRecord records to LabelSSTRecord convertLabelRecords(records, recOffset); while (recOffset < records.size()) { Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset ); recOffset = sheet.getEofLoc()+1; // TODO - use better technique to keep track of the used records _sheets.add(new HSSFSheet(this, sheet)); } for (int i = 0 ; i < workbook.getNumNames() ; ++i){ HSSFName name = new HSSFName(this, workbook.getNameRecord(i)); names.add(name); } } public HSSFWorkbook(InputStream s) throws IOException { this(s,true); } /** * Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your * inputstream. * * @param s the POI filesystem that contains the Workbook stream. * @param preserveNodes whether to preseve other nodes, such as * macros. This takes more memory, so only say yes if you * need to. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem * @see #HSSFWorkbook(POIFSFileSystem) * @exception IOException if the stream cannot be read */ public HSSFWorkbook(InputStream s, boolean preserveNodes) throws IOException { this(new POIFSFileSystem(s), preserveNodes); } /** * used internally to set the workbook properties. */ private void setPropertiesFromWorkbook(Workbook book) { this.workbook = book; // none currently } /** * This is basically a kludge to deal with the now obsolete Label records. If * you have to read in a sheet that contains Label records, be aware that the rest * of the API doesn't deal with them, the low level structure only provides read-only * semi-immutable structures (the sets are there for interface conformance with NO * impelmentation). In short, you need to call this function passing it a reference * to the Workbook object. All labels will be converted to LabelSST records and their * contained strings will be written to the Shared String tabel (SSTRecord) within * the Workbook. * * @param wb sheet's matching low level Workbook structure containing the SSTRecord. * @see org.apache.poi.hssf.record.LabelRecord * @see org.apache.poi.hssf.record.LabelSSTRecord * @see org.apache.poi.hssf.record.SSTRecord */ private void convertLabelRecords(List records, int offset) { if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "convertLabelRecords called"); for (int k = offset; k < records.size(); k++) { Record rec = ( Record ) records.get(k); if (rec.getSid() == LabelRecord.sid) { LabelRecord oldrec = ( LabelRecord ) rec; records.remove(k); LabelSSTRecord newrec = new LabelSSTRecord(); int stringid = workbook.addSSTString(new UnicodeString(oldrec.getValue())); newrec.setRow(oldrec.getRow()); newrec.setColumn(oldrec.getColumn()); newrec.setXFIndex(oldrec.getXFIndex()); newrec.setSSTIndex(stringid); records.add(k, newrec); } } if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "convertLabelRecords exit"); } /** * Retrieves the current policy on what to do when * getting missing or blank cells from a row. * The default is to return blank and null cells. * {@link MissingCellPolicy} */ public MissingCellPolicy getMissingCellPolicy() { return missingCellPolicy; } /** * Sets the policy on what to do when * getting missing or blank cells from a row. * This will then apply to all calls to * {@link HSSFRow.getCell()}. See * {@link MissingCellPolicy} */ public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) { this.missingCellPolicy = missingCellPolicy; } /** * sets the order of appearance for a given sheet. * * @param sheetname the name of the sheet to reorder * @param pos the position that we want to insert the sheet into (0 based) */ public void setSheetOrder(String sheetname, int pos ) { _sheets.add(pos,_sheets.remove(getSheetIndex(sheetname))); workbook.setSheetOrder(sheetname, pos); } private void validateSheetIndex(int index) { int lastSheetIx = _sheets.size() - 1; if (index < 0 || index > lastSheetIx) { throw new IllegalArgumentException("Sheet index (" + index +") is out of range (0.." + lastSheetIx + ")"); } } /** * Selects a single sheet. This may be different to * the 'active' sheet (which is the sheet with focus). */ public void setSelectedTab(int index) { validateSheetIndex(index); int nSheets = _sheets.size(); for (int i=0; i