使用OpenXml 2.0向Excel文档加入自定义Ribbon
Ribbon.xml
Imports log4netImports System.Windows.FormsImports System.IOImports DocumentFormat.OpenXml.PackagingImports DocumentFormat.OpenXml.Office.CustomUIModule 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 SubEnd Module欢迎访问《许阳的红泥屋》