当前位置: 首页 > news >正文

Linux中.NET读取excel组件,不会出现The type initializer for ‘Gdip‘ threw an exception异常

组件,可通过nuget安装,直接搜名字:
ExcelDataReader
在这里插入图片描述

using ConsoleAppReadFileData.Model;
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleAppReadFileData.FileRead
{/// <summary>///  xls文件读取,/// 使用ExcelDataReader组件读取,不依赖图形组件,兼容Linux/// 不会报这个错误:System.TypeInitializationException : The type initializer for 'Gdip' threw an exception/// </summary>/// 2024-11-8 17:47:46,wanghaolipublic class WantaiExcelRead2{public Analysis_wt_View ReadExcel(string filePath){
#if DEBUGstring dir = @"G:\work\test\11220241060";//filePath = Path.Combine(dir, "2024-11-04_A_1_20241107.xls");filePath = Path.Combine(dir, "2024-11-04_A_2_20241107.xls");#endif//   string filepath2 = "/data/WANTAI03595/2024-11-04_B_1_20241107.xls";Analysis_wt_View analysis_Wt_View = new Analysis_wt_View();try{using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read)){var options = new ExcelReaderConfiguration(){//设置读取Excel文件的编码方式,例如GB2312,用于处理中文字符FallbackEncoding = Encoding.GetEncoding("GB2312"),};//1.打开xls文件using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream, options)){do{int rowIndex = 1;/* 行号*/List<Analysis_wt_details> analysislist = new List<Analysis_wt_details>();//循环行while (reader.Read()){if (rowIndex == 1){//实验名称analysis_Wt_View.Experiment_name = reader.GetString(1);}if (rowIndex == 2){//实验时间analysis_Wt_View.Experimental_time = reader.GetString(3);//仪器型号analysis_Wt_View.DeviceModel = reader.GetString(5);//样本数量analysis_Wt_View.Samples_num = Convert.ToInt32(reader.GetValue(1));}if (rowIndex == 3){//仪器编码analysis_Wt_View.Device_code = reader.GetString(6);}if (rowIndex < 10){goto ReadEnd;}if (rowIndex >= 4 && rowIndex < 11){goto ReadEnd;}// Analysis_wt_details  rowInfo = new Analysis_wt_details(); 读取每一行的数据//for (int i = 0; i < reader.FieldCount; i++)//{//    //单元格数据//    var cellValue = reader.GetValue(i);//    Console.Write(cellValue + "\t");                                //}int cell_index = 0;Analysis_wt_details rowInfo = new Analysis_wt_details(){No = Convert.ToInt32(reader.GetValue(cell_index++)),TypeName = reader.GetString(cell_index++),SampleName = reader.GetString(cell_index++),SampleBarcode = reader.GetString(cell_index++),SampleLocation = reader.GetString(cell_index++),Detection = reader.GetString(cell_index++),TestItem = reader.GetString(cell_index++),PCR = reader.GetString(cell_index++),HBV = reader.GetString(cell_index++),HCV = reader.GetString(cell_index++),HIV = reader.GetString(cell_index++),IC = reader.GetString(cell_index++),DetectionResult = reader.GetString(cell_index++),ExperimentalRecords = reader.GetString(cell_index++)};analysislist.Add(rowInfo);ReadEnd:rowIndex++;//Console.WriteLine();//行结束}analysis_Wt_View.Analysislist = analysislist;//循环shell} while (reader.NextResult());}}}catch (Exception ex){LogHelpter.AddLog($"万泰读取excel异常={ex.ToString()}", null, "wantai_error");}return analysis_Wt_View;}}
}
http://www.lryc.cn/news/481228.html

相关文章:

  • mmclassification的配置文件样本
  • Java基础——类和对象的定义链表的创建,输出
  • Linux应用项目之量产工具(一)——显示系统
  • Python小白学习教程从入门到入坑------第二十九课 访问模式(语法进阶)
  • 使用 PageHelper 在 Spring Boot 项目中实现分页查询
  • 深度学习-张量相关
  • 电脑提示xinput1_3.dll丢失怎么解决,分享6种有效的解决方法
  • 【计网】数据链路层笔记
  • 蓝牙FTP 协议详解及 Android 实现
  • 【前端】Svelte:动画效果
  • 2024系统架构师--论基于架构的软件设计方法(ABSD)及应用(论文范文)
  • ORU 的 Open RAN 管理平面 (M 平面)
  • 软件缺陷等级评定综述
  • Nuxt.js 应用中的 schema:extend事件钩子详解
  • 自然语言处理在客户服务中的应用
  • OpenCoder:首个完全开源的顶级代码大模型,训练秘籍全公开!| LLM×MapReduce,无需训练就超越GPT-4!
  • springboot静态资源映射不生效问题
  • 通过 SSH 隧道将本地端口转发到远程主机
  • 【北京迅为】itop-3588开发板摄像头使用手册Android12 双摄方案
  • 初见Linux:基础开发工具
  • 微服务架构面试内容整理-分布式配置管理-Nacos Config
  • React官网生成Recat项目的区别
  • 网络安全---安全见闻
  • 在 CSS 中,gap 是 布局容器(flex 或 grid)的属性。它用于设置容器内子元素之间的间距。
  • [zotero]Ubuntu搭建WebDAV网盘
  • 力扣17-电话号码的数字组合
  • 如何处理模型的过拟合和欠拟合问题
  • CSRF详解
  • C# winform 的数据采集,采集周期是间隔10ms、100ms等等,但始终都有1ms的误差,并不是精准的10ms,哪些原因呢
  • 【国内中间件厂商排名及四大中间件对比分析】