我得到以下错误:
无法创建类TestProject.TestClass的实例。错误:无法找到'D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS‘:System.Runtime.InteropServices.COMException。检查文件名的拼写,并验证文件位置是否正确。如果您试图从最近使用的文件列表中打开该文件,请确保该文件没有被重命名、移动或删除。
错误堆栈跟踪:
Microsoft.Office.Interop.Excel.Workbooks.Open(String文件名、对象UpdateLinks、对象ReadOnly、对象格式、对象密码、对象WriteResPassword、对象IgnoreReadOnlyRecommended、对象起源、对象划界器、对象可编辑、对象通知、对象转换器、对象AddToMru、对象本地、对象CorruptLoad)
TestProject.TestLibrary.GetObjectDeclarations(String sModule)在C:\Documents和Settings\Administrator\My \Visual 2010\Projects\TestProject\TestLibrary.cs:第133行
TestProject.TestClass..ctor()在C:\Documents和Settings\Administrator\My \Visual 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);我想知道为什么当我将D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS存储在C:\中时,说找不到OBJECT_DEFINITIONS.XLS是错误的
发布于 2011-06-06 15:41:37
当然,您需要进入到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();
}
}
}
}发布于 2011-06-06 15:38:34
不深入研究您的代码语法(我不是C#程序员),并且根据我对Selenium的经验,尝试如下-
确保OBJECT_DEFINITIONS.XLS只是这样,而不是OBJECT_DEFINITIONS.XLSX。我遇到了与使用Excel2007创建和保存的文件的硒兼容性问题。如果是,请将excel文件保存为“Excel97-2003工作簿”。
https://stackoverflow.com/questions/6254381
复制相似问题