c# - C# 运行带有参数的 excel 宏
问题描述
我正在尝试以编程方式打开一个 excel 工作簿并运行一个宏,该宏接受在命令行中键入的参数。到目前为止,我能够打开工作簿并执行宏,但在传递参数时遇到问题。
我目前的代码:
public void runTemplate(string templateName, string sourceFile, string destinationFile, string ITPath, string date)
{
string template = templateName + "!DoTheImport";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.DisplayAlerts = false;
object misValue = System.Reflection.Missing.Value;
ExcelApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);
RunMacro(ExcelApp, new Object[] { template });
ExcelWorkBook.SaveCopyAs(destinationFile);
ExcelWorkBook.SaveCopyAs(ITPath);
ExcelWorkBook.Close(false, misValue, misValue);
ExcelApp.Quit();
if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}
private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, oApp, oRunArgs);
}
我的宏看起来像:
Sub DoTheImport(sDate As String)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;\\filePath\DecisionsByRegion-" + sDate + ".txt",
Destination:=Range("$A$2") _)
.Name = "test"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Columns("A:G").EntireColumn.AutoFit
Columns("H").EntireColumn.Delete
End Sub
正如我所说,这适用于执行宏(sDate 最初是 Now() 格式化为 sub 中的字符串,而不是如图所示传入),但我试图将 'date' 变量传递给 runTemplate 并传递将它作为 sDate 放入我的宏中。我尝试将其简单地添加到参数对象中RunMacro(ExcelApp, new Object[] { template, date });
,但这引发了错误。
解决方案
虽然我无法使用现有方法传递多个变量,但我找到了一种执行宏的替代方法,它允许我根据需要传递参数。
public void runTemplate(string templateName, string sourceFile, string destinationFile, string ITPath, string date)
{
string sDate = date;
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.DisplayAlerts = false;
object misValue = System.Reflection.Missing.Value;
ExcelApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);
ExcelApp.Run("DoTheImport", sDate);
ExcelWorkBook.SaveCopyAs(destinationFile);
ExcelWorkBook.SaveCopyAs(ITPath);
ExcelWorkBook.Close(false, misValue, misValue);
ExcelApp.Quit();
if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}
我已经删除了 RunMacro 方法并仅用于ExcelApp.Run("DoTheImport", sDate);
执行宏。该方法允许我将参数传递到宏中,可以通过添加'ByVal'参数传递机制在宏中访问:
Sub DoTheImport(ByVal sDate As String)
推荐阅读
- excel - 打开 Excel 时内存泄漏 - 导致崩溃
- reactjs - 更新 Child 中的状态
- blockchain - 为什么我们不在公共区块链中使用伊斯坦布尔拜占庭容错(IBFT)?
- android - 如何在 Android 中更改状态栏字体颜色?
- javascript - 如何在同一个 Razor 页面中使用相同视图组件和 Javascript 文件的多个实例?
- video - 将 VFR 视频拆分为 CFR 块?
- mule - 在 Dataweave 2.0 中将属性从属性文件转换为 json
- go - 如何从2个具有相同名称的文本区域中获取值,并将其保存在切片中
- python - 如何同时等待多个条件,直到其中任何一个返回 true
- woocommerce - Woocommerce中如何拆分订单总量