使用OpenXml向空白文档添加一个带表格线的表

63 篇文章 0 订阅
26 篇文章 0 订阅
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);
        }

        
    }
}

欢迎访问《许阳的红泥屋

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值