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获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I