using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApp9
{
internal class Program
{
static void Main(string[] args)
{
// 创建 Excel 应用程序实例
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
try
{
// 打开包含宏的 Excel 文件
Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\Users\Administrator\Documents\test.xlsm");
try
{
// 运行指定的宏
excelApp.Run("'test.xlsm'!test1","test");
}
catch (Exception ex)
{
Console.WriteLine("Error running macro: " + ex.Message);
}
finally
{
Console.ReadKey();
// 保存工作簿并关闭
workbook.Save();
workbook.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Error opening workbook: " + ex.Message);
}
finally
{
// 退出 Excel 应用程序
excelApp.Quit();
// 释放 COM 对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}
Console.ReadKey();
}
}
}
我已经很久没登录了,我还是最喜欢在哔哩哔哩发视频
【老虎會游泳的个人空间-哔哩哔哩】 https://b23.tv/x5GE5qd