NPOI.HSSF 导入excel数据(记录)

2024-04-25 02:48

本文主要是介绍NPOI.HSSF 导入excel数据(记录),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://download.csdn.net/download/ttbat/15435334  NPOI.HSSF.Dll 下载地址。

在项目引用 NPOI.HSSF.Dll 后 using NPOI.HSSF.UserModel;

PS:excel文件只能读取1997-2003版本的,2007以上的excel版本无法读取。

        /// <summary>/// /// </summary>/// <param name="path">文件地址</param>/// <param name="_syptktService"></param>/// <param name="success">成功数量</param>/// <param name="failure">错误数量</param>/// <returns></returns>
public static string ImprotSXSC(string path, ISYPTKTService _syptktService, out int success, out int failure){string errorMsg = string.Empty;success = failure = 0;try{HSSFWorkbook hssfworkbook;using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read)){#region 检查excel数据hssfworkbook = new HSSFWorkbook(file);HSSFSheet sheet = hssfworkbook.GetSheetAt(0);if (sheet == null || sheet.LastRowNum < 0){errorMsg = "excel格式异常,请检查";return errorMsg;}Dictionary<string, int> headCellDic = GetCellDictNum(sheet, 0); //表头键与索引集合errorMsg = CheckExcel(sheet);if (!string.IsNullOrEmpty(errorMsg))return errorMsg;#endregionHSSFRow row = null;HSSFCell cell;string rowMsg;int startRowIndex = 1;for (int i = startRowIndex; i <= sheet.LastRowNum; i++){                        row = sheet.GetRow(i);cell = row.GetCell(headCellDic["A"]);string name = getCellStringValue(row, cell, "A", out rowMsg);var model = _syptktService.GetSxScxts(s => s.NAME.Contains(name)).FirstOrDefault();bool isnew = false;int failure1 = 0;if (model == null){model = new SXSCXTEntiry();isnew = true;}model.NAME = name;cell = row.GetCell(headCellDic["B"]);string X = getCellStringValue(row, cell, "B", out rowMsg);cell = row.GetCell(headCellDic["C"]);string Y = getCellStringValue(row, cell, "C", out rowMsg);float x = CommonMethodNew.FromDFM(X);float y = CommonMethodNew.FromDFM(Y);model.X = X;model.Y = Y;if (X != "" && Y != "" && x >= 3 && y >= 2){model.SHAPE = "POINT ( " + x + " " + y + ")";}if (string.IsNullOrEmpty(name)){failure1++;errorMsg += string.Format("第{0}行水下生产系统名称未录入$", i);}if (string.IsNullOrEmpty(model.SHAPE)){failure1++;errorMsg += string.Format("{0}水下生产系统经纬度数据不正确$", name);                           }if (failure1 > 0){ failure++; continue; }}}}catch (Exception ex){failure++;errorMsg = ex.ToString();}return errorMsg;}/// <summary>/// 获取excel指定行中,列键值(键/索引)集合/// </summary>/// <param name="sheet"></param>/// <param name="rowIndex"></param>/// <returns></returns>public static Dictionary<string, int> GetCellDictNum(HSSFSheet sheet,int rowIndex){Dictionary<string, int> celldict = new Dictionary<string, int>();HSSFRow frow = sheet.GetRow(rowIndex);int colNum = frow.PhysicalNumberOfCells;char A = 'A';int Acode = (int)A;int firA = 0;String sfir = "";String stow = "";for (int i = 0; i < colNum; i++){if (i < 26){celldict.Add(((char)(Acode + i)).ToString(), i);}else if (i >= 26 && i <= (26 * 26)){firA = i / 26;sfir = ((char)(Acode + firA - 1)).ToString();firA = i % 26;stow = ((char)(Acode + firA)).ToString();celldict.Add(sfir + stow, i);}}return celldict;}/// <summary>/// 获取文本格式/// </summary>/// <param name="row"></param>/// <param name="cell"></param>/// <param name="cellKey"></param>/// <param name="errorMsg"></param>/// <returns></returns>public static string getCellStringValue(HSSFRow row, HSSFCell cell, string cellKey, out string errorMsg){errorMsg = string.Empty;string value = null;if (cell == null){return null;}if (cell.CellType == HSSFCell.CELL_TYPE_NUMERIC)value= cell.NumericCellValue.ToString();else if (cell.CellType == HSSFCell.CELL_TYPE_STRING)value= cell.StringCellValue;else{try{value = cell.StringCellValue;}catch (Exception ex){errorMsg = string.Format("文档第" + (row.RowNum + 1) + "行," + cellKey + "列格式不正确");}}if(!string.IsNullOrEmpty(value))value = value.Trim().Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""); ;return value;}

 

这篇关于NPOI.HSSF 导入excel数据(记录)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

前端导出Excel文件出现乱码或文件损坏问题的解决办法

《前端导出Excel文件出现乱码或文件损坏问题的解决办法》在现代网页应用程序中,前端有时需要与后端进行数据交互,包括下载文件,:本文主要介绍前端导出Excel文件出现乱码或文件损坏问题的解决办法,... 目录1. 检查后端返回的数据格式2. 前端正确处理二进制数据方案 1:直接下载(推荐)方案 2:手动构造

C#利用Free Spire.XLS for .NET复制Excel工作表

《C#利用FreeSpire.XLSfor.NET复制Excel工作表》在日常的.NET开发中,我们经常需要操作Excel文件,本文将详细介绍C#如何使用FreeSpire.XLSfor.NET... 目录1. 环境准备2. 核心功能3. android示例代码3.1 在同一工作簿内复制工作表3.2 在不同

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

Pandas处理缺失数据的方式汇总

《Pandas处理缺失数据的方式汇总》许多教程中的数据与现实世界中的数据有很大不同,现实世界中的数据很少是干净且同质的,本文我们将讨论处理缺失数据的一些常规注意事项,了解Pandas如何表示缺失数据,... 目录缺失数据约定的权衡Pandas 中的缺失数据None 作为哨兵值NaN:缺失的数值数据Panda

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

java读取excel文件为base64实现方式

《java读取excel文件为base64实现方式》文章介绍使用ApachePOI和EasyExcel处理Excel文件并转换为Base64的方法,强调EasyExcel适合大文件且内存占用低,需注意... 目录使用 Apache POI 读取 Excel 并转换为 Base64使用 EasyExcel 处

docker编写java的jar完整步骤记录

《docker编写java的jar完整步骤记录》在平常的开发工作中,我们经常需要部署项目,开发测试完成后,最关键的一步就是部署,:本文主要介绍docker编写java的jar的相关资料,文中通过代... 目录all-docker/生成Docker打包部署文件配置服务A的Dockerfile (a/Docke

python库pydantic数据验证和设置管理库的用途

《python库pydantic数据验证和设置管理库的用途》pydantic是一个用于数据验证和设置管理的Python库,它主要利用Python类型注解来定义数据模型的结构和验证规则,本文给大家介绍p... 目录主要特点和用途:Field数值验证参数总结pydantic 是一个让你能够 confidentl

Python Excel 通用筛选函数的实现

《PythonExcel通用筛选函数的实现》本文主要介绍了PythonExcel通用筛选函数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录案例目的示例数据假定数据来源是字典优化:通用CSV数据处理函数使用说明使用示例注意事项案例目的第一