Apache.POI.HSSF获取单元格数据为String类型(参考源码)新

2024-04-10 00:32

本文主要是介绍Apache.POI.HSSF获取单元格数据为String类型(参考源码)新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 morepackage 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 - 1private 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 recordsRecord 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*/@Overridepublic 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(

这篇关于Apache.POI.HSSF获取单元格数据为String类型(参考源码)新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/889631

相关文章

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

MySQL中删除重复数据SQL的三种写法

《MySQL中删除重复数据SQL的三种写法》:本文主要介绍MySQL中删除重复数据SQL的三种写法,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录方法一:使用 left join + 子查询删除重复数据(推荐)方法二:创建临时表(需分多步执行,逻辑清晰,但会

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

详谈redis跟数据库的数据同步问题

《详谈redis跟数据库的数据同步问题》文章讨论了在Redis和数据库数据一致性问题上的解决方案,主要比较了先更新Redis缓存再更新数据库和先更新数据库再更新Redis缓存两种方案,文章指出,删除R... 目录一、Redis 数据库数据一致性的解决方案1.1、更新Redis缓存、删除Redis缓存的区别二

Redis事务与数据持久化方式

《Redis事务与数据持久化方式》该文档主要介绍了Redis事务和持久化机制,事务通过将多个命令打包执行,而持久化则通过快照(RDB)和追加式文件(AOF)两种方式将内存数据保存到磁盘,以防止数据丢失... 目录一、Redis 事务1.1 事务本质1.2 数据库事务与redis事务1.2.1 数据库事务1.

Mysql 中的多表连接和连接类型详解

《Mysql中的多表连接和连接类型详解》这篇文章详细介绍了MySQL中的多表连接及其各种类型,包括内连接、左连接、右连接、全外连接、自连接和交叉连接,通过这些连接方式,可以将分散在不同表中的相关数据... 目录什么是多表连接?1. 内连接(INNER JOIN)2. 左连接(LEFT JOIN 或 LEFT

Apache Tomcat服务器版本号隐藏的几种方法

《ApacheTomcat服务器版本号隐藏的几种方法》本文主要介绍了ApacheTomcat服务器版本号隐藏的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1. 隐藏HTTP响应头中的Server信息编辑 server.XML 文件2. 修China编程改错误

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

更改docker默认数据目录的方法步骤

《更改docker默认数据目录的方法步骤》本文主要介绍了更改docker默认数据目录的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1.查看docker是否存在并停止该服务2.挂载镜像并安装rsync便于备份3.取消挂载备份和迁