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

相关文章

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模