使用OpenXml向空白文档添加一个带表格线的表
using System;using System.Collections.Generic;using System.Linq;using System.Text;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessing;using com.mksword.Net.OpenXmlTools;namespace ConsoleApplication14{ class Class1 : WordprocessingDocumentUtil { public void Action() { if (CreateW14Document(CreateTable)) SetLog("OK", OXULogType.INFO); else SetLog("No", OXULogType.FATAL); } private bool CreateTable(WordprocessingDocument WPD) { bool result = false; Table tab = new Table(); TableProperties tbps = new TableProperties(); TableWidth tbwidth = new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }; TableBorders tbbs = new TableBorders(); TopBorder tpb = new TopBorder() { Val = BorderValues.Single }; BottomBorder tbb = new BottomBorder() { Val = BorderValues.Single }; RightBorder trb = new RightBorder() { Val = BorderValues.Single }; LeftBorder tlb = new LeftBorder() { Val = BorderValues.Single }; InsideHorizontalBorder tihb = new InsideHorizontalBorder() { Val = BorderValues.Single }; InsideVerticalBorder tivb = new InsideVerticalBorder() { Val = BorderValues.Single }; tbbs.Append(tpb); tbbs.Append(tbb); tbbs.Append(trb); tbbs.Append(tlb); tbbs.Append(tihb); tbbs.Append(tivb); tbps.Append(tbbs); tbps.Append(tbwidth); TableGrid TG = new TableGrid(); GridColumn GC1 = new GridColumn() { Width = "3192" }; GridColumn GC2 = new GridColumn() { Width = "3192" }; GridColumn GC3 = new GridColumn() { Width = "3192" }; TG.Append(GC1); TG.Append(GC2); TG.Append(GC3); TableRow TR1 = new TableRow(); TableCell TC1 = new TableCell(); AddTableCellProperty(TC1); Paragraph P1 = new Paragraph(); TC1.Append(P1); TableCell TC2 = new TableCell(); AddTableCellProperty(TC2); Paragraph P2 = new Paragraph(); TC2.Append(P2); TableCell TC3 = new TableCell(); AddTableCellProperty(TC3); Paragraph P3 = new Paragraph(); TC3.Append(P3); TR1.Append(TC1); TR1.Append(TC2); TR1.Append(TC3); tab.Append(tbps); tab.Append(TG); tab.Append(TR1); WPD.MainDocumentPart.Document.Body.Append(tab); result = true; return result; } private void AddTableCellProperty(TableCell TC) { TableCellProperties TCP = new TableCellProperties(); TableCellWidth TCW = new TableCellWidth() { Width = "3192", Type = TableWidthUnitValues.Dxa }; TCP.Append(TCW); TC.Append(TCP); } }}
欢迎访问《许阳的红泥屋》