我收到以下错误:
无法创建类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 Local,Object 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);
我想知道当D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS
OBJECT_DEFINITIONS.XLS
时,为什么找不到C:\
的错误?
答案 0 :(得分: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();
}
}
}
}
答案 1 :(得分:0)
不深入研究代码语法(我不是C#程序员),并根据我对Selenium的经验,尝试以下内容 -
确保OBJECT_DEFINITIONS.XLS就是那个而不是OBJECT_DEFINITIONS.XLSX。 我遇到了使用Excel 2007创建和保存的文件的Selenium兼容性问题。如果是,请将Excel文件另存为“Excel 97-2003工作簿”。