【发布时间】:2016-03-25 14:09:57
【问题描述】:
我正在尝试从我的计算机上的目录中打开一个预先存在的 excel 文件。但是,当我的代码到达 Workbooks.Open 方法时,我收到了 COMexception。我不确定我做错了什么。任何帮助表示赞赏。
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;
namespace Excel_Create
{
class Program
{
static void Main(string[] args)
{
string mySheet = @"C:\Users\Danny\Documents\Visual Studio 2013\Projects\MWS\MWS\bin\Debug\csharp-Excel.xls";
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
if (xlApp == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(mySheet,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false );
}
}
}
以下是异常的含义:Excel_Create.exe 中发生了“System.Runtime.InteropServices.COMException”类型的未处理异常
其他信息:找不到“C:\Users\Danny\Documents\Visual Studio 2013\Projects\MWS\MWS\bin\Debug\csharp-Excel.xls”。检查文件名的拼写,并验证文件位置是否正确。
【问题讨论】:
-
如果文件在那里,它是否在另一个应用程序(例如 Excel)中打开?
-
是的,它就在那里。这是一个 Microsoft Excel 97-2003 工作表 (.xls),保存在:C:\Users\Danny\Documents\Visual Studio 2013\Projects\MWS\MWS\bin\Debug\csharp-Excel.xls 从属性视图复制/粘贴文件。
标签: c# excel automation excel-interop