简体   繁体   English

从 excel 读取值

[英]Read value from excel

I have a problem that I can't solve.我有一个我无法解决的问题。 I need to pass a value from one Selenium test to another.我需要将一个 Selenium 测试中的值传递给另一个。 And the writing part works just fine.写作部分工作得很好。 I saves the value to an excel file.我将值保存到 excel 文件中。 The problem appears when I try to read it.当我尝试阅读时出现问题。 I have a class which should be able to read the cell, but it won't.我有一个 class 应该能够读取单元格,但它不会。

Excel.cs Excel.cs

using System.IO;
using OfficeOpenXml;
using excel = Microsoft.Office.Interop.Excel;
namespace Core
{
    public sealed class Excel
    {
        #region Private Properties
        private static string InputPath { get; } = ConfigSettingProvider.InputSourcePath;

        private static string OutputPath { get; } = ConfigSettingProvider.OutputSourcePath;
        #endregion Private Properties

        public static string ReadCell (int sheet, int x, int y) // (sheet, row, column)
        {

            FileInfo existingFile = new FileInfo("..\\..\\..\\Property\\Input\\data1.xlsx");
            
            using (ExcelPackage package = new ExcelPackage(existingFile))
            {
                return package.Workbook.Worksheets[sheet].Cells[x, y].Value.ToString();
            }
        }
     }
}

And then in Test Fixture class I am trying to call ReadCell method:然后在测试夹具 class 我试图调用 ReadCell 方法:

var cellPolicyNo = Excel.ReadCell(1, 1, 1);

But it simply won't even enter to read it.但它根本就不会进入阅读它。 Immediately the exception is thrown: "The type initializer for 'Core.Excel' threw an exception".立即引发异常:“'Core.Excel' 的类型初始化程序引发了异常”。 at Core.Excel.ReadCell(Int32 sheet, Int32 x, Int32 y)在 Core.Excel.ReadCell(Int32 表,Int32 x,Int32 y)

What am I doing wrong?我究竟做错了什么? What should I change?我应该改变什么?

Thank you谢谢

You might want to take a look at this thread to see if it helps with answering your question.你可能想看看这个线程,看看它是否有助于回答你的问题。

How to read single Excel cell value 如何读取单个 Excel 单元格值

This is the more common way of reading from an excel file that is more reliable.这是从更可靠的 excel 文件中读取的更常见的方式。 There are also ways of doing it through OLEDB but that is for reading the entire excel file.也有通过 OLEDB 执行此操作的方法,但用于读取整个 excel 文件。 Hopefully this helps as it is similar to code I have used in the past to read data from excel files.希望这会有所帮助,因为它类似于我过去用来从 excel 文件中读取数据的代码。

It appears to be a static constructor problem, see here: https://stackoverflow.com/questions/4398334/the-type-initializer-for-myclass-threw-an-exception#:~:text=The%20type%20initializer%20for%20%27CSMessageUtility.,-CSDetails%27%20threw%20an&text=means%20that%20the%20static%20constructor,static%20members%20of%20that%20class .它似乎是 static 构造函数问题,请参见此处: https://stackoverflow.com/questions/4398334/the-type-initializer-for-myclass-threw-an-exception#:~:text=The%20type%2 %20for%20%27CSMessageUtility.,-CSDetails%27%20threw%20an&text=表示%20that%20the%20static%20constructor,static%20members%20of%20that%20class

Btw, it might perhaps help to try a different toolkit, namely EPPlus (the last free version is 4.5.3.3).顺便说一句,尝试不同的工具包可能会有所帮助,即 EPPlus(最后一个免费版本是 4.5.3.3)。 I've tried to read cells also with Excel Interop but it didn't work out so well.我也尝试使用 Excel Interop 读取单元格,但效果不佳。 With EPPlus you can use LINQ on the range objects of Excel.使用 EPPlus,您可以在 Excel 的范围对象上使用 LINQ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM