在本篇文章里小编给大家整理了关于OpenXml合并Table单元格的相关实例详解,需要的朋友们参考下。
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<List<string>> _tabData;
public Program(List<List<string>> tabData) {
_tabData = tabData;
}
static void Main(string[] args)
{
List<string> dataTitle = new List<string>() { "序号","姓名","性别"};
List<string> data1 = new List<string>() { "1", "张三", "男" };
List<string> data2 = new List<string>() { "2", "王五", "男" };
List<string> data3 = new List<string>() { "3", "李四", "女" };
_tabData = new List<List<string>>();
_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();
}
/// <summary>
/// 创建Word
/// </summary>
/// <param name="filePath"></param>
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);
/
织梦狗教程
本文标题为:OpenXml合并Table单元格代码实例


基础教程推荐
猜你喜欢
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity shader实现高斯模糊效果 2023-01-16
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#中的Linq to JSON操作详解 2023-06-08
- C# 解析XML和反序列化的示例 2023-04-14
- C#调用摄像头实现拍照功能的示例代码 2023-03-09