【问题标题】:I try to open an Excel file with C#, I get this error: System.Runtime.InteropServices.COMException HResult=0x800A03E我尝试使用 C# 打开 Excel 文件,但出现此错误:System.Runtime.InteropServices.COMException HResult=0x800A03E
【发布时间】:2019-08-10 02:02:06
【问题描述】:

我尝试在 C# 中打开一个 Excel 文件,但出现此错误:

System.Runtime.InteropServices.COMException HResult=0x800A03E

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using excel = Microsoft.Office.Interop.Excel;

namespace Example_Reading_A_File_From_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            excel.Application x1app = new excel.Application();
            excel.Workbook x1workbook = x1app.Workbooks.Open(@"‪D:\saniaertest.xlsx");
            excel._Worksheet x1worksheet = x1workbook.Sheets[1];
            excel.Range x1range = x1worksheet.UsedRange;

            string website;

            for(int i = 1; i <= 3; i++)
            {
                website = x1range.Cells[i][9].value2;

                IWebDriver driver = new ChromeDriver();
                driver.Navigate().GoToUrl(website);
                Thread.Sleep(30);
                driver.Close();
            }
        }
    }
}

通常它应该打开 Excel 文件并打开 Chrome 浏览器并导航到第 9 列中列出的所有 URL。但就像之前所说的,我只得到错误:

System.Runtime.InteropServices.COMException HResult=0x800A03E

它说找不到工作簿。

【问题讨论】:

  • 仅仅异常类型是不够的。提供完整的异常消息和相应的 HRESULT 值。
  • 你永远不会退出excel.Application x1app。另见here
  • System.Runtime.InteropServices.COMException HResult=0x800A03EC
  • @oherby 您刚刚在聊天中输入的内容与您在问题中的内容不同。请编辑您的问题并澄清。
  • 也许找不到工作簿...因为这就是错误消息所述的内容。您是否确认工作簿在该路径中并且您没有拼写错误?工作簿是否以 .xls 或 .xlsx 结尾?等

标签: c# excel selenium selenium-webdriver


【解决方案1】:

这很奇怪,因为我尝试了你代码的前几行,但我得到了同样的错误,尽管我的文件确实存在于我放置它的位置。

由于某种原因,Workbooks.Open 没有指向正确的文件路径,所以 我必须将其更新为以下内容:

string file = Directory.GetCurrentDirectory() + "\\" + "Test.xlsx";

if (File.Exists(file))
  {
     excel.Application x1app = new excel.Application();
     excel.Workbook x1workbook = x1app.Workbooks.Open(file);
     excel._Worksheet x1worksheet = x1workbook.Sheets[1];   
     excel.Range x1range = x1worksheet.UsedRange;
  }

这对我有用,因为我不再收到找不到工作簿的错误。

【讨论】:

    【解决方案2】:

    我建议改用ExcelDataReader 包。它不需要在机器上安装 Excel 或 InteropServices。作为奖励,它还可以在 Linux 中运行时读取文件。

    using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
    {
        // Auto-detect format, supports:
        //  - Binary Excel files (2.0-2003 format; *.xls)
        //  - OpenXml Excel files (2007 format; *.xlsx)
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            // Choose one of either 1 or 2:
    
            // 1. Use the reader methods
            do
            {
                while (reader.Read())
                {
                    // reader.GetDouble(0);
                }
            } while (reader.NextResult());
    
            // 2. Use the AsDataSet extension method
            var result = reader.AsDataSet();
    
            // The result of each spreadsheet is in result.Tables
        }
    }
    

    -示例来自: https://github.com/ExcelDataReader/ExcelDataReader

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      相关资源
      最近更新 更多