⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sheet.java

📁 java 报表 to office文档: 本包由java语言开发
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* ====================================================================   Copyright 2002-2004   Apache Software Foundation   Licensed 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.model;import org.apache.poi.hssf.record.*;import org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate;import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate;import org.apache.poi.hssf.record.formula.Ptg;import org.apache.poi.util.POILogFactory;import org.apache.poi.util.POILogger;import java.util.ArrayList;import java.util.Iterator;import java.util.List;   // 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. DBCell & Index Record writing 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;    private static POILogger            log              = POILogFactory.getLogger(Sheet.class);    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;    protected ColumnInfoRecordsAggregate columns           =     null;    protected ValueRecordsAggregate      cells             =     null;    protected RowRecordsAggregate        rows              =     null;    private   Iterator                   valueRecIterator  =     null;    private   Iterator                   rowRecIterator    =     null;    protected int                        eofLoc            =     0;    protected ProtectRecord              protect           =     null;    protected PageBreakRecord            rowBreaks         =     null;    protected PageBreakRecord            colBreaks         =     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)    {        if (log.check( POILogger.DEBUG ))            log.logFormatted(POILogger.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)            {                if (log.check( POILogger.DEBUG ))                    log.log(POILogger.DEBUG, "Hit label record.");                retval.containsLabels = true;            }            else if (rec.getSid() == BOFRecord.sid)            {                bofEofNestingLevel++;                if (log.check( POILogger.DEBUG ))                    log.log(POILogger.DEBUG, "Hit BOF record. Nesting increased to " + bofEofNestingLevel);            }            else if (rec.getSid() == EOFRecord.sid)            {                --bofEofNestingLevel;                if (log.check( POILogger.DEBUG ))                    log.log(POILogger.DEBUG, "Hit EOF record. Nesting decreased to " + bofEofNestingLevel);                if (bofEofNestingLevel == 0) {                    records.add(rec);                    retval.eofLoc = k;                    break;                }            }            else if (rec.getSid() == DimensionsRecord.sid)            {                // Make a columns aggregate if one hasn't ready been created.                if (retval.columns == null)                {                    retval.columns = new ColumnInfoRecordsAggregate();                    records.add(retval.columns);                }                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)            {                ColumnInfoRecord col = (ColumnInfoRecord)rec;                if (retval.columns != null)                {                    rec = null; //only add the aggregate once                }                else                {                    rec = retval.columns = new ColumnInfoRecordsAggregate();                }                retval.columns.insertColumn(col);            }            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() == DBCellRecord.sid )            {                rec = null;            }            else if ( rec.getSid() == IndexRecord.sid )            {                rec = null;            }            			else if ( rec.getSid() == ProtectRecord.sid )			{				retval.protect = (ProtectRecord) rec;			} 			else if (rec.getSid() == PageBreakRecord.HORIZONTAL_SID) 			{					retval.rowBreaks = (PageBreakRecord)rec;							}			else if (rec.getSid() == PageBreakRecord.VERTICAL_SID) 			{					retval.colBreaks = (PageBreakRecord)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();//        }        if (log.check( POILogger.DEBUG ))            log.log(POILogger.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();                        if (valRec instanceof FormulaRecordAggregate) {                FormulaRecordAggregate fmAgg = (FormulaRecordAggregate)valRec;                Record fmAggRec = fmAgg.getFormulaRecord();                if (fmAggRec != null)                  clonedRecords.add(fmAggRec);                fmAggRec =   fmAgg.getStringRecord();                if (fmAggRec != null)                  clonedRecords.add(fmAggRec);              } else {                clonedRecords.add(valRec);              }          }        } else if (rec instanceof FormulaRecordAggregate) {  //Is this required now??          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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -