如何在C#中打开Excel文件? [英] How to open an Excel file in C#?
问题描述
我正在尝试将一些 VBA 代码转换为C#。我是C#的新手。目前,我正在尝试从文件夹打开一个Excel文件,如果不存在,则创建它。我正在尝试像下面这样的东西。如何使其工作?
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();
推荐答案
您需要安装Microsoft Visual Studio Tools for Office 。
You need to have installed Microsoft Visual Studio Tools for Office.
之后创建常见的.NET项目并添加对COM对象的引用 Microsoft.Office.Interop.Excel.dll
通过'添加参考..'对话框。
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
是一个特殊的反射结构对于不必要的参数替换
Missing.Value
is a special reflection struct for unnecessary parameters replacement
这篇关于如何在C#中打开Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!