using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelAddIn1
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Excel.Application ExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
_ = ExcelApp.Workbooks.Open($"{AppDomain.CurrentDomain.BaseDirectory}template\\模板.xlsx");
}
}
}
上面代码是写的打开模板文件的代码,其他的不会了。查了好久,只找到把新建表所有内容都写在代码里的资料,这样工作量大,修改难度也大。
假设模板.xlsx文件放在程序目录下的template文件夹里,这个工作簿有两个工作表,名字分别是“苹果表”和“桔子表”。我希望点击VSTO插件上的按钮可以在指定路径模板工作簿里的“苹果表”复制在当前工作簿,或者说在当前工作簿中新建一个和“苹果表“一模一样的表格。
去官方文档参考:https://learn.microsoft.com/zh-cn/visualstudio/vsto/excel-solutions?view=vs-2022