C#生成word文档,写入内容,带插入表格和格式排版功能(附源码)

//钟伟海 1078321412@qq.com 

public static void CreateWordFile(string filePath)
        {
            
            try
            {
                CreateFile(filePath);
                //
         
                _Document wordDoc = wordApp.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
               
                int ii = 0;
                foreach (DataRow dr in dtDepts.Rows)
                {
                    string dept = dr["dept"].ToString();
                    Paragraph oPara0 = wordDoc.Content.Paragraphs.Add(ref oMissing);
                    oPara0.Range.Text = string.Format("{0}-{1}", ii + 1, dept);
                    //oPara0.Range.Font.Bold = 1;
                    //oPara0.Format.SpaceAfter = 5;
                    oPara0.Range.Select();
                    oPara0.set_Style(ref heading2);
                    oPara0.Range.InsertParagraphAfter();
                    System.Data.DataTable dtTemplate = DatabaseHelper.getTemplateByDept(dept);
                    int jj = 0;
                    foreach (DataRow dr1 in dtTemplate.Rows)
                    {
                        string template = dr1["template"].ToString();
                        string user1 = dr1["user1"].ToString();
                        string remark = dr1["remark"].ToString();
                        System.Data.DataTable dtData = DatabaseHelper.getDataByDeptAndTemplate(dept, template);
                        int count = dtData.Rows.Count;
                        int row = count + 4;
                        int column = 5;
                        object ncount = 1;

                        wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);
                        wordApp.Selection.TypeParagraph();
                        Paragraph oPara1 = wordDoc.Content.Paragraphs.Add(ref oMissing);
                        oPara1.Range.Select();
                        oPara1.Range.Text = string.Format("{0}-{1}、{2}", ii + 1, jj + 1, template);
                        //oPara1.Range.Font.Bold = 1;
                        //oPara1.Format.SpaceAfter = 5;
                        oPara1.set_Style(ref heading3);
                        oPara1.Range.InsertParagraphAfter();
                        wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);
                        wordApp.Selection.TypeParagraph();
                        //设置表格
                        Table table = wordDoc.Tables.Add(wordApp.Selection.Range, row, column, ref oMissing, ref oMissing);
                       
                        table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                        table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                        table.Range.Font.Bold = 0;
                        table.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthAuto;
                        table.Columns[1].Width = 60f;
                        table.Columns[2].Width = 100f;
                        table.Columns[3].Width = 100f;
                        table.Columns[4].Width = 60f;
                        table.Columns[5].Width = 100f;
                        //列的合并
                        Cell cell = table.Cell(1, 2);
                        cell.Merge(table.Cell(1, 5));
                        Cell cell2 = table.Cell(2, 2);
                        cell2.Merge(table.Cell(2, 5));
                        Cell cell3 = table.Cell(3, 2);
                        cell3.Merge(table.Cell(3, 5));
                        //赋值
                        table.Cell(1, 1).Range.Text = "流程名称:";
                        table.Cell(2, 1).Range.Text = "使用人:";
                        table.Cell(3, 1).Range.Text = "流程说明:";
                        table.Cell(4, 1).Range.Text = "节点";
                        table.Cell(4, 2).Range.Text = "节点名";
                        table.Cell(4, 3).Range.Text = "处理人员";
                        table.Cell(4, 4).Range.Text = "处理方式";
                        table.Cell(4, 5).Range.Text = "跳转信息";
                        table.Cell(1, 2).Range.Text = template;
                        table.Cell(2, 2).Range.Text = user1;
                        table.Cell(3, 2).Range.Text = remark;
                        int kk = 5;
                        foreach (DataRow dr2 in dtData.Rows)
                        {
                            table.Cell(kk, 1).Range.Text = (kk - 4).ToString();
                            table.Cell(kk, 2).Range.Text = dr2["NodeName"].ToString();
                            table.Cell(kk, 3).Range.Text = dr2["DoName"].ToString();
                            table.Cell(kk, 4).Range.Text = dr2["DoType"].ToString();
                            table.Cell(kk, 5).Range.Text = string.Empty;
                            kk++;
                        }
                        table.Cell(kk - 1, 5).Range.Select();

                        wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);//移动焦点
                        wordApp.Selection.TypeParagraph();//插入段落

                        jj++;
                    }
                    ii++;
                }

                //保存
                wordDoc.Save();
                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                MessageFilter.Revoke();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);

            }
        }

vs2017源码

https://download.csdn.net/download/bluepb/12385529

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值