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

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

SpringBoot集成EasyPoi实现Excel模板导出成PDF文件

《SpringBoot集成EasyPoi实现Excel模板导出成PDF文件》在日常工作中,我们经常需要将数据导出成Excel表格或PDF文件,本文将介绍如何在SpringBoot项目中集成EasyPo... 目录前言摘要简介源代码解析应用场景案例优缺点分析类代码方法介绍测试用例小结前言在日常工作中,我们经