使用Docx.Core创建word表格 并合并单元格
使用Docx.Core创建word表格
下载DocxCore Nuget包 当前版本 1.0.7
Install-Package DocxCore -Version 1.0.7
创建表格参数
TableDto.cs
/// <summary>
/// 表格
/// </summary>
public class TableDto
{
/// <summary>
/// 表头
/// </summary>
public List<List<TableTdDto>> TRS { get; set; }
/// <summary>
/// 表内容
/// </summary>
public List<List<TableTdDto>> TDS { get; set; }
}
TableTdDto.cs
/// <summary>
/// 表格TD属性
/// </summary>
public class TableTdDto
{
/// <summary>
/// 宽度比例
/// </summary>
public int W { get; set; }
/// <summary>
/// 内容
/// </summary>
public string N { get; set; }
/// <summary>
/// 占列数,对应 colspan
/// </summary>
public int CL { get; set; } = 1;
/// <summary>
/// 占行数,对应 rowspan
/// </summary>
public int RL { get; set; } = 1;
/// <summary>
/// 个数,扩展
/// </summary>
public int C1 { get; set; } = 0;
/// <summary>
/// 样式
/// </summary>
public string S { get; set; }
}
使用Docx创建表格
public class DocxHelper
{
/// <summary>
/// 创建word
/// </summary>
/// <param name="dto"></param>
public static void CreateWord(TableDocumentDto dto)
{
var uploadPath = AppDomain.CurrentDomain.BaseDirectory;
string fileName = string.Format("{0}.docx", dto.Title, System.Text.Encoding.UTF8);
// Create a document.
using (var document = DocX.Create(uploadPath+fileName))
{
var first = dto.Table.TRS.FirstOrDefault();
var cols = first.Sum(o => o.CL);
var rows = dto.Table.TRS.Count + dto.Table.TDS.Count;
var headerTable = dto.Table.TRS;
var w = first.Sum(o => o.W);
var allWidth = w == 0 ? 5200 : w;
Table table1 = document.AddTable(rows, cols);
table1.Design = TableDesign.TableGrid; //表格样式
table1.Alignment = Alignment.center; //设置表格居中
headerTable.AddRange(dto.Table.TDS);
for (int i = 0; i < headerTable.Count; i++)
{
if (table1.Rows.Count < headerTable.Count)
{
throw new Exception("请检查表格参数");
}
var rol = headerTable[i].Max(o => o.RL);
int a = 0;//表示起始列位置
for (int j = 0; j < headerTable[i].Count; j++)
{
if (table1.Rows[i].Cells.Count < headerTable[i].Count)
{
throw new Exception("请检查表格参数");
}
if (headerTable[i][j].CL > 1 && headerTable[i][j].RL > 1)
{
throw new Exception("当前无法同时合并行和列");
}
var width = headerTable[i][j].W == 0 ? 120 : headerTable[i][j].W;
//当前合并列
if (headerTable[i][j].CL > 1)
{
//当前需要合并列
//MergeCells(起始列,结束列);
table1.Rows[i].MergeCells(a, (headerTable[i][j].CL - 1) + a);//合并列
table1.Rows[i].Cells[a].Paragraphs[0].Append(headerTable[i][j].N).Bold();
a += headerTable[i][j].CL - 1;
}
else if (headerTable[i][j].RL > 1)
{
//当前需要合并行
//MergeCellsInColumn(起始列,起始行,结束行)
table1.MergeCellsInColumn(j, i, (i+headerTable[i][j].RL) - 1);//合并行
table1.Rows[i].Cells[a].Paragraphs[0].Append(headerTable[i][j].N).Bold();
a++;
}
else
{
table1.Rows[i].Cells[a].Paragraphs[0].Append(headerTable[i][j].N).Bold();
a++;
}
}
}
//table1.MergeCellsInColumn(2, 2, 3);
//table1.Rows[0].Cells[0].Paragraphs[0].Append("列1").Bold();
//table1.Rows[0].Cells[1].Paragraphs[0].Append("列2").Bold();
//table1.Rows[0].Cells[0].Width = 100; //设置单元格宽度
//table1.Rows[0].Cells[1].Width = 100;
//table1.Rows[1].MergeCells(1, 2);
//table1.Rows[1].Cells[1].Paragraphs[0].Append("列2").Bold();
Paragraph p = document.InsertParagraph();
p.Alignment = Alignment.center;
p.Append(dto.Title).Bold();
p.InsertTableAfterSelf(table1);
document.Save();
Console.WriteLine($"表【{dto.Title}.docx】创建成功");
}
}
}
在使用时 行和列一起合并的时候出现索引无法找到问题 所以现在无法进行行列一起合并 只能单独合并
一定要正确定义TableDto 使用CL和RL可进行合并,但不能同时使用 每一列都是固定的 例如
list
集合里有7条数据 其中有一条数据进行了合并占用了两格
所以有8列数据 那么以下所有的数据都最多只能占用8列,超出将会报错
如果要上传图片的话 使用以下方法 将图片放置在单元格中
var image = document.AddImage(@"D:/其他文件/图片/" + @"logo.png");
// Create a picture from image.
var picture = image.CreatePicture(25, 100);
table1.Rows[i].Cells[a].Paragraphs[0].AppendPicture(picture);//第一张图
table1.Rows[i].Cells[a].Paragraphs[0].AppendPicture(picture);//第二张图
table1.Rows[i].Cells[a].Paragraphs[0].AppendPicture(picture);//第三张图
//当前图片都放置在同一单元格中
测试
class Program
{
static void Main(string[] args)
{
var filePath = @"D:\文档\标题2.docx";
var list = new List<List<TableTdDto>>();
list.Add(new List<TableTdDto>() {
new TableTdDto(){
N = "序号",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "检查项目",
CL = 2,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "扣分标准",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "应得分数",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "扣减分数",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "实得分数",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "备注",
CL = 1,
RL = 1,
W = 10,
},
});
var tes = new List<List<TableTdDto>>();
tes.Add(new List<TableTdDto>() {
new TableTdDto(){
N = "这是序号",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "保证项目",
CL = 1,
RL = 6,
W = 10,
},
new TableTdDto(){
N = "安全生产责任制",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = @"测试数据",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = @"10",
CL = 1,
RL = 1,
W = 10,
}
});
var tableTd = new List<TableTdDto>();
for (int i = 0; i < 5; i++)
{
tableTd.Add(new TableTdDto()
{
N = "测试数据",
CL = 1,
RL = 1,
W = 10,
});
tes.Add(tableTd);
}
tes.Add(new List<TableTdDto>() {
new TableTdDto(){
N = "1",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = "一般项目",
CL = 1,
RL = 5,
W = 10,
},
new TableTdDto(){
N = "安全生产责任制",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = @"测试数据",
CL = 1,
RL = 1,
W = 10,
},
new TableTdDto(){
N = @"10",
CL = 1,
RL = 1,
W = 10,
}
});
var tableTds = new List<TableTdDto>();
for (int i = 0; i < 4; i++)
{
tableTds.Add(new TableTdDto()
{
N = "测试数据",
CL = 1,
RL = 1,
W = 10,
});
tes.Add(tableTd);
}
var data = new TableDocumentDto()
{
Title = "安全管理检查评分表",
Table = new TableDto() {
TDS = tes,
TRS = list
}
};
DocxHelper.CreateWord(data);
Console.ReadKey();
}
}
END
文档太少,例子太少,只有一步一步的试每个方法.最开始用的NPOI
但是NPOI文档都找不到了,合并列还行,到合并行的时候就没办法了,按照其他博客进行操作,太复杂,也没有解释属性和方法的意思,造成即使写了出现错误也不知道为什么会出现这个问题.
Docx虽然文档也没有找到 但是靠猜方法找到了合并行和列的方法 解决了我的问题
例子:https://github.com/xceedsoftware/DocX/tree/master/Xceed.Words.NET.Examples/Samples