我正在尝试在Excel打开之前定义列宽。因此,在执行任何操作之前,我希望具有所需的列宽。我正在将Visual Studio Tools for Office与ThisAddIn类(功能区)一起使用。在下面,我提供了要在其中实现此功能的类。有很多答案与我的问题相似,但不完全相同。
namespace CreateLayout
{
using System;
using Excel = Microsoft.Office.Interop.Excel;
public partial class ThisAddIn
{
public void ApplicationWorkbookBeforeSave(Excel.Workbook wb, bool saveAsUI, ref bool cancel)
{
Excel.Worksheet activeWorksheet = (Excel.Worksheet)this.Application.ActiveSheet;
}
private void ThisAddIn_Startup(object sender, EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(this.ApplicationWorkbookBeforeSave); // used to connect Application_WorkbookBeforeSave event
KeyboardHooking.SetHook();
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
KeyboardHooking.ReleaseHook();
}
#region VSTO generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}