📄 hssfcell.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/>. *//* * Cell.java * * Created on September 30, 2001, 3:46 PM */package org.apache.poi.hssf.usermodel;import org.apache.poi.hssf.model.Workbook;import org.apache.poi.hssf.model.Sheet;import org.apache.poi.hssf.model.FormulaParser;import org.apache.poi.hssf.record.CellValueRecordInterface;import org.apache.poi.hssf.record.Record;import org.apache.poi.hssf.record.FormulaRecord;import org.apache.poi.hssf.record.LabelSSTRecord;import org.apache.poi.hssf.record.NumberRecord;import org.apache.poi.hssf.record.BlankRecord;import org.apache.poi.hssf.record.BoolErrRecord;import org.apache.poi.hssf.record.ExtendedFormatRecord;import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;import org.apache.poi.hssf.record.formula.Ptg;import org.apache.poi.hssf.util.SheetReferences;//import org.apache.poi.hssf.record.formula.FormulaParser;import java.util.Date;import java.util.Calendar;/** * High level representation of a cell in a row of a spreadsheet. * Cells can be numeric, formula-based or string-based (text). The cell type * specifies this. String cells cannot conatin numbers and numeric cells cannot * contain strings (at least according to our model). Client apps should do the * conversions themselves. Formula cells are treated like string cells, simply * containing a formula string. They'll be rendered differently. * <p> * Cells should have their number (0 based) before being added to a row. Only * cells that have values should be added. * <p> * NOTE: the alpha won't be implementing formulas * * @author Andrew C. Oliver (acoliver at apache dot org) * @author Dan Sherman (dsherman at isisph.com) * @author Brian Sanders (kestrel at burdell dot org) Active Cell support * @version 1.0-pre */public class HSSFCell{ /** * Numeric Cell type (0) * @see #setCellType(int) * @see #getCellType() */ public final static int CELL_TYPE_NUMERIC = 0; /** * String Cell type (1) * @see #setCellType(int) * @see #getCellType() */ public final static int CELL_TYPE_STRING = 1; /** * Formula Cell type (2) * @see #setCellType(int) * @see #getCellType() */ public final static int CELL_TYPE_FORMULA = 2; /** * Blank Cell type (3) * @see #setCellType(int) * @see #getCellType() */ public final static int CELL_TYPE_BLANK = 3; /** * Boolean Cell type (4) * @see #setCellType(int) * @see #getCellType() */ public final static int CELL_TYPE_BOOLEAN = 4; /** * Error Cell type (5) * @see #setCellType(int) * @see #getCellType() */ public final static int CELL_TYPE_ERROR = 5; public final static short ENCODING_COMPRESSED_UNICODE = 0; public final static short ENCODING_UTF_16 = 1; private short cellNum; private int cellType; private HSSFCellStyle cellStyle; private double cellValue; private String stringValue; private boolean booleanValue; private byte errorValue; private short encoding = ENCODING_COMPRESSED_UNICODE; private Workbook book; private Sheet sheet; //private short row; private int row; private CellValueRecordInterface record; /** * Creates new Cell - Should only be called by HSSFRow. This creates a cell * from scratch. * <p> * When the cell is initially created it is set to CELL_TYPE_BLANK. Cell types * can be changed/overwritten by calling setCellValue with the appropriate * type as a parameter although conversions from one type to another may be * prohibited. * * @param book - Workbook record of the workbook containing this cell * @param sheet - Sheet record of the sheet containing this cell * @param row - the row of this cell * @param col - the column for this cell * * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short) */ //protected HSSFCell(Workbook book, Sheet sheet, short row, short col) protected HSSFCell(Workbook book, Sheet sheet, int row, short col) { checkBounds(col); cellNum = col; this.row = row; cellStyle = null; cellValue = 0; stringValue = null; booleanValue = false; errorValue = ( byte ) 0; this.book = book; this.sheet = sheet; // Relying on the fact that by default the cellType is set to 0 which // is different to CELL_TYPE_BLANK hence the following method call correctly // creates a new blank cell. setCellType(CELL_TYPE_BLANK, false); ExtendedFormatRecord xf = book.getExFormatAt(0xf); setCellStyle(new HSSFCellStyle(( short ) 0xf, xf)); } /** * Creates new Cell - Should only be called by HSSFRow. This creates a cell * from scratch. * * @param book - Workbook record of the workbook containing this cell * @param sheet - Sheet record of the sheet containing this cell * @param row - the row of this cell * @param col - the column for this cell * @param type - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK, * CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR * Type of cell * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short,int) * @deprecated As of 22-Jan-2002 use @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short) * and use setCellValue to specify the type lazily. */ //protected HSSFCell(Workbook book, Sheet sheet, short row, short col, protected HSSFCell(Workbook book, Sheet sheet, int row, short col, int type) { checkBounds(col); cellNum = col; this.row = row; cellType = type; cellStyle = null; cellValue = 0; stringValue = null; booleanValue = false; errorValue = ( byte ) 0; this.book = book; this.sheet = sheet; switch (type) { case CELL_TYPE_NUMERIC : record = new NumberRecord(); (( NumberRecord ) record).setColumn(col); (( NumberRecord ) record).setRow(row); (( NumberRecord ) record).setValue(( short ) 0); (( NumberRecord ) record).setXFIndex(( short ) 0); break; case CELL_TYPE_STRING : record = new LabelSSTRecord(); (( LabelSSTRecord ) record).setColumn(col); (( LabelSSTRecord ) record).setRow(row); (( LabelSSTRecord ) record).setXFIndex(( short ) 0); break; case CELL_TYPE_BLANK : record = new BlankRecord(); (( BlankRecord ) record).setColumn(col); (( BlankRecord ) record).setRow(row); (( BlankRecord ) record).setXFIndex(( short ) 0); break; case CELL_TYPE_FORMULA : FormulaRecord formulaRecord = new FormulaRecord(); record = new FormulaRecordAggregate(formulaRecord,null); formulaRecord.setColumn(col); formulaRecord.setRow(row); formulaRecord.setXFIndex(( short ) 0); case CELL_TYPE_BOOLEAN : record = new BoolErrRecord(); (( BoolErrRecord ) record).setColumn(col); (( BoolErrRecord ) record).setRow(row); (( BoolErrRecord ) record).setXFIndex(( short ) 0); (( BoolErrRecord ) record).setValue(false); break; case CELL_TYPE_ERROR : record = new BoolErrRecord(); (( BoolErrRecord ) record).setColumn(col); (( BoolErrRecord ) record).setRow(row); (( BoolErrRecord ) record).setXFIndex(( short ) 0); (( BoolErrRecord ) record).setValue(( byte ) 0); break; } ExtendedFormatRecord xf = book.getExFormatAt(0xf); setCellStyle(new HSSFCellStyle(( short ) 0xf, xf)); } /** * Creates an HSSFCell from a CellValueRecordInterface. HSSFSheet uses this when * reading in cells from an existing sheet. * * @param book - Workbook record of the workbook containing this cell * @param sheet - Sheet record of the sheet containing this cell * @param cval - the Cell Value Record we wish to represent */ //protected HSSFCell(Workbook book, Sheet sheet, short row, protected HSSFCell(Workbook book, Sheet sheet, int row, CellValueRecordInterface cval) { cellNum = cval.getColumn(); record = cval; this.row = row; cellType = determineType(cval); cellStyle = null; stringValue = null; this.book = book; this.sheet = sheet; switch (cellType) { case CELL_TYPE_NUMERIC : cellValue = (( NumberRecord ) cval).getValue(); break; case CELL_TYPE_STRING : stringValue = book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex()); break; case CELL_TYPE_BLANK : break; case CELL_TYPE_FORMULA : cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue(); stringValue=((FormulaRecordAggregate) cval).getStringValue(); break; case CELL_TYPE_BOOLEAN :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -