C# 开源组件--Word操作组件DocX

时间:2022-01-22 04:23:44
  1. 使用模版生成简历
  2. 读写表格数据
  3. 合并单元格
  4. 工具源代码下载
  5. 学习使用

使用模版生成简历

下面将以一个简历实例来讲解DocX对表格的操作,先看看生成的效果

C# 开源组件--Word操作组件DocX

private static void CreateResume()
{
DocX doc;
try
{
doc = DocX.Load(@"ResumeTemplate.docx");
//给域赋值
if (doc.Tables != null && doc.Tables.Count > )
{
Table table=doc.Tables[];
//姓名
table.Rows[].Cells[].Paragraphs[].Append("DocX");
//姓名
table.Rows[].Cells[].Paragraphs[].Append("未婚"); //出生
table.Rows[].Cells[].Paragraphs[].Append("2010-09-19");
//政治面貌
table.Rows[].Cells[].Paragraphs[].Append("团员"); //性别
table.Rows[].Cells[].Paragraphs[].Append("男");
//民族
table.Rows[].Cells[].Paragraphs[].Append("汉族"); //学位
table.Rows[].Cells[].Paragraphs[].Append("大学本科");
//移动电话
table.Rows[].Cells[].Paragraphs[].Append(""); //专业
table.Rows[].Cells[].Paragraphs[].Append("软件工程");
//邮箱
table.Rows[].Cells[].Paragraphs[].Append("2345678@qq.com"); //地址
table.Rows[].Cells[].Paragraphs[].Append("故宫"); //照片
Novacode.Image image = doc.AddImage(@"logo.jpg");
Picture picture = image.CreatePicture();
table.Rows[].Cells[].InsertParagraph().AppendPicture(picture); //教育背景
table.Rows[].Cells[].Paragraphs[].Append("2011-2013");
table.Rows[].Cells[].Paragraphs[].Append("小学"); //主修课程
table.Rows[].Cells[].Paragraphs[].Append("数据结构,C语言,算法,C++"); //个人能力
table.Rows[].Cells[].Paragraphs[].Append("熟练掌握DocX操作Word,SQL能力强悍"); //计算机能力
table.Rows[].Cells[].Paragraphs[].Append("高级软件工程师"); //外语水平
table.Rows[].Cells[].Paragraphs[].Append("CET-4,CET-6"); //奖励情况
table.Rows[].Cells[].Paragraphs[].Append("1999年几月 曾获优秀班*,3等奖学金1999年几月 曾获校优秀*,学生会先进集体,2等奖学金20**年几月 曾获优秀学习委员,网络技术协会负责人,……………………"); //自我评价
table.Rows[].Cells[].Paragraphs[].Append("本人性格开朗、稳重、有活力,待人热情、真诚;工作认真负责,积极主动,能吃苦耐劳,用于承受压力,勇于创新;有很强的组织能力和团队协作精神,具有较强的适应能力;纪律性强,工作积极配合;意志坚强,具有较强的无私奉献精神。"); }
doc.SaveAs(@"DocXResume.docx");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

读写表格数据

上面的例子是想表格中写入数据,那么读取数据的操作可以类比出来,也是很简单的这里就不多介绍了,工具里面创建表格写入数据的代码贴出来,供大家参考。

/// <summary>
/// 根据读取的表信息导出WORD文档
/// </summary>
/// <param name="list">表信息集合</param>
/// <param name="strExportPath">导出路径</param>
public static void CreateWord(List<TableInfo> list, string strExportPath, BackgroundWorker bw)
{
int fontSize = ;
using (DocX doc = DocX.Create(strExportPath, DocumentTypes.Document))
{
int proc = ;
foreach (TableInfo t in list)
{
Paragraph p1 = doc.InsertParagraph();
p1.AppendLine(string.IsNullOrEmpty(t.Name) ? t.Code : t.Name + "\n").Bold(); Table table = doc.AddTable(t.ListColumnInfo.Count + , );
table.Design = TableDesign.TableGrid;
table.Alignment = Alignment.center; List<Row> rows = table.Rows;
Row row0 = rows[];
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append("数据表中文名称").FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append(t.Name).FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append("修改说明").FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append(t.IsUpdate ? "调整" : "新增").FontSize(fontSize).Color(t.IsUpdate ? Color.Red : Color.Blue); row0.Cells[].Width = ;
row0.Cells[].Width = ;
row0.Cells[].Width = ;
row0.Cells[].Width = ;
row0.Height = ; Row row1 = rows[];
row1.MergeCells(, );
row1.Cells[].Paragraphs[].Append("数据表英文名称").FontSize(fontSize);
row1.MergeCells(, );
row1.Cells[].Paragraphs[].Append(t.Code).FontSize(fontSize);
row1.Cells[].Width = ;
row1.Cells[].Width = ;
row1.Height = ; Row row2 = rows[];
row2.MergeCells(, );
row2.Cells[].Paragraphs[].Append("功能简述").FontSize(fontSize);
row2.MergeCells(, );
row2.Cells[].Paragraphs[].Append("").FontSize(fontSize);
row2.Cells[].Width = ;
row2.Cells[].Width = ;
row2.Height = ; Row row3 = rows[];
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ; row3.Cells[].Paragraphs[].Append("序号").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("字段中文名").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("字段英文名").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("数据类型").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("宽度").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("约束").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("默认值").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("空值").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("枚举&说明").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("自增").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("修改说明").Bold().FontSize(fontSize); Row row = null;
ColumnInfo info = null;
for (int i = ; i < t.ListColumnInfo.Count; i++)
{
row = rows[i + ];
info = t.ListColumnInfo[i];
row.Cells[].Paragraphs[].Append(info.Sequence.ToString()).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Name).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Code).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.DataType).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Width).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.PK ? "PK" : "").FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.DefaultValue).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Nullable ? "" : "N").FontSize(fontSize);
row.Cells[].Paragraphs[].Append("").FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Identity ? "Y" : "").FontSize(fontSize);
row.Cells[].Paragraphs[].Append("").FontSize(fontSize); row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ; row.Height = ;
}
p1.InsertTableAfterSelf(table);
bw.ReportProgress(proc * / list.Count, "Process");
proc++;
}
doc.Save();
}
}/// <summary>
/// 根据读取的表信息导出WORD文档
/// </summary>
/// <param name="list">表信息集合</param>
/// <param name="strExportPath">导出路径</param>
public static void CreateWord(List<TableInfo> list, string strExportPath, BackgroundWorker bw)
{
int fontSize = ;
using (DocX doc = DocX.Create(strExportPath, DocumentTypes.Document))
{
int proc = ;
foreach (TableInfo t in list)
{
Paragraph p1 = doc.InsertParagraph();
p1.AppendLine(string.IsNullOrEmpty(t.Name) ? t.Code : t.Name + "\n").Bold(); Table table = doc.AddTable(t.ListColumnInfo.Count + , );
table.Design = TableDesign.TableGrid;
table.Alignment = Alignment.center; List<Row> rows = table.Rows;
Row row0 = rows[];
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append("数据表中文名称").FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append(t.Name).FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append("修改说明").FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append(t.IsUpdate ? "调整" : "新增").FontSize(fontSize).Color(t.IsUpdate ? Color.Red : Color.Blue); row0.Cells[].Width = ;
row0.Cells[].Width = ;
row0.Cells[].Width = ;
row0.Cells[].Width = ;
row0.Height = ; Row row1 = rows[];
row1.MergeCells(, );
row1.Cells[].Paragraphs[].Append("数据表英文名称").FontSize(fontSize);
row1.MergeCells(, );
row1.Cells[].Paragraphs[].Append(t.Code).FontSize(fontSize);
row1.Cells[].Width = ;
row1.Cells[].Width = ;
row1.Height = ; Row row2 = rows[];
row2.MergeCells(, );
row2.Cells[].Paragraphs[].Append("功能简述").FontSize(fontSize);
row2.MergeCells(, );
row2.Cells[].Paragraphs[].Append("").FontSize(fontSize);
row2.Cells[].Width = ;
row2.Cells[].Width = ;
row2.Height = ; Row row3 = rows[];
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].FillColor = Color.FromArgb(, , );
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ;
row3.Cells[].Width = ; row3.Cells[].Paragraphs[].Append("序号").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("字段中文名").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("字段英文名").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("数据类型").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("宽度").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("约束").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("默认值").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("空值").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("枚举&说明").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("自增").Bold().FontSize(fontSize);
row3.Cells[].Paragraphs[].Append("修改说明").Bold().FontSize(fontSize); Row row = null;
ColumnInfo info = null;
for (int i = ; i < t.ListColumnInfo.Count; i++)
{
row = rows[i + ];
info = t.ListColumnInfo[i];
row.Cells[].Paragraphs[].Append(info.Sequence.ToString()).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Name).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Code).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.DataType).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Width).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.PK ? "PK" : "").FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.DefaultValue).FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Nullable ? "" : "N").FontSize(fontSize);
row.Cells[].Paragraphs[].Append("").FontSize(fontSize);
row.Cells[].Paragraphs[].Append(info.Identity ? "Y" : "").FontSize(fontSize);
row.Cells[].Paragraphs[].Append("").FontSize(fontSize); row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ;
row.Cells[].Width = ; row.Height = ;
}
p1.InsertTableAfterSelf(table);
bw.ReportProgress(proc * / list.Count, "Process");
proc++;
}
doc.Save();
}
}

合并单元格

DocX使用MergeCells方法就可以合并单元格了

 List<Row> rows = table.Rows;
Row row0 = rows[];
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append("数据表中文名称").FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append(t.Name).FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append("修改说明").FontSize(fontSize);
row0.MergeCells(, );
row0.Cells[].Paragraphs[].Append(t.IsUpdate ? "调整" : "新增").FontSize(fontSize).Color(t.IsUpdate ? Color.Red : Color.Blue);

本人转自:http://www.cnblogs.com/yanweidie/p/3861482.html#_label2