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

组件,可通过nuget安装,直接搜名字:

ExcelDataReader

csharp 复制代码
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,wanghaoli
    public class WantaiExcelRead2
    {
        public Analysis_wt_View ReadExcel(string filePath)
        {
#if DEBUG
            string 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;
        }



    }
}
相关推荐
俊俊几年的34 分钟前
RK3588 硬件架构详解:CPU、GPU、NPU 与 RKNN 软件栈
linux·c++·python·yolo·numpy·rockchip
沫璃染墨1 小时前
从零入门MySQL系列(一):Linux 环境下 MySQL 安装部署、服务启动与首次登录问题排查
linux·运维·数据库·sql·mysql·database·数据库架构
打工仔折腾 AI1 小时前
Linux网络:UDP一次sendto对应一次recvfrom的底层机制解析
linux·网络·人工智能·后端·python·性能优化·udp
程序员与背包客_CoderZ1 小时前
Linux C++异步网络编程:c-ares高性能DNS解析编码实战
linux·服务器·c语言·开发语言·网络·c++·网络编程
鱼很腾apoc1 小时前
【Redis】第4期 详解主从复制、哨兵、集群
linux·redis·学习·集群·主从复制·哨兵
老一岁2 小时前
Git详解_提交_回退_分支_合并
大数据·linux·git
ai_xiaogui2 小时前
Ubuntu Server 安装避坑实录:服务器装成桌面版、网卡驱动缺失怎么办?
linux·服务器·ubuntu·ubuntu server·ubuntu 网卡驱动安装·服务器装机避坑·服务器装机
小则又沐风a2 小时前
线程的互斥和同步
linux·c++
开发者导航2 小时前
Cursor plugins 插件仓库上手:结构解析与安装使用教程
android·linux·ubuntu·ai·typescript·cursor·插件开发
灵析表格2 小时前
灵析表格函数精讲|Query运行Sql:自定义SQL取数
excel·办公自动化·wps·效率工具·数据处理·ai办公·函数公式