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

相关文章

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils