使用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