使用OpenXml 2.0向Excel文档加入自定义Ribbon
Ribbon.xml
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"> <ribbon> <tabs> <tab id="CustomTab" label="My Tab"> <group id="MyGroup" label="My Group" > <button id="Button1" label="My Large Button" size="large"/> <button id="Button2" label="My Normal Button" size="normal"/> </group > </tab> </tabs> </ribbon> </customUI>
Program:
Imports log4net Imports System.Windows.Forms Imports System.IO Imports DocumentFormat.OpenXml.Packaging Imports DocumentFormat.OpenXml.Office.CustomUI Module Module1 Private MyLog As ILog = log4net.LogManager.GetLogger(GetType(Module1)) Sub Main() Dim OFD As New OpenFileDialog Dim TargetFile As String Dim SourceFile As String Dim RibbonXml As String Dim RibbonExtend As RibbonExtensibilityPart OFD.Multiselect = False OFD.Title = "打开目标Excel文件" OFD.InitialDirectory = System.Environment _ .GetFolderPath(System.Environment.SpecialFolder.Desktop) OFD.Filter = "Excel文件|*.xlsx;*.xlsm" OFD.ShowDialog() TargetFile = OFD.FileName MyLog.Info("Target File : " + TargetFile) OFD.Title = "打开RibbonXml文件" OFD.Filter = "Ribbon Xml文件|*.xml" OFD.ShowDialog() SourceFile = OFD.FileName MyLog.Info("Source File : " + SourceFile) RibbonXml = File.OpenText(SourceFile).ReadToEnd() MyLog.Info("Ribbon Xml : " + RibbonXml) Using SSD As SpreadsheetDocument = SpreadsheetDocument _ .Open(TargetFile, True) RibbonExtend = SSD.GetPartsOfType(Of RibbonExtensibilityPart)() _ .FirstOrDefault() If RibbonExtend Is Nothing Then RibbonExtend = SSD.AddRibbonExtensibilityPart() Else RibbonExtend.CustomUI = New CustomUI(RibbonXml) RibbonExtend.CustomUI.Save() End If End Using Console.ReadKey() End Sub End Module
欢迎访问《许阳的红泥屋》
征诛志异,三让两家王朝;功同开辟,一桮万古江南。