C#使用Docx编写word表格
最近接手了一个小Demo,要求使用Docx,将Xml文件中的数据转换为word文档,组织数据形成表格。
写了已经一周,网络上的知识太零碎,就想自己先统计整理出来,方便以后查阅。
目前就记录自己会用的和知道的一些,后续的知识,会持续的更新补上。
//检定原始记录——补偿器
public void CreatTable_Compensator(string path)
{
string realpath = @path + "\\补偿器.docx";
try
{
if (File.Exists(realpath))
{
//若存在则删除
File.Delete(realpath);
}
//File.Create(realpath).Close();
//创建表格
using (var document = DocX.Create(realpath))
{
var title = document.InsertParagraph().Append("补偿器检定记录").Bold().FontSize(20);
title.Alignment = Alignment.center;
var table = document.AddTable(24, 6);
table.Design = TableDesign.TableGrid;
/*table.AutoFit = AutoFit.Window;*/
table.Alignment = Alignment.center;
List<Row> rows = table.Rows;
Row row0 = rows[0];
row0.Height = 50;
row0.MergeCells(0, 2);
row0.Cells[0].Paragraphs[0].Append("设备ID:" + "18635445").FontSize(16).Font(new FontFamily("Arial")).Bold().Alignment = Alignment.center;
row0.Cells[0].VerticalAlignment = VerticalAlignment.Center;
row0.MergeCells(1, 3);
row0.Cells[1].Paragraphs[0].Append("仪器编号:" + "FH5919").FontSize(16).Font(new FontFamily("Arial")).Bold().Alignment = Alignment.center;
row0.Cells[1].VerticalAlignment = VerticalAlignment.Center;
//第二行
Row row1 = rows[1];
row1.MergeCells(0, 5);
row1.Cells[0].Paragraphs[0].Append("补偿范围检定记录").Bold().FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center;
row1.Height = 60;
row1.Cells[0].VerticalAlignment = VerticalAlignment.Center;
//第三行
Row row2 = rows[2];
row2.MergeCells(0, 1);
row2.Cells[0].Paragraphs[0].Append("次序").FontSize(11).Font(new FontFamily("等线")).Alignment = Alignment.center;
row2.MergeCells(1, 4);
row2.Cells[1].Paragraphs[0].Append("观测值显示").FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center;
//第四行
Row row3 = rows[3];
row3.MergeCells(0, 1);
row3.Cells[0].Paragraphs[0].Append("1").FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center;
row3.MergeCells(1, 4);
row3.Cells[1].Paragraphs[0].Append("3.[1]").FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center;
row3.Height = 35;
row3.Cells[0].VerticalAlignment = VerticalAlignment.Center;
row3.Cells[1].VerticalAlignment = VerticalAlignment.Center;
//第五行
Row row4 = rows[4];
row4.MergeCells(0, 1);
row4.Cells[0].Paragraphs[0].Append("2").FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center;
row4.MergeCells(1, 4);
row4.Cells[1].Paragraphs[0].Append("4.[1]").FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center;
row4.Height = 35;
row4.Cells[0].VerticalAlignment = VerticalAlignment.Center;
row4.Cells[1].VerticalAlignment = VerticalAlignment.Center;
//第六行
Row row5 = rows[5];
row5.MergeCells(0, 1);
row5.Cells[0].Paragraphs[0].Append("计算结果").FontSize(11).Font(new FontFamily("Arial")).Alignment = Alignment.center; ;
row5.MergeCells(1, 4);
row5.Cells[1].Paragraphs[0].Append("5.[1]").FontSize(11).Font(new FontFamily("Arial"));
row5.Height = 35;
row5.Cells[0].VerticalAlignment = VerticalAlignment.Center;
row5.Cells[1].VerticalAlignment = VerticalAlignment.Center;
//第七行
Row row6 = rows[6];
row6.MergeCells(0, 5);
row6.Cells[0