首页 > 解决方案 > 使用 C# selenium web 驱动程序从 excel 读取数据时出现异常

问题描述

我在使用 selenium Web 驱动程序从 Excel 读取数据时遇到问题。我使用过“Microsoft.Office.Interop.Excel”,并且在运行我的代码时发现了该异常

System.Runtime.InteropServices.Exception:Excel 无法访问“下载”。该文档可以是只读的或加密的。 在此处输入图像描述

这是我使用的代码 public void sch_issue_1958M() {

        Application xlApp = new Application();
        Workbook xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\kkhatab\Desktop\3.0.0\Cube.Portal.Regression_for_issuesTests\bin\Downloads");
        Worksheet xlWorkSheet = new Worksheet();
        xlWorkSheet = xlWorkBook.Sheets[1];
        Range xlRange = xlWorkSheet.UsedRange;
        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;
        for (int i = 1; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                //new line
                if (j == 1)
                    Console.Write("\r\n");

                //write the value to the console
                if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                    Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");


            }


        }
    }

标签: c#excel

解决方案


只是为了其他人清楚:

工作簿 xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\kkhatab\Desktop\3.0.0\Cube.Portal.Regression_for_issuesTests\bin\Downloads");

您试图在此处打开文件夹而不是文档。


推荐阅读