如何打开在C#中的Excel文件?文件、Excel

2023-09-02 01:40:02 作者:说谎的目击者

我想将一些 VBA code到C#。我是新来的C#。目前,我试图打开一个文件夹中的Excel文件,如果不存在则创建它。我想类似于下面的东西。我怎样才能使它工作?

  Excel.Application objexcel;
Excel.Workbook wbexcel;
布尔wbexists;
Excel.Worksheet objsht;
Excel.Range objrange;

objexcel =新Excel.Application();
如果(目录(C:\ \ CSHARP错误report1.xls)=)
{
    wbexcel.NewSheet();
}

其他
{
    wbexcel.Open(C:\ \ CSHARP错误report1.xls);
    objsht =(工作表Sheet1);
}
objsht.Activate();
 

解决方案

您需要为办公室安装了Microsoft Visual Studio工具。

excel中打开之后,出现文档恢复,怎么样可以让它不在提示

这是建立共同的.NET项目, Microsoft.Office.Interop.Excel.dll 通过引用添加到COM对象添加引用...对话框后。

 应用程序的Excel =新的应用程序();
工作簿WB = excel.Workbooks.Open(路径);
 

Missing.Value 是一个特殊的反射结构的不必要的参数替换

I am trying to convert some VBA code to C#. I am new to C#. Currently I am trying to open an Excel file from a folder and if it does not exist then create it. I am trying something like the following. How can I make it work?

Excel.Application objexcel;
Excel.Workbook wbexcel;
bool wbexists;
Excel.Worksheet objsht;
Excel.Range objrange;

objexcel = new Excel.Application();
if (Directory("C:\csharp\error report1.xls") = "")
{
    wbexcel.NewSheet();
}

else
{
    wbexcel.Open("C:\csharp\error report1.xls");
    objsht = ("sheet1");
}
objsht.Activate();

解决方案

You need to have installed Microsoft Visual Studio Tools for Office.

After that create common .NET project and add the reference to COM object Microsoft.Office.Interop.Excel.dll via 'Add Reference..' dialog.

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);

Missing.Value is a special reflection struct for unnecessary parameters replacement