C#Winform 新建excel

1. 启动 Microsoft Visual Studio .NET。
2. 文件菜单上,单击新建,然后单击项目。从 Visual C# 项目类型中选择 Windows 应用程序。默认情况下会创建 Form1。
3. 添加对 Microsoft Excel 对象库Microsoft Visual Basic for Applications 扩展库的引用。为此,请按照下列步骤操作:
a. 项目菜单上,单击添加引用
b. COM 选项卡上,找到 Microsoft Excel 对象库,然后单击选择。然后找到 Microsoft Visual Basic for Applications 扩展库并单击选择

注意:Microsoft Office 2003 包含主 Interop 程序集 (PIA)。Microsoft Office XP 不包含 PIA,但您可以下载 PIA。 有关 Office XP PIA 的其他信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
328912 (http://support.microsoft.com/kb/328912/) INFO: Microsoft Office XP PIA 可供下载
c. 添加引用对话框中单击确定以接受您的选择。
4. 视图菜单上,选择工具箱以显示工具箱,然后向 Form1 添加一个按钮。
5. 双击 Button1。代码窗口打开并显示 Button1Click 事件。将以下代码行添加到代码窗口顶部的 using 语句中:
using Office = Microsoft.Office.Core;

            using VBIDE = Microsoft.Vbe.Interop;

            using Excel = Microsoft.Office.Interop.Excel;

            
6. 在代码窗口中,将以下代码
private void button1_Click(object sender, System.EventArgs e)

            {

            }

            
替换为:
private void button1_Click(object sender, System.EventArgs e)

            {

            Excel.Application oExcel;

            Excel.Workbook oBook;

            VBIDE.VBComponent oModule;

            Office.CommandBar oCommandBar;

            Office.CommandBarButton oCommandBarButton;

            String sCode;

            Object oMissing = System.Reflection.Missing.Value;

            // Create an instance of Excel.

            oExcel = new Excel.Application();

            // Add a workbook.

            oBook = oExcel.Workbooks.Add(oMissing);

            // Create a new VBA code module.

            oModule = oBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

            sCode =

            "sub VBAMacro()\r\n" +

            "   msgbox \"VBA Macro called\"\r\n" +

            "end sub";

            // Add the VBA macro to the new code module.

            oModule.CodeModule.AddFromString(sCode);

            try

            {

            // Create a new toolbar and show it to the user.

            oCommandBar = oExcel.CommandBars.Add("VBAMacroCommandBar",oMissing, oMissing,\);

            oCommandBar.Visible = true;

            // Create a new button on the toolbar.

            oCommandBarButton = (Office.CommandBarButton) oCommandBar.Controls.Add(

            Office.MsoControlType.msoControlButton,

            oMissing, oMissing, oMissing, oMissing);

            // Assign a macro to the button.

            oCommandBarButton.OnAction = "VBAMacro";

            // Set the caption of the button.

            oCommandBarButton.Caption = "Call VBAMacro";

            // Set the icon on the button to a picture.

            oCommandBarButton.FaceId = 2151;

            }

            catch(Exception eCBError) {

            MessageBox.Show("VBAMacroCommandBar already exists.","Error");

            }

            // Make Excel visible to the user.

            oExcel.Visible = true;

            // Set the UserControl property so Excel won't shut down.

            oExcel.UserControl = true;

            // Release the variables.

            oCommandBarButton = null;

            oCommandBar = null;

            oModule = null;

            oBook = null;

            oExcel = null;

            // Collect garbage.

            GC.Collect();

            }

            
7. 按 F5 键生成并运行该程序。
8. 单击 Button1 启动 Microsoft Excel,插入 Visual Basic for Applications (VBA) 代码,然后添加一个新的 CommandBar。单击 CommandBar 上的按钮以运行 VBA 宏。

此文章为转载,如出现

关于“ 不信任到Visual Basic Project 的程序连接”问题

解决方法如下:

打开Excel-》工具-》宏-》安全性-》可靠发行商,选中“信任对于Visiual Basic 项目的访问”,按确定即可。

你可能感兴趣的:(WinForm)