POI获取单元格数据为String类型
HSSFCell 如何获取数据类型 String
用法:
/**
* .
* 获取单元格数据内容为字符串类型的数据
* TODO
* @param cell Excel单元格
* @returnString 单元格数据内容
*/
private String getStringCellValue(HSSFCell cell) {
String cellValue = "";
if(cell.getCellType()==cell.CELL_TYPE_STRING) {cellValue = cell.getRichStringCellValue().getString(); }
if(cell.getCellType()==cell.CELL_TYPE_NUMERIC) {
cell.setCellType(cell.CELL_TYPE_STRING);
cellValue = String.valueOf(cell.getRichStringCellValue().getString());
}
return cellValue;
}
用switch,case编译器会报错。(case值必须为常量)
参考源码:
Licensed to the Apache Software Foundation (ASF) under one or more
package org.apache.poi.hssf.usermodel;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.HyperlinkRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordBase;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.ptg.ExpPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
/**
* 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 contain numbers and numeric cells cannot
* contain strings (at least according to our model). Client apps should do the
* conversions themselves. Formula cells have the formula string, as well as
* the formula result, which can be numeric or string.
* <p>
* Cells should have their number (0 based) before being added to a row. Only
* cells that have values should be added.
* <p>
*/
public class HSSFCell implements Cell {
private static final String FILE_FORMAT_NAME = "BIFF8";
/**
* The maximum number of columns in BIFF8
*/
public static final int LAST_COLUMN_NUMBER = SpreadsheetVersion.EXCEL97.getLastColumnIndex(); // 2^8 - 1
private static final String LAST_COLUMN_NAME = SpreadsheetVersion.EXCEL97.getLastColumnName();
public final static short ENCODING_UNCHANGED = -1;
public final static short ENCODING_COMPRESSED_UNICODE = 0;
public final static short ENCODING_UTF_16 = 1;
private final HSSFWorkbook _book;
private final HSSFSheet _sheet;
private CellType _cellType;
private HSSFRichTextString _stringValue;
private CellValueRecordInterface _record;
private HSSFComment _comment;
/**
* 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 {@link CellType#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(int)
*/
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col)
{
checkBounds(col);
_stringValue = null;
_book = book;
_sheet = sheet;
// Relying on the fact that by default the cellType is set to 0 which
// is different to {@link CellType#BLANK} hence the following method call correctly
// creates a new blank cell.
short xfindex = sheet.getSheet().getXFIndexForColAt(col);
setCellType(CellType.BLANK, false, row, col,xfindex);
}
/**
* Returns the HSSFSheet this cell belongs to
*
* @return the HSSFSheet that owns this cell
*/
public HSSFSheet getSheet() {
return _sheet;
}
/**
* Returns the HSSFRow this cell belongs to
*
* @return the HSSFRow that owns this cell
*/
public HSSFRow getRow() {
int rowIndex = getRowIndex();
return _sheet.getRow(rowIndex);
}
/**
* 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 - Type of cell
* @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(int,int)
*/
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
CellType type)
{
checkBounds(col);
_cellType = CellType._NONE; // Force 'setCellType' to create a first Record
_stringValue = null;
_book = book;
_sheet = sheet;
short xfindex = sheet.getSheet().getXFIndexForColAt(col);
setCellType(type,false,row,col,xfindex);
}
/**
* 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(HSSFWorkbook book, HSSFSheet sheet, CellValueRecordInterface cval) {
_record = cval;
_cellType = determineType(cval);
_stringValue = null;
_book = book;
_sheet = sheet;
switch (_cellType)
{
case STRING :
_stringValue = new HSSFRichTextString(book.getWorkbook(), (LabelSSTRecord ) cval);
break;
case BLANK :
break;
case FORMULA :
_stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue());
break;
default :
break;
}
}
/**
* used internally -- given a cell value record, figure out its type
*/
private static CellType determineType(CellValueRecordInterface cval) {
if (cval instanceof FormulaRecordAggregate) {
return CellType.FORMULA;
}
// all others are plain BIFF records
Record record = ( Record ) cval;
switch (record.getSid()) {
case NumberRecord.sid : return CellType.NUMERIC;
case BlankRecord.sid : return CellType.BLANK;
case LabelSSTRecord.sid : return CellType.STRING;
case BoolErrRecord.sid :
BoolErrRecord boolErrRecord = ( BoolErrRecord ) record;
return boolErrRecord.isBoolean()
? CellType.BOOLEAN
: CellType.ERROR;
}
throw new RuntimeException("Bad cell value rec (" + cval.getClass().getName() + ")");
}
/**
* Returns the Workbook that this Cell is bound to
*/
protected InternalWorkbook getBoundWorkbook() {
return _book.getWorkbook();
}
/**
* @return the (zero based) index of the row containing this cell
*/
@Override
public int getRowIndex() {
return _record.getRow();
}
/**
* Updates the cell record's idea of what
* column it belongs in (0 based)
* @param num the new cell number
*/
protected void updateCellNum(