📄 hssfworkbook.java
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003, 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. *//* * HSSFWorkbook.java * * Created on September 30, 2001, 3:37 PM */package org.apache.poi.hssf.usermodel;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Stack;import org.apache.poi.hssf.eventmodel.EventRecordFactory;import org.apache.poi.hssf.model.Sheet;import org.apache.poi.hssf.model.Workbook;import org.apache.poi.hssf.record.BackupRecord;import org.apache.poi.hssf.record.ExtendedFormatRecord;import org.apache.poi.hssf.record.FontRecord;import org.apache.poi.hssf.record.NameRecord;import org.apache.poi.hssf.record.RecordFactory;import org.apache.poi.hssf.record.SSTRecord;import org.apache.poi.hssf.record.UnknownRecord;import org.apache.poi.hssf.record.WindowTwoRecord;import org.apache.poi.hssf.record.formula.Area3DPtg;import org.apache.poi.hssf.record.formula.MemFuncPtg;import org.apache.poi.hssf.record.formula.UnionPtg;import org.apache.poi.hssf.util.CellReference;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.DocumentInputStream;import org.apache.poi.poifs.filesystem.Entry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;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 java.lang.Object{ 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 */ private ArrayList sheets; /** * this holds the HSSFName objects attached to this workbook */ private ArrayList names; /** * holds whether or not to preserve other nodes in the POIFS. Used * for macros and embedded objects. */ private boolean preserveNodes; /** * if you do preserve the nodes, you'll need to hold the whole POIFS in * memory. */ private POIFSFileSystem poifs; /** * 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; private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class); /** * Creates new HSSFWorkbook from scratch (start here!) * */ public HSSFWorkbook() { workbook = Workbook.createWorkbook(); 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. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem * @exception IOException if the stream cannot be read */ public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes) throws IOException { this.preserveNodes = preserveNodes; if (preserveNodes) { this.poifs = fs; } sheets = new ArrayList(INITIAL_CAPACITY); names = new ArrayList(INITIAL_CAPACITY); InputStream stream = fs.createDocumentInputStream("Workbook"); EventRecordFactory factory = new EventRecordFactory(); List records = RecordFactory.createRecords(stream); workbook = Workbook.createWorkbook(records); setPropertiesFromWorkbook(workbook); int recOffset = workbook.getNumRecords(); int sheetNum = 0; while (recOffset < records.size()) { Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset ); recOffset = sheet.getEofLoc()+1; sheet.convertLabelRecords( workbook); // convert all LabelRecord records to LabelSSTRecord HSSFSheet hsheet = new HSSFSheet(workbook, sheet); sheets.add(hsheet); // workbook.setSheetName(sheets.size() -1, "Sheet"+sheets.size()); } for (int i = 0 ; i < workbook.getNumNames() ; ++i){ HSSFName name = new HSSFName(workbook, 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 } /** * 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 ) { workbook.setSheetOrder(sheetname, pos); } public final static byte ENCODING_COMPRESSED_UNICODE = 0; public final static byte ENCODING_UTF_16 = 1; /** * set the sheet name. * Will throw IllegalArgumentException if the name is greater than 31 chars * or contains /\?*[] * @param sheet number (0 based) * @param sheet name */ public void setSheetName(int sheet, String name) { workbook.setSheetName( sheet, name, ENCODING_COMPRESSED_UNICODE ); } public void setSheetName( int sheet, String name, short encoding ) { if (sheet > (sheets.size() - 1)) { throw new RuntimeException("Sheet out of bounds"); } switch ( encoding ) { case ENCODING_COMPRESSED_UNICODE: case ENCODING_UTF_16: break; default: // TODO java.io.UnsupportedEncodingException throw new RuntimeException( "Unsupported encoding" ); } workbook.setSheetName( sheet, name, encoding ); } /** * get the sheet name * @param sheet Number * @return Sheet name */ public String getSheetName(int sheet) { if (sheet > (sheets.size() - 1)) { throw new RuntimeException("Sheet out of bounds"); } return workbook.getSheetName(sheet); } /* * get the sheet's index * @param name sheet name * @return sheet index or -1 if it was not found. */ /** Returns the index of the sheet by his name * @param name the sheet name * @return index of the sheet (0 based) */ public int getSheetIndex(String name) { int retval = workbook.getSheetIndex(name); return retval; } /** * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns * the high level representation. Use this to create new sheets. * * @return HSSFSheet representing the new sheet. */ public HSSFSheet createSheet() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -