C# word 操作进阶-02-表格底部创建动态行

要求:生成纯化水电导率检测记录,要打印出来存档。
最终效果

需求分析:

1.文档中,编号、生产单位、资产编号等等都要读取赋值。

2.表格行数据根据数据量自动创建。

1.先画word模板,如下

注意:这里表格保留了一行空数据,因为行内有样式,例如居中、字体大小等等,后续创建的行会模仿此行的样式,建议保留一行数据行作为模板行,毕竟迫不得已谁都不想用代码调word样式。

2.WordHelper.cs增加方法,WordHelper.cs在(C# word 操作进阶-01-模板环境 - 0Behavior - 博客园 (cnblogs.com))中有介绍

    /// <summary>
        /// ATableBLL生成word
        /// 纯化水电导率仪检测记录
        /// </summary>
        /// <param name="dc">基础参数</param>
        /// <param name="dt">数据表</param>
        /// <returns></returns>
        public TData ATableBLLGenerateWord1(Dictionary<string, string> dc, DataTable dt)
        {
            TData result = new TData();
            //初始化
            TData td_Init = Init();
            if (td_Init.Tag == 0)
            {
                return td_Init;
            }
            //绑定基础参数
            TData td_ReplaceAllRang = ReplaceAllRang(dc);
            if (td_ReplaceAllRang.Tag == 0)
            {
                return td_Init;
            }
            try
            {

                //绑定数据表
                word.Table tb = wdoc.Tables[1];
                //第二列为恒定值
                string column2 = "";
                //第一行为标题,行数从1开始计数,所以第一行第一列数据的绑定下标为tb.Cell(2, 1)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i > 0)
                    {
                        tb.Rows.Add(ref missing);

                    }
                    //普通文本
                    tb.Cell(i + 2, 1).Range.Text = dt.Rows[i][0].ToString();
                    tb.Cell(i + 2, 4).Range.Text = dt.Rows[i][2].ToString();
                    tb.Cell(i + 2, 5).Range.Text = dt.Rows[i][3].ToString();
                    tb.Cell(i + 2, 6).Range.Text = dt.Rows[i][4].ToString();
                    tb.Cell(i + 2, 7).Range.Text = dt.Rows[i][5].ToString();

                    //特殊符号
                    tb.Cell(i + 2, 3).Range.Font.Name = "Wingdings 2";//设置字体类型
                                                                      //word中,插入>符号>其他符号>字符代码(十六进制)
                    tb.Cell(i + 2, 3).Range.Text = dt.Rows[i][1].ToString() == "1" ? "\u0053" : "\u0052";



                }
                if (dt.Rows.Count > 0)
                {
                    //纵向合并单元格
                    wdoc.Tables[1].Cell(2, 2).Select();//第二行第二列
                    object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                    object moveCount = dt.Rows.Count - 1;//合并所有行
                    object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                    wapp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                    wapp.Selection.Cells.Merge();
                    wdoc.Tables[1].Cell(2, 2).Range.ParagraphFormat.Alignment = word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
                    wdoc.Tables[1].Cell(2, 2).VerticalAlignment = word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中

                    //保存文件
                    wdoc.SaveAs2(ref savefile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                      ref missing, ref missing);
                }
                DisposeWord();
                result.Tag = 1;
                result.Message = savefile.ToString();
            }
            catch (Exception ex)
            {
                result.Message = "生成失败:" + ex.Message;
            }
            return result;
        }

3.业务层准备测试数据,调用第2步中的ATableBLLGenerateWord1方法

                    //开始模拟测试数据
                    Dictionary<string, string> dc = new Dictionary<string, string>();
                    dc.Add("$记录编号$", "QC10001");
                    dc.Add("$E1_Text1$", "沃特世公司");
                    dc.Add("$E1_Text2$", "No2002231");
                    dc.Add("$E1_Text3$", "2025-03-15");
                    dc.Add("$E2_Text2$", "No1222011");
                    dc.Add("$E2_Text3$", "2022-01-22");

                    #region datatable测试数据
                    //https://docs.microsoft.com/zh-cn/dotnet/api/system.data.datatable?view=netframework-4.7.2
                    DataTable table = new DataTable();
                    DataColumn column;
                    DataRow row;

                    //检测日期		定标结果	检测结果	结论	检测者	总务科知晓
                    // Create first column and add to the DataTable.
                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "C1";
                    column.Caption = "检测日期";
                    table.Columns.Add(column);

                    // Create second column.
                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.Int32");
                    column.ColumnName = "C2";
                    column.Caption = "定标结果";
                    table.Columns.Add(column);

                    // Create third column.
                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.Int32");
                    column.ColumnName = "C3";
                    column.Caption = "检测结果";
                    table.Columns.Add(column);

                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "C4";
                    column.Caption = "结论";
                    table.Columns.Add(column);

                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "C5";
                    column.Caption = "检测者";
                    table.Columns.Add(column);

                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "C6";
                    column.Caption = "总务科知晓";
                    table.Columns.Add(column);



                    row = table.NewRow();
                    row["C1"] = "2022-07-01";
                    row["C2"] = 1;
                    row["C3"] = 80;
                    row["C4"] = "合格";
                    row["C5"] = "张1";
                    row["C6"] = "是";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["C1"] = "2022-07-02";
                    row["C2"] = 0;
                    row["C3"] = 120;
                    row["C4"] = "不合格";
                    row["C5"] = "张2";
                    row["C6"] = "是";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["C1"] = "2022-07-03";
                    row["C2"] = 0;
                    row["C3"] = 110;
                    row["C4"] = "不合格";
                    row["C5"] = "张3";
                    row["C6"] = "否";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["C1"] = "2022-07-04";
                    row["C2"] = 1;
                    row["C3"] = 83;
                    row["C4"] = "合格";
                    row["C5"] = "王2";
                    row["C6"] = "是";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["C1"] = "2022-07-05";
                    row["C2"] = 1;
                    row["C3"] = 84;
                    row["C4"] = "合格";
                    row["C5"] = "王3";
                    row["C6"] = "是";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["C1"] = "2022-07-06";
                    row["C2"] = 1;
                    row["C3"] = 85;
                    row["C4"] = "合格";
                    row["C5"] = "王4";
                    row["C6"] = "否";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["C1"] = "2022-07-07";
                    row["C2"] = 1;
                    row["C3"] = 86;
                    row["C4"] = "合格";
                    row["C5"] = "王5";
                    row["C6"] = "否";
                    table.Rows.Add(row);
                    #endregion

                    obj = new WordHelper(src + template, src + savefile).ATableBLLGenerateWord1(dc, table);

注意:src + template是word模板物理路径, src + savefile是生成word后保存的物理路径

 

posted @ 2022-07-28 16:09  0Behavior  阅读(220)  评论(0编辑  收藏  举报