使用 VSTO 创建外接程序,Gallery控件动态加载工作簿名称
代码如下:
加载工作簿名称:
private void Gallery1_ItemsLoading(object sender, RibbonControlEventArgs e)
{
app = Globals.ThisAddIn.Application;
gallery1.Items.Clear();
int n = ;
foreach (excel.Workbook workbook in app.Workbooks)
{
//RibbonGallery gallery = this.Factory.CreateRibbonGallery();
RibbonDropDownItem downItem = this.Factory.CreateRibbonDropDownItem();
downItem.Tag = workbook.Name + n.ToString();
gallery1.Items.Add(downItem);
downItem.Label = workbook.Name;
downItem.Image = Properties.Resources.Excel_32px_1185985_easyicon_net;
n++;
}
}
选择子项目激活工作簿:
private void Gallery1_Click(object sender, RibbonControlEventArgs e)
{ string tg = gallery1.SelectedItem.Label;
//MessageBox.Show(tg);
app = Globals.ThisAddIn.Application;
excel.Workbook workbook = app.Workbooks[tg];
workbook.Activate();
}
使用 VSTO 创建外接程序,dropDown控件动态加载工作表名称
代码如下:
加载工作表名称:
private void DropDown1_ItemsLoading(object sender, RibbonControlEventArgs e)
{
try
{ app = Globals.ThisAddIn.Application;
int n = ;
dropDown1.Items.Clear();
foreach (excel.Worksheet worksheet in app.Worksheets)
{
RibbonDropDownItem downItem = this.Factory.CreateRibbonDropDownItem();
downItem.Tag = worksheet.Name + n.ToString();
dropDown1.Items.Add(downItem);
downItem.Label = worksheet.Name;
downItem.Image = Properties.Resources.office_excel_24px_1116424_easyicon_net;
n++;
}
if (app.ActiveWorkbook.Path != "")
{
RibbonDropDownItem downItem1 = this.Factory.CreateRibbonDropDownItem();
downItem1.Label = "打开工作簿所在文件夹";
dropDown1.Items.Add(downItem1);
downItem1.Image = Properties.Resources.folder_32px_1229424_easyicon_net;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
选择子项目激活工作表:
private void DropDown1_SelectionChanged(object sender, RibbonControlEventArgs e)
{
try
{
string tg = dropDown1.SelectedItem.Label;
//MessageBox.Show(tg);
app = Globals.ThisAddIn.Application;
if (tg == "打开工作簿所在文件夹")
{
string FilePath = app.ActiveWorkbook.Path;
string FileName = app.ActiveWorkbook.Name;
//System.Diagnostics.Process.Start(FilePath);
System.Diagnostics.Process.Start("Explorer.exe", "/select," + FilePath + "\\" + FileName);
}
else
{
excel.Worksheet worksheet = app.ActiveWorkbook.Worksheets[tg];
worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
worksheet.Protect(DrawingObjects: MsoTriState.msoTrue, Contents: MsoTriState.msoTrue, Scenarios: MsoTriState.msoTrue, AllowFiltering: MsoTriState.msoTrue, AllowUsingPivotTables: MsoTriState.msoTrue);
worksheet.Unprotect();
worksheet.Visible = excel.XlSheetVisibility.xlSheetVisible;
worksheet.Activate();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}