资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用OpenXml怎么合并Table单元格

今天就跟大家聊聊有关使用OpenXml怎么合并Table单元格,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都服务器托管,成都创新互联公司提供包括服务器租用、四川电信机房托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、域名注册等业务的一体化完整服务。电话咨询:18980820575

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using OpenXML.Model;
using System;
using System.Collections.Generic;

namespace OpenXML
{
  class Program
  {
    //表格数据
    public static List> _tabData;

    public Program(List> tabData) {
      _tabData = tabData;
    }

    static void Main(string[] args)
    {
      List dataTitle = new List() { "序号","姓名","性别"};
      List data1 = new List() { "1", "张三", "男" };
      List data2 = new List() { "2", "王五", "男" };
      List data3 = new List() { "3", "李四", "女" };

      _tabData = new List>();
      _tabData.Add(dataTitle);
      _tabData.Add(data1);
      _tabData.Add(data2);
      _tabData.Add(data3);
      CreateTable(_tabData, @"C:\Users\dzw159\Desktop\WT\VS\OpenXMLFile\openXMLTest.docx",300);

      //CreateOpenXMLFile(@"C:\Users\dzw159\Desktop\WT\VS\OpenXMLFile\openXMLTest.docx");
      Console.WriteLine("Hello World!");
      Console.Read();
    }

    /// 
    /// 创建Word
    /// 
    /// 
    public static void CreateOpenXMLFile(string filePath)
    {
      using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
      {
        MainDocumentPart objMainDocumentPart = objWordDocument.AddMainDocumentPart();
        objMainDocumentPart.Document = new Document(new Body());
        Body objBody = objMainDocumentPart.Document.Body;
        //创建一些需要用到的样式,如标题3,标题4,在OpenXml里面,这些样式都要自己来创建的  
        //ReportExport.CreateParagraphStyle(objWordDocument); 
        SectionProperties sectionProperties = new SectionProperties();
        PageSize pageSize = new PageSize();
        PageMargin pageMargin = new PageMargin();
        Columns columns = new Columns() { Space = "220" };//720 
        DocGrid docGrid = new DocGrid() { LinePitch = 100 };//360 
                                  //创建页面的大小,页距,页面方向一些基本的设置,如A4,B4,Letter,  
                                  //GetPageSetting(PageSize,PageMargin); 

        //在这里填充各个Paragraph,与Table,页面上第一级元素就是段落,表格. 
        objBody.Append(new Paragraph());
        objBody.Append(new Table());
        objBody.Append(new Paragraph());

        //我会告诉你这里的顺序很重要吗?下面才是把上面那些设置放到Word里去.(大家可以试试把这下面的代码放上面,会不会出现打开openxml文件有误,因为内容有误) 
        sectionProperties.Append(pageSize, pageMargin, columns, docGrid);
        objBody.Append(sectionProperties);

        //如果有页眉,在这里添加页眉. 
        //if (IsAddHead)
        //{
          //添加页面,如果有图片,这个图片和上面添加在objBody方式有点不一样,这里搞了好久. 
          //ReportExport.AddHeader(objMainDocumentPart, image); 
        //}
        objMainDocumentPart.Document.Save();
      }
    }

    /// 
    /// 创建Tab
    /// 
    /// 
    /// 
    /// 
    public static void CreateTable(List> tabData, string filePath,int width)
    {
      //打开Word文件
      using (WordprocessingDocument d = WordprocessingDocument.Open(filePath,true))
      {
        //声明表格
        Table tab = new Table();
        //表格样式
        TableProperties tblProp = new TableProperties(new TableBorders(
          new TableBorders(
            new TopBorder() { Val = new EnumValue(BorderValues.Single),Size = 4},
             new BottomBorder() { Val = new EnumValue(BorderValues.Single), Size = 4 },
            new LeftBorder() { Val = new EnumValue(BorderValues.Single), Size = 4 },
            new RightBorder() { Val = new EnumValue(BorderValues.Single), Size = 4 },
            new InsideHorizontalBorder() { Val = new EnumValue(BorderValues.Single), Size = 4 },
            new InsideVerticalBorder() { Val = new EnumValue(BorderValues.Single), Size = 4 }
          )
        ));

        //设置表格宽度
        tblProp.TableWidth = new TableWidth() { Width = width.ToString(), Type = TableWidthUnitValues.Dxa };
        //样式添加
        tab.Append(tblProp);

        int j = 0;
        //循环生成单元格
        foreach (var item in tabData)
        {
          //声明Tab行
          TableRow row = new TableRow();
          for (var i = 0; i < item.Count; i++)
          {
            //申明单元格
            TableCell cell = new TableCell();

            TableCellProperties tableCellProperties = new TableCellProperties();
            //单元格样式设置
            TableCellMargin margin = new TableCellMargin();
            margin.LeftMargin = new LeftMargin() {
              Width="100",
              Type = TableWidthUnitValues.Dxa
            };
            margin.RightMargin = new RightMargin()
            {
              Width = "100",
              Type = TableWidthUnitValues.Dxa
            };
            tableCellProperties.Append(margin);

            Paragraph par = new Paragraph();
            Run run = new Run();


            //如果同一列的参数相同(合并单元格)
            if (j < (tabData.Count - 1) && item[i] == tabData[j + 1][i])
            {
              VerticalMerge verticalMerge = new VerticalMerge() {
                Val = MergedCellValues.Restart
              };
              //RunProperties rpr = new RunProperties();
              //rpr.Append(new Bold());
              //run.Append(rpr);
              //verticalMerge.Val = MergedCellValues.Restart;
              //Text t = new Text(item[i]);
              //t.Space = new EnumValue(SpaceProcessingModeValues.Preserve);

              //run.Append(t);

              TableCellProperties tableCellProperties2 = new TableCellProperties();
              tableCellProperties2.Append(verticalMerge);
              cell.Append(tableCellProperties2);
              Text t = new Text(item[i]);
              t.Space = new EnumValue(SpaceProcessingModeValues.Preserve);
              run.Append(t);
            }
            //和上一行比较(合并单元格)
            else if (j>0 && j < (tabData.Count) && item[i] == tabData[j -1][i])
            {

              VerticalMerge verticalMerge = new VerticalMerge()
              {
                Val = MergedCellValues.Continue
              };
              TableCellProperties tableCellProperties2 = new TableCellProperties();
              tableCellProperties2.Append(verticalMerge);
              cell.Append(tableCellProperties2);
              Text t = new Text(item[i]);
              t.Space = new EnumValue(SpaceProcessingModeValues.Preserve);
              run.Append(t);
            }
            else
            {
              //RunProperties rPr = new RunProperties();
              //rPr.Append(new Bold());
              //run.Append(rPr);

              //单元格内容添加(由内向外顺序)
              Text t = new Text(item[i]);
              t.Space = new EnumValue(SpaceProcessingModeValues.Preserve);
              run.Append(t);
            }
            par.Append(run);
            cell.Append(tableCellProperties);
            cell.Append(par);
            row.Append(cell);

          }
          j++;
          //表格添加行
          tab.Append(row);
        }
        //objBody.Append(new Paragraph());
        //objBody.Append(new Table());
        

        d.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(tab)));
        d.MainDocumentPart.Document.Save();
      }

    }
    

  }
}

使用OpenXml怎么合并Table单元格

看完上述内容,你们对使用OpenXml怎么合并Table单元格有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


新闻名称:使用OpenXml怎么合并Table单元格
标题链接:http://cdkjz.cn/article/jccdeh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220