![](/img/trans.png)
[英]'System.Runtime.InteropServices.COMException'
[英]System.Runtime.InteropServices.COMException
我收到以下錯誤:
無法創建 class 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, Object AddToMru , Object 本地, Object CorruptLoad)
C 中的 TestProject.TestLibrary.GetObjectDeclarations(String sModule):\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestLibrary.cs:第 133 行
C 中的 TestProject.TestClass..ctor():\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestClass.cs:第 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
存儲在D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS
C:\
當然,您需要指定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();
}
}
}
}
無需深入研究您的代碼語法(我不是 C# 程序員),根據我對 Selenium 的經驗,請嘗試以下操作 -
確保 OBJECT_DEFINITIONS.XLS 就是這樣,而不是 OBJECT_DEFINITIONS.XLSX。 我遇到了 Selenium 與使用 Excel 2007 創建和保存的文件的兼容性問題。如果是,請將 ZBF57C906FA7D2BB66D07372E41585D906Z 文件保存為“Excel 97-2”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.