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

📄 hssfsheet.java

📁 java 报表 to office文档: 本包由java语言开发
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* ====================================================================   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.==================================================================== *//* * HSSFSheet.java * * Created on September 30, 2001, 3:40 PM */package org.apache.poi.hssf.usermodel;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.*;import org.apache.poi.hssf.util.Region;import org.apache.poi.util.POILogFactory;import org.apache.poi.util.POILogger;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.TreeMap;/** * High level representation of a worksheet. * @author  Andrew C. Oliver (acoliver at apache dot org) * @author  Glen Stampoultzis (glens at apache.org) * @author  Libin Roman (romal at vistaportal.com) * @author  Shawn Laubach (slaubach at apache dot org) (Just a little) */public class HSSFSheet{    private static final int DEBUG = POILogger.DEBUG;    /* Constants for margins */    public static final short LeftMargin = Sheet.LeftMargin;    public static final short RightMargin = Sheet.RightMargin;    public static final short TopMargin = Sheet.TopMargin;    public static final short BottomMargin = Sheet.BottomMargin;    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;    /**     * Used for compile-time optimization.  This is the initial size for the collection of     * rows.  It is currently set to 20.  If you generate larger sheets you may benefit     * by setting this to a higher number and recompiling a custom edition of HSSFSheet.     */    public final static int INITIAL_CAPACITY = 20;    /**     * reference to the low level Sheet object     */    private Sheet sheet;    private TreeMap rows;    private Workbook book;    private int firstrow;    private int lastrow;    private static POILogger log = POILogFactory.getLogger(HSSFSheet.class);    /**     * Creates new HSSFSheet   - called by HSSFWorkbook to create a sheet from     * scratch.  You should not be calling this from application code (its protected anyhow).     *     * @param book - lowlevel Workbook object associated with the sheet.     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()     */    protected HSSFSheet(Workbook book)    {        sheet = Sheet.createSheet();        rows = new TreeMap();   // new ArrayList(INITIAL_CAPACITY);        this.book = book;    }    /**     * Creates an HSSFSheet representing the given Sheet object.  Should only be     * called by HSSFWorkbook when reading in an exisiting file.     *     * @param book - lowlevel Workbook object associated with the sheet.     * @param sheet - lowlevel Sheet object this sheet will represent     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()     */    protected HSSFSheet(Workbook book, Sheet sheet)    {        this.sheet = sheet;        rows = new TreeMap();        this.book = book;        setPropertiesFromSheet(sheet);    }    HSSFSheet cloneSheet(Workbook book) {      return new HSSFSheet(book, sheet.cloneSheet());    }    /**     * used internally to set the properties given a Sheet object     */    private void setPropertiesFromSheet(Sheet sheet)    {        int sloc = sheet.getLoc();        RowRecord row = sheet.getNextRow();        while (row != null)        {            createRowFromRecord(row);            row = sheet.getNextRow();        }        sheet.setLoc(sloc);        CellValueRecordInterface cval = sheet.getNextValueRecord();        long timestart = System.currentTimeMillis();        if (log.check( POILogger.DEBUG ))            log.log(DEBUG, "Time at start of cell creating in HSSF sheet = ",                new Long(timestart));        HSSFRow lastrow = null;        while (cval != null)        {            long cellstart = System.currentTimeMillis();            HSSFRow hrow = lastrow;            if ( ( lastrow == null ) || ( lastrow.getRowNum() != cval.getRow() ) )            {                hrow = getRow( cval.getRow() );            }            if ( hrow != null )            {                lastrow = hrow;                if (log.check( POILogger.DEBUG ))                    log.log( DEBUG, "record id = " + Integer.toHexString( ( (Record) cval ).getSid() ) );                hrow.createCellFromRecord( cval );                cval = sheet.getNextValueRecord();                if (log.check( POILogger.DEBUG ))                    log.log( DEBUG, "record took ",                        new Long( System.currentTimeMillis() - cellstart ) );            }            else            {                cval = null;            }        }        if (log.check( POILogger.DEBUG ))            log.log(DEBUG, "total sheet cell creation took ",                new Long(System.currentTimeMillis() - timestart));    }    /**     * Create a new row within the sheet and return the high level representation     *     * @param rownum  row number     * @return High level HSSFRow object representing a row in the sheet     * @see org.apache.poi.hssf.usermodel.HSSFRow     * @see #removeRow(HSSFRow)     */    public HSSFRow createRow(int rownum)    {        HSSFRow row = new HSSFRow(book, sheet, rownum);        addRow(row, true);        return row;    }    /**     * Used internally to create a high level Row object from a low level row object.     * USed when reading an existing file     * @param row  low level record to represent as a high level Row and add to sheet     * @return HSSFRow high level representation     */    private HSSFRow createRowFromRecord(RowRecord row)    {        HSSFRow hrow = new HSSFRow(book, sheet, row);        addRow(hrow, false);        return hrow;    }    /**     * Remove a row from this sheet.  All cells contained in the row are removed as well     *     * @param row   representing a row to remove.     */    public void removeRow(HSSFRow row)    {        sheet.setLoc(sheet.getDimsLoc());        if (rows.size() > 0)        {            rows.remove(row);            if (row.getRowNum() == getLastRowNum())            {                lastrow = findLastRow(lastrow);            }            if (row.getRowNum() == getFirstRowNum())            {                firstrow = findFirstRow(firstrow);            }            Iterator iter = row.cellIterator();            while (iter.hasNext())            {                HSSFCell cell = (HSSFCell) iter.next();                sheet.removeValueRecord(row.getRowNum(),                        cell.getCellValueRecord());            }            sheet.removeRow(row.getRowRecord());        }    }    /**     * used internally to refresh the "last row" when the last row is removed.     */    private int findLastRow(int lastrow)    {        int rownum = lastrow - 1;        HSSFRow r = getRow(rownum);        while (r == null && rownum > 0)        {            r = getRow(--rownum);        }        if (r == null)          return -1;        return rownum;    }    /**     * used internally to refresh the "first row" when the first row is removed.     */    private int findFirstRow(int firstrow)    {        int rownum = firstrow + 1;        HSSFRow r = getRow(rownum);        while (r == null && rownum <= getLastRowNum())        {            r = getRow(++rownum);        }        if (rownum > getLastRowNum())            return -1;        return rownum;    }    /**     * add a row to the sheet     *     * @param addLow whether to add the row to the low level model - false if its already there     */    private void addRow(HSSFRow row, boolean addLow)    {        rows.put(row, row);        if (addLow)        {            sheet.addRow(row.getRowRecord());        }        if (row.getRowNum() > getLastRowNum())        {            lastrow = row.getRowNum();        }        if (row.getRowNum() < getFirstRowNum())        {            firstrow = row.getRowNum();        }    }    /**     * Returns the logical row (not physical) 0-based.  If you ask for a row that is not     * defined you get a null.  This is to say row 4 represents the fifth row on a sheet.     * @param rownum  row to get     * @return HSSFRow representing the rownumber or null if its not defined on the sheet     */    public HSSFRow getRow(int rownum)    {        HSSFRow row = new HSSFRow();        //row.setRowNum((short) rownum);        row.setRowNum( rownum);        return (HSSFRow) rows.get(row);    }    /**     * Returns the number of phsyically defined rows (NOT the number of rows in the sheet)     */    public int getPhysicalNumberOfRows()    {        return rows.size();    }    /**     * gets the first row on the sheet     * @return the number of the first logical row on the sheet     */    public int getFirstRowNum()    {        return firstrow;    }    /**     * gets the last row on the sheet     * @return last row contained n this sheet.     */    public int getLastRowNum()    {        return lastrow;    }    /**     * set the width (in units of 1/256th of a character width)     * @param column - the column to set (0-based)     * @param width - the width in units of 1/256th of a character width     */    public void setColumnWidth(short column, short width)    {        sheet.setColumnWidth(column, width);    }    /**     * get the width (in units of 1/256th of a character width )     * @param column - the column to set (0-based)     * @return width - the width in units of 1/256th of a character width     */    public short getColumnWidth(short column)    {        return sheet.getColumnWidth(column);    }    /**     * get the default column width for the sheet (if the columns do not define their own width) in     * characters     * @return default column width     */    public short getDefaultColumnWidth()    {        return sheet.getDefaultColumnWidth();    }    /**     * get the default row height for the sheet (if the rows do not define their own height) in     * twips (1/20 of  a point)     * @return  default row height     */    public short getDefaultRowHeight()    {        return sheet.getDefaultRowHeight();    }    /**     * get the default row height for the sheet (if the rows do not define their own height) in     * points.     * @return  default row height in points     */    public float getDefaultRowHeightInPoints()    {        return (sheet.getDefaultRowHeight() / 20);    }    /**     * set the default column width for the sheet (if the columns do not define their own width) in     * characters     * @param width default column width     */    public void setDefaultColumnWidth(short width)    {        sheet.setDefaultColumnWidth(width);    }    /**     * set the default row height for the sheet (if the rows do not define their own height) in     * twips (1/20 of  a point)     * @param  height default row height     */    public void setDefaultRowHeight(short height)    {        sheet.setDefaultRowHeight(height);    }    /**     * set the default row height for the sheet (if the rows do not define their own height) in     * points     * @param height default row height     */    public void setDefaultRowHeightInPoints(float height)    {        sheet.setDefaultRowHeight((short) (height * 20));    }    /**     * get whether gridlines are printed.     * @return true if printed     */    public boolean isGridsPrinted()    {        return sheet.isGridsPrinted();    }    /**     * set whether gridlines printed.     * @param value  false if not printed.     */    public void setGridsPrinted(boolean value)    {

⌨️ 快捷键说明

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