通过VB.NET,如何使用Microsoft.Office.Interop.Excel添加和编辑Excel文件的代码模块?

问题描述 投票:0回答:1

我正在更新用VB.Net编写的公司软件。我们的数据存储在MYSQL中。我试图更新的功能之一是创建报告的能力。现在这些报告将由软件生成为 Excel 工作簿。经过一些困难,我已经完成了可以创建 Excel 工作簿和插入数据,甚至通过 VB.Net Microsoft.Office.Interop.Excel 使用一些 Excel 功能的部分。但是,我希望能够直接通过互操作创建宏/硬编码,这将使这些报告更加有益。我希望能够使用工作表级模块和通用代码模块。我尝试过使用语法,但我认为我还没有接近。我似乎找不到有关该主题的任何说明。谢谢您的帮助。

        Dim adapter As New MySqlDataAdapter("CALL reports_productionschedule();", connection)
        Dim table As New DataTable
        connection.Open()
        adapter.Fill(table)
        connection.Close()


        Dim app As New Microsoft.Office.Interop.Excel.Application
        Dim wb As Microsoft.Office.Interop.Excel.Workbook = app.Workbooks.Add()

        Call DataTableToExcel(table, wb, "ProductionSchedule")'sub I built to print out Headers 
        'and Data from a datatable to an excel file

        app.Visible = True

        Dim ws As Microsoft.Office.Interop.Excel.Worksheet

        wb.Activate()
        ws = wb.Worksheets(1)
        ws.Activate()

        ws.Range("A1:ZZ100000").Columns.AutoFit()

        ws.Range("A1:ZZ100000").AutoFilter2(Field:=1)

        app.ActiveWindow.SplitColumn = 0
        app.ActiveWindow.SplitRow = 1
        app.ActiveWindow.FreezePanes = True
        
        'everything works to this point. here are things I have tried without success

        Dim strcode As String
        strcode = "Private Sub Worksheet_Change(ByVal Target As Range)" & vbCrLf & "End Sub"
        Dim nmod As Microsoft.Office.Interop.Excel.Module = wb.Modules.Add(strcode)

        app.Modules.Add(strcode)

        'I know you can programmatically write code in another workbook through excel, I figure 
        'there has to be a way in VB.Net

        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim linenum As Long
        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents(2)
        Set CodeMod = VBComp.CodeModule
    
        With CodeMod

        linenum = .CountOfLines + 1
        .InsertLines linenum, "Private Sub Worksheet_Change(ByVal Target As Range)"


        'But none of these key words comes up in VB.Net intellisense
excel vb.net module office-interop
1个回答
0
投票

最简单的方法是使用EPPlus。您可以将 VBA 代码写入 Excel 文件。

EPPlus 支持创建、读取和写入 VBA。不支持 VBA 代码的执行/解释。请记住,该包必须以扩展名 xlsm 保存。 VBA 项目是通过 ExcelWorkbook 类的 CreateVBAProject() 方法创建的。这使您能够将 VBA 代码写入项目...

链接页面提供了一个 C# 示例,应该很容易转换为 VB.NET:

private static void VBASample1(DirectoryInfo outputDir)
{
   ExcelPackage pck = new ExcelPackage();
   //Add a worksheet.
   var ws=pck.Workbook.Worksheets.Add("VBA Sample");
   ws.Drawings.AddShape("VBASampleRect", eShapeStyle.RoundRect);            
   //Create a vba project             
   pck.Workbook.CreateVBAProject();
   //Now add some code to update the text of the shape...
   var sb = new StringBuilder();
   sb.AppendLine("Private Sub Workbook_Open()");
   sb.AppendLine("    [VBA Sample].Shapes(\"VBASampleRect\").TextEffect.Text = \"This text is set from VBA!\"");
   sb.AppendLine("End Sub");
   pck.Workbook.CodeModule.Code = sb.ToString();            

   //And Save as xlsm
   pck.SaveAs(new FileInfo(outputDir.FullName + @"\sample15-1.xlsm"));
}
© www.soinside.com 2019 - 2024. All rights reserved.