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

  1. 使用模版生成简历
  2. 读写表格数据
  3. 合并单元格
  4. 工具源代码下载
  5. 学习使用

使用模版生成简历                                                                               

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

 

private static void CreateResume()
        {
            DocX doc;
            try
            {
                doc = DocX.Load(@"ResumeTemplate.docx");
                //给域赋值
                if (doc.Tables != null && doc.Tables.Count > 0)
                {
                    Table table=doc.Tables[0];
                    //姓名
                    table.Rows[1].Cells[1].Paragraphs[0].Append("DocX");
                    //姓名
                    table.Rows[1].Cells[3].Paragraphs[0].Append("未婚");

                    //出生
                    table.Rows[2].Cells[1].Paragraphs[0].Append("2010-09-19");
                    //政治面貌
                    table.Rows[2].Cells[3].Paragraphs[0].Append("团员");

                    //性别
                    table.Rows[3].Cells[1].Paragraphs[0].Append("");
                    //民族
                    table.Rows[3].Cells[3].Paragraphs[0].Append("汉族");

                    //学位
                    table.Rows[4].Cells[1].Paragraphs[0].Append("大学本科");
                    //移动电话
                    table.Rows[4].Cells[3].Paragraphs[0].Append("13567890987");

                    //专业
                    table.Rows[5].Cells[1].Paragraphs[0].Append("软件工程");
                    //邮箱
                    table.Rows[5].Cells[3].Paragraphs[0].Append("2345678@qq.com");

                    //地址
                    table.Rows[6].Cells[1].Paragraphs[0].Append("故宫");

                    //照片
                    Novacode.Image image = doc.AddImage(@"logo.jpg");
                    Picture picture = image.CreatePicture();
                    table.Rows[1].Cells[4].InsertParagraph().AppendPicture(picture);

                    //教育背景
                    table.Rows[8].Cells[0].Paragraphs[0].Append("2011-2013");
                    table.Rows[8].Cells[1].Paragraphs[0].Append("小学");

                    //主修课程
                    table.Rows[10].Cells[0].Paragraphs[0].Append("数据结构,C语言,算法,C++");

                    //个人能力
                    table.Rows[12].Cells[0].Paragraphs[0].Append("熟练掌握DocX操作Word,SQL能力强悍");

                    //计算机能力
                    table.Rows[14].Cells[0].Paragraphs[0].Append("高级软件工程师");

                    //外语水平
                    table.Rows[16].Cells[0].Paragraphs[0].Append("CET-4,CET-6");

                    //奖励情况
                    table.Rows[18].Cells[0].Paragraphs[0].Append("1999年几月  曾获优秀班干部,3等奖学金1999年几月  曾获校优秀干部,学生会先进集体,2等奖学金20**年几月  曾获优秀学习委员,网络技术协会负责人,……………………");

                    //自我评价
                    table.Rows[20].Cells[0].Paragraphs[0].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 = 9;
            using (DocX doc = DocX.Create(strExportPath, DocumentTypes.Document))
            {
                int proc = 1;
                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 + 4, 11);
                    table.Design = TableDesign.TableGrid;
                    table.Alignment = Alignment.center;

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

                    row0.Cells[0].Width = 143;
                    row0.Cells[1].Width = 211;
                    row0.Cells[2].Width = 127;
                    row0.Cells[3].Width = 149;
                    row0.Height = 25;

                    Row row1 = rows[1];
                    row1.MergeCells(0, 1);
                    row1.Cells[0].Paragraphs[0].Append("数据表英文名称").FontSize(fontSize);
                    row1.MergeCells(1, 9);
                    row1.Cells[1].Paragraphs[0].Append(t.Code).FontSize(fontSize);
                    row1.Cells[0].Width = 143;
                    row1.Cells[1].Width = 487;
                    row1.Height = 25;

                    Row row2 = rows[2];
                    row2.MergeCells(0, 1);
                    row2.Cells[0].Paragraphs[0].Append("功能简述").FontSize(fontSize);
                    row2.MergeCells(1, 9);
                    row2.Cells[1].Paragraphs[0].Append("").FontSize(fontSize);
                    row2.Cells[0].Width = 143;
                    row2.Cells[1].Width = 487;
                    row2.Height = 25;

                    Row row3 = rows[3];
                    row3.Cells[0].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[1].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[2].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[3].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[4].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[5].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[6].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[7].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[8].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[9].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[10].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[0].Width = 35;
                    row3.Cells[1].Width = 108;
                    row3.Cells[2].Width = 116;
                    row3.Cells[3].Width = 95;
                    row3.Cells[4].Width = 51;
                    row3.Cells[5].Width = 36;
                    row3.Cells[6].Width = 40;
                    row3.Cells[7].Width = 35;
                    row3.Cells[8].Width = 40;
                    row3.Cells[9].Width = 39;
                    row3.Cells[10].Width = 35;

                    row3.Cells[0].Paragraphs[0].Append("序号").Bold().FontSize(fontSize);
                    row3.Cells[1].Paragraphs[0].Append("字段中文名").Bold().FontSize(fontSize);
                    row3.Cells[2].Paragraphs[0].Append("字段英文名").Bold().FontSize(fontSize);
                    row3.Cells[3].Paragraphs[0].Append("数据类型").Bold().FontSize(fontSize);
                    row3.Cells[4].Paragraphs[0].Append("宽度").Bold().FontSize(fontSize);
                    row3.Cells[5].Paragraphs[0].Append("约束").Bold().FontSize(fontSize);
                    row3.Cells[6].Paragraphs[0].Append("默认值").Bold().FontSize(fontSize);
                    row3.Cells[7].Paragraphs[0].Append("空值").Bold().FontSize(fontSize);
                    row3.Cells[8].Paragraphs[0].Append("枚举&说明").Bold().FontSize(fontSize);
                    row3.Cells[9].Paragraphs[0].Append("自增").Bold().FontSize(fontSize);
                    row3.Cells[10].Paragraphs[0].Append("修改说明").Bold().FontSize(fontSize);

                    Row row = null;
                    ColumnInfo info = null;
                    for (int i = 0; i < t.ListColumnInfo.Count; i++)
                    {
                        row = rows[i + 4];
                        info = t.ListColumnInfo[i];
                        row.Cells[0].Paragraphs[0].Append(info.Sequence.ToString()).FontSize(fontSize);
                        row.Cells[1].Paragraphs[0].Append(info.Name).FontSize(fontSize);
                        row.Cells[2].Paragraphs[0].Append(info.Code).FontSize(fontSize);
                        row.Cells[3].Paragraphs[0].Append(info.DataType).FontSize(fontSize);
                        row.Cells[4].Paragraphs[0].Append(info.Width).FontSize(fontSize);
                        row.Cells[5].Paragraphs[0].Append(info.PK ? "PK" : "").FontSize(fontSize);
                        row.Cells[6].Paragraphs[0].Append(info.DefaultValue).FontSize(fontSize);
                        row.Cells[7].Paragraphs[0].Append(info.Nullable ? "" : "N").FontSize(fontSize);
                        row.Cells[8].Paragraphs[0].Append("").FontSize(fontSize);
                        row.Cells[9].Paragraphs[0].Append(info.Identity ? "Y" : "").FontSize(fontSize);
                        row.Cells[10].Paragraphs[0].Append("").FontSize(fontSize);


                        row.Cells[0].Width = 35;
                        row.Cells[1].Width = 108;
                        row.Cells[2].Width = 116;
                        row.Cells[3].Width = 95;
                        row.Cells[4].Width = 51;
                        row.Cells[5].Width = 36;
                        row.Cells[6].Width = 40;
                        row.Cells[7].Width = 35;
                        row.Cells[8].Width = 40;
                        row.Cells[9].Width = 39;
                        row.Cells[10].Width = 35;

                        row.Height = 35;
                    }
                    p1.InsertTableAfterSelf(table);
                    bw.ReportProgress(proc * 100 / 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 = 9;
            using (DocX doc = DocX.Create(strExportPath, DocumentTypes.Document))
            {
                int proc = 1;
                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 + 4, 11);
                    table.Design = TableDesign.TableGrid;
                    table.Alignment = Alignment.center;

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

                    row0.Cells[0].Width = 143;
                    row0.Cells[1].Width = 211;
                    row0.Cells[2].Width = 127;
                    row0.Cells[3].Width = 149;
                    row0.Height = 25;

                    Row row1 = rows[1];
                    row1.MergeCells(0, 1);
                    row1.Cells[0].Paragraphs[0].Append("数据表英文名称").FontSize(fontSize);
                    row1.MergeCells(1, 9);
                    row1.Cells[1].Paragraphs[0].Append(t.Code).FontSize(fontSize);
                    row1.Cells[0].Width = 143;
                    row1.Cells[1].Width = 487;
                    row1.Height = 25;

                    Row row2 = rows[2];
                    row2.MergeCells(0, 1);
                    row2.Cells[0].Paragraphs[0].Append("功能简述").FontSize(fontSize);
                    row2.MergeCells(1, 9);
                    row2.Cells[1].Paragraphs[0].Append("").FontSize(fontSize);
                    row2.Cells[0].Width = 143;
                    row2.Cells[1].Width = 487;
                    row2.Height = 25;

                    Row row3 = rows[3];
                    row3.Cells[0].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[1].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[2].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[3].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[4].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[5].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[6].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[7].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[8].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[9].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[10].FillColor = Color.FromArgb(226, 226, 226);
                    row3.Cells[0].Width = 35;
                    row3.Cells[1].Width = 108;
                    row3.Cells[2].Width = 116;
                    row3.Cells[3].Width = 95;
                    row3.Cells[4].Width = 51;
                    row3.Cells[5].Width = 36;
                    row3.Cells[6].Width = 40;
                    row3.Cells[7].Width = 35;
                    row3.Cells[8].Width = 40;
                    row3.Cells[9].Width = 39;
                    row3.Cells[10].Width = 35;

                    row3.Cells[0].Paragraphs[0].Append("序号").Bold().FontSize(fontSize);
                    row3.Cells[1].Paragraphs[0].Append("字段中文名").Bold().FontSize(fontSize);
                    row3.Cells[2].Paragraphs[0].Append("字段英文名").Bold().FontSize(fontSize);
                    row3.Cells[3].Paragraphs[0].Append("数据类型").Bold().FontSize(fontSize);
                    row3.Cells[4].Paragraphs[0].Append("宽度").Bold().FontSize(fontSize);
                    row3.Cells[5].Paragraphs[0].Append("约束").Bold().FontSize(fontSize);
                    row3.Cells[6].Paragraphs[0].Append("默认值").Bold().FontSize(fontSize);
                    row3.Cells[7].Paragraphs[0].Append("空值").Bold().FontSize(fontSize);
                    row3.Cells[8].Paragraphs[0].Append("枚举&说明").Bold().FontSize(fontSize);
                    row3.Cells[9].Paragraphs[0].Append("自增").Bold().FontSize(fontSize);
                    row3.Cells[10].Paragraphs[0].Append("修改说明").Bold().FontSize(fontSize);

                    Row row = null;
                    ColumnInfo info = null;
                    for (int i = 0; i < t.ListColumnInfo.Count; i++)
                    {
                        row = rows[i + 4];
                        info = t.ListColumnInfo[i];
                        row.Cells[0].Paragraphs[0].Append(info.Sequence.ToString()).FontSize(fontSize);
                        row.Cells[1].Paragraphs[0].Append(info.Name).FontSize(fontSize);
                        row.Cells[2].Paragraphs[0].Append(info.Code).FontSize(fontSize);
                        row.Cells[3].Paragraphs[0].Append(info.DataType).FontSize(fontSize);
                        row.Cells[4].Paragraphs[0].Append(info.Width).FontSize(fontSize);
                        row.Cells[5].Paragraphs[0].Append(info.PK ? "PK" : "").FontSize(fontSize);
                        row.Cells[6].Paragraphs[0].Append(info.DefaultValue).FontSize(fontSize);
                        row.Cells[7].Paragraphs[0].Append(info.Nullable ? "" : "N").FontSize(fontSize);
                        row.Cells[8].Paragraphs[0].Append("").FontSize(fontSize);
                        row.Cells[9].Paragraphs[0].Append(info.Identity ? "Y" : "").FontSize(fontSize);
                        row.Cells[10].Paragraphs[0].Append("").FontSize(fontSize);


                        row.Cells[0].Width = 35;
                        row.Cells[1].Width = 108;
                        row.Cells[2].Width = 116;
                        row.Cells[3].Width = 95;
                        row.Cells[4].Width = 51;
                        row.Cells[5].Width = 36;
                        row.Cells[6].Width = 40;
                        row.Cells[7].Width = 35;
                        row.Cells[8].Width = 40;
                        row.Cells[9].Width = 39;
                        row.Cells[10].Width = 35;

                        row.Height = 35;
                    }
                    p1.InsertTableAfterSelf(table);
                    bw.ReportProgress(proc * 100 / list.Count, "Process");
                    proc++;
                }
                doc.Save();
            }
        }

 

合并单元格                                                                                        

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

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

 

 

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

 

posted @ 2017-01-01 15:38  奔跑.Ken  阅读(5791)  评论(0编辑  收藏  举报