.NET 使用 Office Open XML SDK2.5

本文介绍OpenXML SDK 2.5的基本概念及其安装步骤,并通过具体案例展示了如何利用该SDK处理Word文档,包括添加文本、设置格式及插入表格等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Open XML SDK 2.5 for Office

Open XML 是可由不同平台上的多个应用程序自由实现的字处理文档、演示文稿和电子表格的开放式标准。Open XML 旨在如实表示用 Microsoft Office 应用程序定义的二进制格式进行编码的现有字处理文档、演示文稿和电子表格。使用 Open XML 的原因很简单:现在存在数以亿计的文档,但遗憾的是,这些文档中的信息与创建文档的程序紧密耦合。Open XML 标准的目的是分离由 Microsoft Office 应用程序创建的文档,以便其他应用程序可以独立于专有格式操作这些文档且不会丢失数据

1.下载安装

https://www.microsoft.com/en-us/download/details.aspx?id=30425

1.OpenXMLSDKV25.msi  安装完成后目录出现    DocumentFormat.OpenXml.dll

2.OpenXMLSDKToolV25.msi 这是OpenXMLTool可以打开Office 2007以上的版本并且生成标准Office XML格式和标准c#源代码工具。

OpenXMLSDKV25.msi

2.5 MB

2.5 MB

OpenXMLSDKToolV25.msi

24.9 MB

24.9 MB

 使用 Open XML SDK 中的类              


              
2.使用 Open XML SDK 2.5 中的类     

Open XML SDK 2.5 中的类使用起来很简单。在安装了 Open XML SDK 2.5 之后,请在 Visual Studio 中打开现有的项目或应用程序,或者创建新的项目或应用程序。然后在您的项目或应用程序中,添加对以下组件的引用。

  • DocumentFormat.OpenXml                

  • WindowsBase

3.案例

 处理段落 (Open XML SDK)

public static void WriteToWordDoc(string filepath, string txt)
{
    // Open a WordprocessingDocument for editing using the filepath.
    using (WordprocessingDocument wordprocessingDocument =
         WordprocessingDocument.Open(filepath, true))
    {
        // Assign a reference to the existing document body.
        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        // Add a paragraph with some text.
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text(txt));
    }
}

 使用连续文本 (Open XML SDK)

public static void WriteToWordDoc(string filepath, string txt)
{
    // Open a WordprocessingDocument for editing using the filepath.
    using (WordprocessingDocument wordprocessingDocument =
         WordprocessingDocument.Open(filepath, true))
    {
        // Assign a reference to the existing document body.
        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        // Add new text.
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());

        // Apply bold formatting to the run.
        RunProperties runProperties = run.AppendChild(new RunProperties(new Bold()));   
        run.AppendChild(new Text(txt));                
    }
}

 使用 WordprocessingML 表 (Open XML SDK)

public static void InsertTableInDoc(string filepath)
{
    // Open a WordprocessingDocument for editing using the filepath.
    using (WordprocessingDocument wordprocessingDocument =
         WordprocessingDocument.Open(filepath, true))
    {
        // Assign a reference to the existing document body.
        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        // Create a table.
        Table tbl = new Table();

        // Set the style and width for the table.
        TableProperties tableProp = new TableProperties();
        TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };

        // Make the table width 100% of the page width.
        TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
        
        // Apply
        tableProp.Append(tableStyle, tableWidth);
        tbl.AppendChild(tableProp);

        // Add 3 columns to the table.
        TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
        tbl.AppendChild(tg);
        
        // Create 1 row to the table.
        TableRow tr1 = new TableRow();

        // Add a cell to each column in the row.
        TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("1"))));
        TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("2"))));
        TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("3"))));
        tr1.Append(tc1, tc2, tc3);
        
        // Add row to the table.
        tbl.AppendChild(tr1);

        // Add the table to the document
        body.AppendChild(tbl);
    }
}

 

 

参考:

https://msdn.microsoft.com/zh-cn/library/office/bb448854.aspx

https://msdn.microsoft.com/ZH-CN/library/office/gg278313.aspx

https://www.microsoft.com/en-us/download/details.aspx?id=30425

 

转载于:https://www.cnblogs.com/asjiang/p/4441777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值