2002-01-30 21:22:28 -05:00
|
|
|
/* ====================================================================
|
|
|
|
* The Apache Software License, Version 1.1
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 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;
|
|
|
|
|
2002-03-14 06:05:10 -05:00
|
|
|
import org.apache.poi.util.POILogFactory;
|
2002-03-10 07:00:33 -05:00
|
|
|
import org.apache.poi.hssf.model.Sheet;
|
|
|
|
import org.apache.poi.hssf.model.Workbook;
|
|
|
|
import org.apache.poi.hssf.record.*;
|
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
2002-09-16 14:14:31 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.Entry;
|
|
|
|
import org.apache.poi.poifs.filesystem.DirectoryEntry;
|
|
|
|
import org.apache.poi.poifs.filesystem.DocumentEntry;
|
|
|
|
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
2002-03-10 07:00:33 -05:00
|
|
|
import org.apache.poi.util.POILogger;
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
2002-03-10 07:00:33 -05:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2002-01-30 21:22:28 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2002-09-16 14:14:31 -04:00
|
|
|
import java.util.Iterator;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
2002-02-10 23:23:11 -05:00
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
2002-08-15 10:13:34 -04:00
|
|
|
* @author Shawn Laubach (shawnlaubach at cox.net)
|
2002-01-30 21:22:28 -05:00
|
|
|
* @version 2.0-pre
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class HSSFWorkbook
|
2002-03-10 07:00:33 -05:00
|
|
|
extends java.lang.Object
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
private static final int DEBUG = POILogger.DEBUG;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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!
|
|
|
|
*/
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
public final static int INITIAL_CAPACITY = 3;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this is the reference to the low level Workbook object
|
|
|
|
*/
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
private Workbook workbook;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this holds the HSSFSheet objects attached to this workbook
|
|
|
|
*/
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
private ArrayList sheets;
|
2002-04-23 18:24:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* this holds the HSSFName objects attached to this workbook
|
|
|
|
*/
|
|
|
|
|
|
|
|
private ArrayList names;
|
2002-09-16 14:14:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2002-04-23 18:24:41 -04:00
|
|
|
|
2002-03-14 06:05:10 -05:00
|
|
|
private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new HSSFWorkbook from scratch (start here!)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFWorkbook()
|
|
|
|
{
|
|
|
|
workbook = Workbook.createWorkbook();
|
2002-03-10 07:00:33 -05:00
|
|
|
sheets = new ArrayList(INITIAL_CAPACITY);
|
2002-04-23 18:24:41 -04:00
|
|
|
names = new ArrayList(INITIAL_CAPACITY);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2002-09-16 14:14:31 -04:00
|
|
|
public HSSFWorkbook(POIFSFileSystem fs) throws IOException {
|
|
|
|
this(fs,true);
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
2002-09-16 14:14:31 -04:00
|
|
|
* @param preserveNodes whether to preseve other nodes, such as
|
|
|
|
* macros. This takes more memory, so only say yes if you
|
|
|
|
* need to.
|
2002-01-30 21:22:28 -05:00
|
|
|
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem
|
|
|
|
* @exception IOException if the stream cannot be read
|
|
|
|
*/
|
|
|
|
|
2002-09-16 14:14:31 -04:00
|
|
|
public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes)
|
2002-03-10 07:00:33 -05:00
|
|
|
throws IOException
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-09-16 14:14:31 -04:00
|
|
|
this.preserveNodes = preserveNodes;
|
|
|
|
|
|
|
|
if (preserveNodes) {
|
|
|
|
this.poifs = fs;
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
sheets = new ArrayList(INITIAL_CAPACITY);
|
2002-04-23 18:24:41 -04:00
|
|
|
names = new ArrayList(INITIAL_CAPACITY);
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
InputStream stream = fs.createDocumentInputStream("Workbook");
|
|
|
|
List records = RecordFactory.createRecords(stream);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
workbook = Workbook.createWorkbook(records);
|
|
|
|
setPropertiesFromWorkbook(workbook);
|
2002-08-21 07:56:49 -04:00
|
|
|
int recOffset = workbook.getNumRecords();
|
2002-04-12 04:26:29 -04:00
|
|
|
int sheetNum = 0;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2002-08-21 07:56:49 -04:00
|
|
|
while (recOffset < records.size())
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-08-21 07:56:49 -04:00
|
|
|
Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset );
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2002-08-21 07:56:49 -04:00
|
|
|
recOffset = sheet.getEofLoc()+1;
|
2002-01-30 21:22:28 -05:00
|
|
|
sheet.convertLabelRecords(
|
2002-03-10 07:00:33 -05:00
|
|
|
workbook); // convert all LabelRecord records to LabelSSTRecord
|
2002-01-30 21:22:28 -05:00
|
|
|
HSSFSheet hsheet = new HSSFSheet(workbook, sheet);
|
|
|
|
|
|
|
|
sheets.add(hsheet);
|
|
|
|
|
|
|
|
// workbook.setSheetName(sheets.size() -1, "Sheet"+sheets.size());
|
|
|
|
}
|
2002-04-23 18:24:41 -04:00
|
|
|
|
|
|
|
for (int i = 0 ; i < workbook.getNumNames() ; ++i){
|
|
|
|
HSSFName name = new HSSFName(workbook, workbook.getNameRecord(i));
|
|
|
|
names.add(name);
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2002-09-16 14:14:31 -04:00
|
|
|
public HSSFWorkbook(InputStream s) throws IOException {
|
|
|
|
this(s,true);
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* Companion to HSSFWorkbook(POIFSFileSystem), this constructs the POI filesystem around your
|
|
|
|
* inputstream.
|
|
|
|
*
|
2002-03-10 07:00:33 -05:00
|
|
|
* @param s the POI filesystem that contains the Workbook stream.
|
2002-09-16 14:14:31 -04:00
|
|
|
* @param preserveNodes whether to preseve other nodes, such as
|
|
|
|
* macros. This takes more memory, so only say yes if you
|
|
|
|
* need to.
|
2002-01-30 21:22:28 -05:00
|
|
|
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem
|
|
|
|
* @see #HSSFWorkbook(POIFSFileSystem)
|
|
|
|
* @exception IOException if the stream cannot be read
|
|
|
|
*/
|
|
|
|
|
2002-09-16 14:14:31 -04:00
|
|
|
public HSSFWorkbook(InputStream s, boolean preserveNodes)
|
2002-03-10 07:00:33 -05:00
|
|
|
throws IOException
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-09-16 14:14:31 -04:00
|
|
|
this(new POIFSFileSystem(s), preserveNodes);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* used internally to set the workbook properties.
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void setPropertiesFromWorkbook(Workbook book)
|
|
|
|
{
|
|
|
|
this.workbook = book;
|
|
|
|
|
|
|
|
// none currently
|
|
|
|
}
|
2002-07-28 18:54:43 -04:00
|
|
|
|
2002-09-01 22:11:16 -04:00
|
|
|
public final static byte ENCODING_COMPRESSED_UNICODE = 0;
|
|
|
|
public final static byte ENCODING_UTF_16 = 1;
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* set the sheet name.
|
|
|
|
* @param sheet number (0 based)
|
|
|
|
* @param sheet name
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void setSheetName(int sheet, String name)
|
2002-09-01 22:11:16 -04:00
|
|
|
{
|
|
|
|
workbook.setSheetName( sheet, name, ENCODING_COMPRESSED_UNICODE );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSheetName( int sheet, String name, short encoding )
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
if (sheet > (sheets.size() - 1))
|
|
|
|
{
|
|
|
|
throw new RuntimeException("Sheet out of bounds");
|
|
|
|
}
|
2002-09-01 22:11:16 -04:00
|
|
|
|
|
|
|
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 );
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2002-07-28 18:54:43 -04:00
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2002-04-23 18:24:41 -04:00
|
|
|
/*
|
2002-01-30 21:22:28 -05:00
|
|
|
* get the sheet's index
|
2002-03-10 07:00:33 -05:00
|
|
|
* @param name sheet name
|
2002-01-30 21:22:28 -05:00
|
|
|
* @return sheet index or -1 if it was not found.
|
|
|
|
*/
|
|
|
|
|
2002-04-23 18:24:41 -04:00
|
|
|
/** Returns the index of the sheet by his name
|
|
|
|
* @param name the sheet name
|
|
|
|
* @return index of the sheet (0 based)
|
|
|
|
*/
|
2002-01-30 21:22:28 -05:00
|
|
|
public int getSheetIndex(String name)
|
|
|
|
{
|
2002-04-23 18:24:41 -04:00
|
|
|
int retval = workbook.getSheetIndex(name);
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
return retval;
|
|
|
|
}
|
2002-04-23 18:24:41 -04:00
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
// if (getNumberOfSheets() == 3)
|
2002-01-30 21:22:28 -05:00
|
|
|
// throw new RuntimeException("You cannot have more than three sheets in HSSF 1.0");
|
|
|
|
HSSFSheet sheet = new HSSFSheet(workbook);
|
|
|
|
|
|
|
|
sheets.add(sheet);
|
|
|
|
workbook.setSheetName(sheets.size() - 1,
|
2002-03-10 07:00:33 -05:00
|
|
|
"Sheet" + (sheets.size() - 1));
|
|
|
|
WindowTwoRecord windowTwo = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
|
|
|
|
windowTwo.setSelected(sheets.size() == 1);
|
|
|
|
windowTwo.setPaged(sheets.size() == 1);
|
2002-01-30 21:22:28 -05:00
|
|
|
return sheet;
|
|
|
|
}
|
|
|
|
|
2002-09-04 20:26:28 -04:00
|
|
|
/**
|
|
|
|
* create an HSSFSheet from an existing sheet in the HSSFWorkbook.
|
|
|
|
*
|
|
|
|
* @return HSSFSheet representing the cloned sheet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFSheet cloneSheet(int sheetNum) {
|
|
|
|
HSSFSheet srcSheet = (HSSFSheet)sheets.get(sheetNum);
|
|
|
|
String srcName = workbook.getSheetName(sheetNum);
|
|
|
|
if (srcSheet != null) {
|
|
|
|
HSSFSheet clonedSheet = srcSheet.cloneSheet(workbook);
|
|
|
|
WindowTwoRecord windowTwo = (WindowTwoRecord) clonedSheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
|
|
|
|
windowTwo.setSelected(sheets.size() == 1);
|
|
|
|
windowTwo.setPaged(sheets.size() == 1);
|
|
|
|
|
|
|
|
sheets.add(clonedSheet);
|
|
|
|
workbook.setSheetName(sheets.size()-1, srcName+"[1]");
|
|
|
|
return clonedSheet;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
* create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
|
|
|
|
* the high level representation. Use this to create new sheets.
|
|
|
|
*
|
2002-03-10 07:00:33 -05:00
|
|
|
* @param sheetname sheetname to set for the sheet.
|
2002-01-30 21:22:28 -05:00
|
|
|
* @return HSSFSheet representing the new sheet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFSheet createSheet(String sheetname)
|
|
|
|
{
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
// if (getNumberOfSheets() == 3)
|
2002-01-30 21:22:28 -05:00
|
|
|
// throw new RuntimeException("You cannot have more than three sheets in HSSF 1.0");
|
|
|
|
HSSFSheet sheet = new HSSFSheet(workbook);
|
|
|
|
|
|
|
|
sheets.add(sheet);
|
|
|
|
workbook.setSheetName(sheets.size() - 1, sheetname);
|
2002-03-10 07:00:33 -05:00
|
|
|
WindowTwoRecord windowTwo = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
|
|
|
|
windowTwo.setSelected(sheets.size() == 1);
|
|
|
|
windowTwo.setPaged(sheets.size() == 1);
|
2002-01-30 21:22:28 -05:00
|
|
|
return sheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the number of spreadsheets in the workbook (this will be three after serialization)
|
|
|
|
* @return number of sheets
|
|
|
|
*/
|
|
|
|
|
|
|
|
public int getNumberOfSheets()
|
|
|
|
{
|
|
|
|
return sheets.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the HSSFSheet object at the given index.
|
|
|
|
* @param index of the sheet number (0-based physical & logical)
|
|
|
|
* @return HSSFSheet at the provided index
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFSheet getSheetAt(int index)
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
return (HSSFSheet) sheets.get(index);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sheet with the given name
|
|
|
|
* @param name of the sheet
|
|
|
|
* @return HSSFSheet with the name provided or null if it does not exist
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFSheet getSheet(String name)
|
|
|
|
{
|
|
|
|
HSSFSheet retval = null;
|
|
|
|
|
|
|
|
for (int k = 0; k < sheets.size(); k++)
|
|
|
|
{
|
|
|
|
String sheetname = workbook.getSheetName(k);
|
|
|
|
|
|
|
|
if (sheetname.equals(name))
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
retval = (HSSFSheet) sheets.get(k);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes sheet at the given index
|
|
|
|
* @param index of the sheet (0-based)
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void removeSheetAt(int index)
|
|
|
|
{
|
|
|
|
sheets.remove(index);
|
|
|
|
workbook.removeSheet(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determine whether the Excel GUI will backup the workbook when saving.
|
|
|
|
*
|
|
|
|
* @param backupValue true to indicate a backup will be performed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void setBackupFlag(boolean backupValue)
|
|
|
|
{
|
|
|
|
BackupRecord backupRecord = workbook.getBackupRecord();
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
backupRecord.setBackup(backupValue ? (short) 1
|
|
|
|
: (short) 0);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determine whether the Excel GUI will backup the workbook when saving.
|
|
|
|
*
|
|
|
|
* @return the current setting for backups.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public boolean getBackupFlag()
|
|
|
|
{
|
|
|
|
BackupRecord backupRecord = workbook.getBackupRecord();
|
|
|
|
|
|
|
|
return (backupRecord.getBackup() == 0) ? false
|
2002-03-10 07:00:33 -05:00
|
|
|
: true;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new Font and add it to the workbook's font table
|
|
|
|
* @return new font object
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFFont createFont()
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
FontRecord font = workbook.createNewFont();
|
|
|
|
short fontindex = (short) (getNumberOfFonts() - 1);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
if (fontindex > 3)
|
|
|
|
{
|
|
|
|
fontindex++; // THERE IS NO FOUR!!
|
|
|
|
}
|
|
|
|
HSSFFont retval = new HSSFFont(fontindex, font);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the number of fonts in the font table
|
|
|
|
* @return number of fonts
|
|
|
|
*/
|
|
|
|
|
|
|
|
public short getNumberOfFonts()
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
return (short) workbook.getNumberOfFontRecords();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the font at the given index number
|
2002-03-10 07:00:33 -05:00
|
|
|
* @param idx index number
|
2002-01-30 21:22:28 -05:00
|
|
|
* @return HSSFFont at the index
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFFont getFontAt(short idx)
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
FontRecord font = workbook.getFontRecordAt(idx);
|
|
|
|
HSSFFont retval = new HSSFFont(idx, font);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new Cell style and add it to the workbook's style table
|
|
|
|
* @return the new Cell Style object
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFCellStyle createCellStyle()
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
ExtendedFormatRecord xfr = workbook.createCellXF();
|
|
|
|
short index = (short) (getNumCellStyles() - 1);
|
|
|
|
HSSFCellStyle style = new HSSFCellStyle(index, xfr);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the number of styles the workbook contains
|
|
|
|
* @return count of cell styles
|
|
|
|
*/
|
|
|
|
|
|
|
|
public short getNumCellStyles()
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
return (short) workbook.getNumExFormats();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the cell style object at the given index
|
2002-03-10 07:00:33 -05:00
|
|
|
* @param idx index within the set of styles
|
2002-01-30 21:22:28 -05:00
|
|
|
* @return HSSFCellStyle object at the index
|
|
|
|
*/
|
|
|
|
|
|
|
|
public HSSFCellStyle getCellStyleAt(short idx)
|
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
ExtendedFormatRecord xfr = workbook.getExFormatAt(idx);
|
|
|
|
HSSFCellStyle style = new HSSFCellStyle(idx, xfr);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method write - write out this workbook to an Outputstream. Constructs
|
|
|
|
* a new POI POIFSFileSystem, passes in the workbook binary representation and
|
|
|
|
* writes it out.
|
|
|
|
*
|
|
|
|
* @param stream - the java OutputStream you wish to write the XLS to
|
|
|
|
*
|
|
|
|
* @exception IOException if anything can't be written.
|
|
|
|
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void write(OutputStream stream)
|
2002-03-10 07:00:33 -05:00
|
|
|
throws IOException
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-03-10 07:00:33 -05:00
|
|
|
byte[] bytes = getBytes();
|
|
|
|
POIFSFileSystem fs = new POIFSFileSystem();
|
2002-09-16 14:14:31 -04:00
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
fs.createDocument(new ByteArrayInputStream(bytes), "Workbook");
|
2002-09-16 14:14:31 -04:00
|
|
|
|
|
|
|
if (preserveNodes) {
|
|
|
|
List excepts = new ArrayList(1);
|
|
|
|
excepts.add("Workbook");
|
|
|
|
copyNodes(this.poifs,fs,excepts);
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
fs.writeFilesystem(stream);
|
2002-09-16 14:14:31 -04:00
|
|
|
//poifs.writeFilesystem(stream);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method getBytes - get the bytes of just the HSSF portions of the XLS file.
|
|
|
|
* Use this to construct a POI POIFSFileSystem yourself.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return byte[] array containing the binary representation of this workbook and all contained
|
|
|
|
* sheets, rows, cells, etc.
|
|
|
|
*
|
|
|
|
* @see org.apache.poi.hssf.model.Workbook
|
|
|
|
* @see org.apache.poi.hssf.model.Sheet
|
|
|
|
*/
|
|
|
|
|
2002-03-10 07:00:33 -05:00
|
|
|
public byte[] getBytes()
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
log.log(DEBUG, "HSSFWorkbook.getBytes()");
|
2002-03-10 07:00:33 -05:00
|
|
|
int wbsize = workbook.getSize();
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
// log.debug("REMOVEME: old sizing method "+workbook.serialize().length);
|
|
|
|
// ArrayList sheetbytes = new ArrayList(sheets.size());
|
|
|
|
int totalsize = wbsize;
|
|
|
|
|
|
|
|
for (int k = 0; k < sheets.size(); k++)
|
|
|
|
{
|
|
|
|
workbook.setSheetBof(k, totalsize);
|
|
|
|
|
|
|
|
// sheetbytes.add((( HSSFSheet ) sheets.get(k)).getSheet().getSize());
|
2002-03-10 07:00:33 -05:00
|
|
|
totalsize += ((HSSFSheet) sheets.get(k)).getSheet().getSize();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2002-09-16 14:14:31 -04:00
|
|
|
/* if (totalsize < 4096)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
totalsize = 4096;
|
2002-09-16 14:14:31 -04:00
|
|
|
}*/
|
|
|
|
byte[] retval = new byte[totalsize];
|
|
|
|
int pos = workbook.serialize(0, retval);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
// System.arraycopy(wb, 0, retval, 0, wb.length);
|
|
|
|
for (int k = 0; k < sheets.size(); k++)
|
|
|
|
{
|
|
|
|
|
|
|
|
// byte[] sb = (byte[])sheetbytes.get(k);
|
|
|
|
// System.arraycopy(sb, 0, retval, pos, sb.length);
|
2002-03-10 07:00:33 -05:00
|
|
|
pos += ((HSSFSheet) sheets.get(k)).getSheet().serialize(pos,
|
2002-09-16 14:14:31 -04:00
|
|
|
retval); // sb.length;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2002-09-16 14:14:31 -04:00
|
|
|
/* for (int k = pos; k < totalsize; k++)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
2002-09-16 14:14:31 -04:00
|
|
|
retval[k] = 0;
|
|
|
|
}*/
|
|
|
|
return retval;
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public int addSSTString(String string)
|
|
|
|
{
|
|
|
|
return workbook.addSSTString(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSSTString(int index)
|
|
|
|
{
|
|
|
|
return workbook.getSSTString(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
Workbook getWorkbook()
|
|
|
|
{
|
|
|
|
return workbook;
|
|
|
|
}
|
2002-04-23 18:24:41 -04:00
|
|
|
|
|
|
|
/** gets the total number of named ranges in the workboko
|
|
|
|
* @return number of named ranges
|
|
|
|
*/
|
|
|
|
public int getNumberOfNames(){
|
|
|
|
int result = names.size();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** gets the Named range
|
|
|
|
* @param index position of the named range
|
|
|
|
* @return named range high level
|
|
|
|
*/
|
|
|
|
public HSSFName getNameAt(int index){
|
|
|
|
HSSFName result = (HSSFName) names.get(index);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** gets the named range name
|
|
|
|
* @param index the named range index (0 based)
|
|
|
|
* @return named range name
|
|
|
|
*/
|
|
|
|
public String getNameName(int index){
|
|
|
|
String result = getNameAt(index).getNameName();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** creates a new named range and add it to the model
|
|
|
|
* @return named range high level
|
|
|
|
*/
|
|
|
|
public HSSFName createName(){
|
|
|
|
NameRecord nameRecord = workbook.createName();
|
|
|
|
|
|
|
|
HSSFName newName = new HSSFName(workbook, nameRecord);
|
|
|
|
|
|
|
|
names.add(newName);
|
|
|
|
|
|
|
|
return newName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** gets the named range index by his name
|
|
|
|
* @param name named range name
|
|
|
|
* @return named range index
|
|
|
|
*/
|
|
|
|
public int getNameIndex(String name)
|
|
|
|
{
|
|
|
|
int retval = -1;
|
|
|
|
|
|
|
|
for (int k = 0; k < names.size(); k++)
|
|
|
|
{
|
|
|
|
String nameName = getNameName(k);
|
|
|
|
|
|
|
|
if (nameName.equals(name))
|
|
|
|
{
|
|
|
|
retval = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** remove the named range by his index
|
|
|
|
* @param index named range index (0 based)
|
|
|
|
*/
|
|
|
|
public void removeName(int index){
|
|
|
|
names.remove(index);
|
|
|
|
workbook.removeName(index);
|
|
|
|
}
|
2002-08-15 10:13:34 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of HSSFDataFormat.
|
|
|
|
* @return the HSSFDataFormat object
|
|
|
|
* @see org.apache.poi.hssf.record.FormatRecord
|
|
|
|
* @see org.apache.poi.hssf.record.Record
|
|
|
|
*/
|
|
|
|
public HSSFDataFormat createDataFormat() {
|
|
|
|
return new HSSFDataFormat(workbook);
|
|
|
|
}
|
|
|
|
|
2002-04-23 18:24:41 -04:00
|
|
|
/** remove the named range by his name
|
|
|
|
* @param name named range name
|
|
|
|
*/
|
|
|
|
public void removeName(String name){
|
|
|
|
int index = getNameIndex(name);
|
|
|
|
|
|
|
|
removeName(index);
|
|
|
|
|
|
|
|
}
|
2002-09-16 14:14:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies nodes from one POIFS to the other minus the excepts
|
|
|
|
* @param source is the source POIFS to copy from
|
|
|
|
* @param target is the target POIFS to copy to
|
|
|
|
* @param excepts is a list of Strings specifying what nodes NOT to copy
|
|
|
|
*/
|
|
|
|
private void copyNodes(POIFSFileSystem source, POIFSFileSystem target,
|
|
|
|
List excepts) throws IOException {
|
|
|
|
//System.err.println("CopyNodes called");
|
|
|
|
|
|
|
|
DirectoryEntry root = source.getRoot();
|
|
|
|
DirectoryEntry newRoot = target.getRoot();
|
|
|
|
|
|
|
|
Iterator entries = root.getEntries();
|
|
|
|
|
|
|
|
while (entries.hasNext()) {
|
|
|
|
Entry entry = (Entry)entries.next();
|
|
|
|
if (!isInList(entry.getName(), excepts)) {
|
|
|
|
copyNodeRecursively(entry,newRoot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isInList(String entry, List list) {
|
|
|
|
for (int k = 0; k < list.size(); k++) {
|
|
|
|
if (((String)list.get(k)).equals(entry)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void copyNodeRecursively(Entry entry, DirectoryEntry target)
|
|
|
|
throws IOException {
|
|
|
|
//System.err.println("copyNodeRecursively called with "+entry.getName()+
|
|
|
|
// ","+target.getName());
|
|
|
|
DirectoryEntry newTarget = null;
|
|
|
|
if (entry.isDirectoryEntry()) {
|
|
|
|
newTarget = target.createDirectory(entry.getName());
|
|
|
|
Iterator entries = ((DirectoryEntry)entry).getEntries();
|
|
|
|
|
|
|
|
while (entries.hasNext()) {
|
|
|
|
copyNodeRecursively((Entry)entries.next(),newTarget);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DocumentEntry dentry = (DocumentEntry)entry;
|
|
|
|
DocumentInputStream dstream = new DocumentInputStream(dentry);
|
|
|
|
target.createDocument(dentry.getName(),dstream);
|
|
|
|
dstream.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|