Open XML SDK 2.5 for Office
Open XML is an open standard for word-processing documents, presentations, and spreadsheets that can be implemented freely by multiple applications on different platforms. The Open XML is designed to faithfully represent existing word-processing documents, presentations, and spreadsheets encoded in the binary format defined by Microsoft Office applications. The reason for using Open XML is simple: Hundreds of millions of documents exist now, but unfortunately, the information in these documents is tightly coupled to the program that created the document. The purpose of the Open XML standard is to detach documents created by Microsoft Office applications so that other applications can manipulate these documents independently of the proprietary format without losing data
1. Download and install :
https://www.microsoft.com/en-us/download/details.aspx?id=30425
1.openxmlsdkv25.msi The directory appears after the installation is complete DocumentFormat.OpenXml.dll
2.openxmlsdktoolv25.msi This is Openxmltool can open more than Office 2007 versions and generate the standard Office XML format and standard C # source code tools.
|
Openxmlsdkv25.msi 2.5 MB |
2.5 MB |
|
Openxmlsdktoolv25.msi 24.9 MB |
24.9 MB |
using classes in the Open XML SDK
2. Using classes in the Open XML SDK 2.5
The classes in Open XML SDK 2.5 are simple to use. After you install the Open XML SDK 2.5, open an existing project or application in Visual Studio, or create a new project or application. Then, in your project or application, add a reference to the following components.
Documentformat.openxml
WindowsBase
3. Case studies
Working with paragraphs (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));} }
Use continuous text (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));} }
Using the WordprocessingML table (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 = "", 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-Text ("1"))); TableCell tc2 = new TableCell (New Paragraph (New-Text ("2"))); TableCell TC3 = new TableCell (New Paragraph (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); }}
Reference:
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
. NET using Office Open XML SDK2.5