【问题标题】:System.Runtime.InteropServices.COMExceptionSystem.Runtime.InteropServices.COMException
【发布时间】:2011-09-09 09:49:11
【问题描述】:

我收到以下错误:

无法创建 TestProject.TestClass 类的实例。错误:System.Runtime.InteropServices.COMException:找不到“D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS”。检查文件名的拼写,并验证文件位置是否正确。如果您尝试从最近使用的文件列表中打开该文件,请确保该文件未被重命名、移动或删除。

错误堆栈跟踪:

Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter , 对象 AddToMru, 对象本地, 对象 CorruptLoad)
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestLibrary.cs 中的 TestProject.TestLibrary.GetObjectDeclarations(String sModule):第 133 行
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestClass.cs 中的 TestProject.TestClass..ctor():第 51 行

代码:

using Excel = Microsoft.Office.Interop.Excel;

namespace TestProject
{
[TestClass]
public class TestLibrary
{
    public string[] arrObj = new string[19];
    public string[] arrConfig = new string[12];
    public string sobjfile;
    .
    .
    .
    xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(sobjfile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

我想知道当我将OBJECT_DEFINITIONS.XLS 存储在C:\ 中时,为什么找不到D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS 的错误提示

【问题讨论】:

  • 在哪里可以看到您的这个 excel 文件的路径?
  • 我不确定。我所看到的只是未分配任何路径的变量 sobjfile。我也试过 public string sobjfile = 'C:\\OBJECT_DEFINITIONS.XLS"; 但得到同样的错误

标签: c# excel comexception vba


【解决方案1】:

当然,您需要指定public string sobjfile 的路径。应用程序不知道在哪里搜索您的文件。

编辑:

using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}

【讨论】:

  • 是的,我是这么认为的,但我正在查看其他人的代码。我不知道为什么我之前分配了 sobjfile 的路径后仍然收到错误。而且,现在它正在工作。非常感谢!
【解决方案2】:

无需深入研究您的代码语法(我不是 C# 程序员),根据我对 Selenium 的经验,尝试以下方法 -

确保 OBJECT_DEFINITIONS.XLS 就是那个,而不是 OBJECT_DEFINITIONS.XLSX。 我在使用 Excel 2007 创建和保存的文件时遇到了 Selenium 兼容性问题。如果是,请将 Excel 文件另存为“Excel 97-2003 工作簿”。

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2011-05-15
    • 2021-09-07
    相关资源
    最近更新 更多