【问题标题】:How do I get the project Excel path如何获取项目 Excel 路径
【发布时间】:2020-03-09 16:56:06
【问题描述】:

在我的项目中,我有 Test Data 文件夹,里面有我的 xlsx 文件。

我正在尝试获取项目路径文件夹,以便从它们读取而不是硬编码路径。但它总是从项目/bin/debug 路径中​​读取。这是我的 Excel 实用程序。在我的课堂上,我正在初始化 ExcelUtil 类并使用 ReadData 方法

    public class ExcelUtil
    {
        public static void InitializeExcel()
        {
            string exeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            File.OpenRead(System.IO.Path.Combine(exeDir, "Data.xlsx"));


        }

        public static DataTable ExcelToDataTable(string fileName)
        {
            //Open the file
            using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                //read the excel file
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        //using anaysome method
                        ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                        {
                            UseHeaderRow = true
                        }
                    });

                    //Storing in DataCollection
                    DataTableCollection table = result.Tables;
                    DataTable resultTable = table["Sheet1"];
                    return resultTable;
                }
            }
        }

        //storing the Data from excel in List type of othe custom class
        public static List<DataCollection> datacol = new List<DataCollection>();

        //Method populates the data into the collection
        public static void PopulateInCollection(string fileName)
        {
            DataTable table = ExcelToDataTable(fileName);

            //Iterating through the rows and columns
            for (int row = 1; row <= table.Rows.Count; row++)
            {
                for (int col = 0; col < table.Columns.Count; col++)
                {
                    DataCollection dTable = new DataCollection()
                    {
                        RowNumber = row,
                        ColName = table.Columns[col].ColumnName,
                        ColValue = table.Rows[row - 1][col].ToString()
                    };
                    //Add all the details for each row
                    datacol.Add(dTable);
                }
            }
        }

        //Method read data from excel using rownum and colname
        public static string ReadData(int rowNumber, string columnName)
        {
            try
            {
                //Retriving data using LINQO to reduce much iterations
                string data = (from colData in datacol
                               where colData.ColName == columnName && colData.RowNumber == rowNumber
                               select colData.ColValue).SingleOrDefault();

                return data.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }

    //Custom class to hold rowsnum and columnnum adn val data
    public class DataCollection
    {
        public int RowNumber { get; set; }
        public string ColName { get; set; }
        public string ColValue { get; set; }
    }
}

【问题讨论】:

  • 你需要说明你使用的是哪个测试框架。例如 NUnit 有 TestContext.CurrentContext.TestDirectory 因为它在运行测试时没有设置当前目录。
  • 阅读您的问题标题和描述,我以为您正在使用现有的测试框架进行单元或集成测试。
  • @Neil 我正在使用 Nunit 框架。我应该在 InitializeExcel 方法中遇到什么

标签: c# testing nunit excel-reader


【解决方案1】:

当您使用 NUnit 时,您需要从 TestContext.CurrentContext.TestDirectory 获取路径,这将是执行测试的文件夹。然后只需添加您的相对路径:

public static void InitializeExcel()
{
    string testDataPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData");
    File.OpenRead(System.IO.Path.Combine(testDataPath, "Data.xlsx"));
}

【讨论】:

  • 在 NUnit 3.x 中,这指向 &lt;project&gt;/bin/Debug/TestData。您如何获得它,以便将文件复制到 Release/Debug 文件夹或指向相对目录?
  • @EmmanuelF 在 SolutionExplorer 中,找到要复制到输出文件夹的文件,然后将“复制到输出文件夹”更改为“如果较新则复制”。该文件将显示在调试文件夹下方,相对于它在项目中的位置。
  • 谢谢@Neil!在摆弄设置后我能够弄清楚。
猜你喜欢
  • 2012-04-05
  • 1970-01-01
  • 2014-11-20
  • 2016-08-24
  • 2023-02-07
  • 2011-01-26
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多