使用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

欢迎访问《 许阳的红泥屋

转载于:https://www.cnblogs.com/mksword/archive/2012/08/22/3934042.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用OpenXML SDK库生成Excel文件的效率比使用Interop.Excel更高,因为它是直接生成Office Open XML格式的文件,而不是通过Excel应用程序进行操作。 以下是使用OpenXML SDK库进行数据导出的示例代码: ```csharp using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using System; using System.Linq; public static void ExportToExcel(string filePath, string[,] data) { // Create a new Excel document using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook)) { // Add a new workbook WorkbookPart workbookPart = document.AddWorkbookPart(); workbookPart.Workbook = new Workbook(); // Add a new worksheet WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(new SheetData()); // Add a new sheet to the workbook Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" }; sheets.Append(sheet); // Write data to the worksheet SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); for (int i = 0; i < data.GetLength(0); i++) { Row row = new Row(); for (int j = 0; j < data.GetLength(1); j++) { Cell cell = new Cell(new InlineString(new Text(data[i, j].ToString()))); row.Append(cell); } sheetData.Append(row); } // Save the workbook workbookPart.Workbook.Save(); } } ``` 在此示例代码中,我们使用OpenXML SDK库创建了一个新的Excel文档,并向其中添加了一个名为“Sheet1”的工作表,然后将数据写入该工作表。 您可以根据需要对此代码进行修改,例如更改工作表的名称、添加样式和格式等,以满足您的具体需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值