在从EPPlus下载的示例Sample15.cs
中,我看到EPPlus可以编写/注入VBA宏。但是,我没有看到如何从.xlsm
工作簿中读取所有VBA宏。
EPPlus可以解释VbaProject.bin
并支持此功能吗?
Open XML SDK
也不能这样做(如果我错了请纠正我),这就是我考虑EPPlus的原因......
答案 0 :(得分:0)
如果您只想阅读每个模块中的代码,您只需要通过'Workbook.VbaProject.Modules'集合,如下所示:
var fi = new FileInfo(@"C:\temp\Book1.xlsm");
using (var pck = new ExcelPackage(fi))
{
var modules = pck.Workbook.VbaProject.Modules;
foreach (var module in modules)
{
Console.WriteLine($"Module Name: {module.Name}{Environment.NewLine}Code:");
Console.Write(module.Code);
Console.WriteLine("---------");
}
这会在输出中给你这个(我刚创建了一个带有两个模块的excel xlsm):
Module Name: ThisWorkbook
Code:
---------
Module Name: Sheet1
Code:
---------
Module Name: Module1
Code:
Public Sub proc1()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws As Worksheet
Set ws = wb.ActiveSheet
ws.Cells(1, 1).Value = "proc1"
End Sub
---------
Module Name: Module2
Code:
Public Sub proc2()
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim ws As Worksheet
Set ws = wb.ActiveSheet
ws.Cells(2, 1).Value = "proc2"
End Sub
---------