📄 sheet.java
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 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/>. */package org.apache.poi.hssf.model;import java.util.List;import java.util.ArrayList;import java.util.Iterator;import org.apache.poi.hssf .record.*; // normally I don't do this, buy we literally mean ALLimport org.apache.poi.hssf.record.formula.Ptg;import org.apache.poi.util.*;import org.apache.poi.hssf.record .aggregates.*; // normally I don't do this, buy we literally mean ALL/** * Low level model implementation of a Sheet (one workbook contains many sheets) * This file contains the low level binary records starting at the sheets BOF and * ending with the sheets EOF. Use HSSFSheet for a high level representation. * <P> * The structures of the highlevel API use references to this to perform most of their * operations. Its probably unwise to use these low level structures directly unless you * really know what you're doing. I recommend you read the Microsoft Excel 97 Developer's * Kit (Microsoft Press) and the documentation at http://sc.openoffice.org/excelfileformat.pdf * before even attempting to use this. * <P> * @author Andrew C. Oliver (acoliver at apache dot org) * @author Glen Stampoultzis (glens at apache.org) * @author Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, and PrintSetup * @author Jason Height (jheight at chariot dot net dot au) Clone support * @author Brian Sanders (kestrel at burdell dot org) Active Cell support * * @see org.apache.poi.hssf.model.Workbook * @see org.apache.poi.hssf.usermodel.HSSFSheet * @version 1.0-pre */public class Sheet implements Model{ public static final short LeftMargin = 0; public static final short RightMargin = 1; public static final short TopMargin = 2; public static final short BottomMargin = 3; protected ArrayList records = null; int preoffset = 0; // offset of the sheet in a new file int loc = 0; protected boolean containsLabels = false; protected int dimsloc = 0; protected DimensionsRecord dims; protected DefaultColWidthRecord defaultcolwidth = null; protected DefaultRowHeightRecord defaultrowheight = null; protected GridsetRecord gridset = null; protected PrintSetupRecord printSetup = null; protected HeaderRecord header = null; protected FooterRecord footer = null; protected PrintGridlinesRecord printGridlines = null; protected WindowTwoRecord windowTwo = null; protected MergeCellsRecord merged = null; protected Margin margins[] = null; protected List mergedRecords = new ArrayList(); protected int numMergedRegions = 0; protected SelectionRecord selection = null; private static POILogger log = POILogFactory.getLogger(Sheet.class); private ArrayList columnSizes = null; // holds column info protected ValueRecordsAggregate cells = null; protected RowRecordsAggregate rows = null; private Iterator valueRecIterator = null; private Iterator rowRecIterator = null; protected int eofLoc = 0; protected ProtectRecord protect = null; public static final byte PANE_LOWER_RIGHT = (byte)0; public static final byte PANE_UPPER_RIGHT = (byte)1; public static final byte PANE_LOWER_LEFT = (byte)2; public static final byte PANE_UPPER_LEFT = (byte)3; /** * Creates new Sheet with no intialization --useless at this point * @see #createSheet(List,int,int) */ public Sheet() { } /** * read support (offset used as starting point for search) for low level * API. Pass in an array of Record objects, the sheet number (0 based) and * a record offset (should be the location of the sheets BOF record). A Sheet * object is constructed and passed back with all of its initialization set * to the passed in records and references to those records held. This function * is normally called via Workbook. * * @param recs array containing those records in the sheet in sequence (normally obtained from RecordFactory) * @param sheetnum integer specifying the sheet's number (0,1 or 2 in this release) * @param offset of the sheet's BOF record * * @return Sheet object with all values set to those read from the file * * @see org.apache.poi.hssf.model.Workbook * @see org.apache.poi.hssf.record.Record */ public static Sheet createSheet(List recs, int sheetnum, int offset) { log.logFormatted(log.DEBUG, "Sheet createSheet (existing file) with %", new Integer(recs.size())); Sheet retval = new Sheet(); ArrayList records = new ArrayList(recs.size() / 5); boolean isfirstcell = true; boolean isfirstrow = true; int bofEofNestingLevel = 0; for (int k = offset; k < recs.size(); k++) { Record rec = ( Record ) recs.get(k); if (rec.getSid() == LabelRecord.sid) { log.log(log.DEBUG, "Hit label record."); retval.containsLabels = true; } else if (rec.getSid() == BOFRecord.sid) { bofEofNestingLevel++; log.log(log.DEBUG, "Hit BOF record. Nesting increased to " + bofEofNestingLevel); } else if (rec.getSid() == EOFRecord.sid) { --bofEofNestingLevel; log.log(log.DEBUG, "Hit EOF record. Nesting decreased to " + bofEofNestingLevel); if (bofEofNestingLevel == 0) { records.add(rec); retval.eofLoc = k; break; } } else if (rec.getSid() == DimensionsRecord.sid) { retval.dims = ( DimensionsRecord ) rec; retval.dimsloc = records.size(); } else if (rec.getSid() == MergeCellsRecord.sid) { retval.mergedRecords.add(rec); retval.merged = ( MergeCellsRecord ) rec; retval.numMergedRegions += retval.merged.getNumAreas(); } else if (rec.getSid() == ColumnInfoRecord.sid) { if (retval.columnSizes == null) { retval.columnSizes = new ArrayList(); } retval.columnSizes.add(rec); } else if (rec.getSid() == DefaultColWidthRecord.sid) { retval.defaultcolwidth = ( DefaultColWidthRecord ) rec; } else if (rec.getSid() == DefaultRowHeightRecord.sid) { retval.defaultrowheight = ( DefaultRowHeightRecord ) rec; } else if ( rec.isValue() && bofEofNestingLevel == 1 ) { if ( isfirstcell ) { retval.cells = new ValueRecordsAggregate(); rec = retval.cells; retval.cells.construct( k, recs ); isfirstcell = false; } else { rec = null; } } else if ( rec.getSid() == StringRecord.sid ) { rec = null; } else if ( rec.getSid() == RowRecord.sid ) { RowRecord row = (RowRecord)rec; if (!isfirstrow) rec = null; //only add the aggregate once if ( isfirstrow ) { retval.rows = new RowRecordsAggregate(); rec = retval.rows; isfirstrow = false; } retval.rows.insertRow(row); } else if ( rec.getSid() == PrintGridlinesRecord.sid ) { retval.printGridlines = (PrintGridlinesRecord) rec; } else if ( rec.getSid() == HeaderRecord.sid && bofEofNestingLevel == 1) { retval.header = (HeaderRecord) rec; } else if ( rec.getSid() == FooterRecord.sid && bofEofNestingLevel == 1) { retval.footer = (FooterRecord) rec; } else if ( rec.getSid() == PrintSetupRecord.sid && bofEofNestingLevel == 1) { retval.printSetup = (PrintSetupRecord) rec; } else if ( rec.getSid() == LeftMarginRecord.sid) { retval.getMargins()[LeftMargin] = (LeftMarginRecord) rec; } else if ( rec.getSid() == RightMarginRecord.sid) { retval.getMargins()[RightMargin] = (RightMarginRecord) rec; } else if ( rec.getSid() == TopMarginRecord.sid) { retval.getMargins()[TopMargin] = (TopMarginRecord) rec; } else if ( rec.getSid() == BottomMarginRecord.sid) { retval.getMargins()[BottomMargin] = (BottomMarginRecord) rec; } else if ( rec.getSid() == SelectionRecord.sid ) { retval.selection = (SelectionRecord) rec; } else if ( rec.getSid() == WindowTwoRecord.sid ) { retval.windowTwo = (WindowTwoRecord) rec; } else if ( rec.getSid() == ProtectRecord.sid ) { retval.protect = (ProtectRecord) rec; } if (rec != null) { records.add(rec); } } retval.records = records;// if (retval.rows == null)// {// retval.rows = new RowRecordsAggregate();// } retval.checkCells(); retval.checkRows();// if (retval.cells == null)// {// retval.cells = new ValueRecordsAggregate();// } log.log(log.DEBUG, "sheet createSheet (existing file) exited"); return retval; } /** * Clones the low level records of this sheet and returns the new sheet instance. * This method is implemented by adding methods for deep cloning to all records that * can be added to a sheet. The <b>Record</b> object does not implement cloneable. * When adding a new record, implement a public clone method if and only if the record * belongs to a sheet. */ public Sheet cloneSheet() { ArrayList clonedRecords = new ArrayList(this.records.size()); for (int i=0; i<this.records.size();i++) { Record rec = (Record)((Record)this.records.get(i)).clone(); //Need to pull out the Row record and the Value records from their //Aggregates. //This is probably the best way to do it since we probably dont want the createSheet //To cater for these artificial Record types if (rec instanceof RowRecordsAggregate) { RowRecordsAggregate rrAgg = (RowRecordsAggregate)rec; for (Iterator rowIter = rrAgg.getIterator();rowIter.hasNext();) { Record rowRec = (Record)rowIter.next(); clonedRecords.add(rowRec); } } else if (rec instanceof ValueRecordsAggregate) { ValueRecordsAggregate vrAgg = (ValueRecordsAggregate)rec; for (Iterator cellIter = vrAgg.getIterator();cellIter.hasNext();) { Record valRec = (Record)cellIter.next(); clonedRecords.add(valRec); } } else if (rec instanceof FormulaRecordAggregate) { FormulaRecordAggregate fmAgg = (FormulaRecordAggregate)rec; Record fmAggRec = fmAgg.getFormulaRecord(); if (fmAggRec != null) clonedRecords.add(fmAggRec); fmAggRec = fmAgg.getStringRecord(); if (fmAggRec != null) clonedRecords.add(fmAggRec); } else { clonedRecords.add(rec); } } return createSheet(clonedRecords, 0, 0); } /** * read support (offset = 0) Same as createSheet(Record[] recs, int, int) * only the record offset is assumed to be 0. * * @param records array containing those records in the sheet in sequence (normally obtained from RecordFactory) * @param sheetnum integer specifying the sheet's number (0,1 or 2 in this release) * @return Sheet object */ public static Sheet createSheet(List records, int sheetnum) { log.log(log.DEBUG, "Sheet createSheet (exisiting file) assumed offset 0"); return createSheet(records, sheetnum, 0); } /** * Creates a sheet with all the usual records minus values and the "index" * record (not required). Sets the location pointer to where the first value * records should go. Use this to create a sheet from "scratch". * * @return Sheet object with all values set to defaults */ public static Sheet createSheet() { log.log(log.DEBUG, "Sheet createsheet from scratch called"); Sheet retval = new Sheet(); ArrayList records = new ArrayList(30); records.add(retval.createBOF());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -