C#生成Word文档代码示例

所属分类: 软件编程 / C#教程 阅读数: 57
收藏 0 赞 0 分享
public bool CreateWordFile(string _filename, "数据List或者你C#要写的数据")
    {
            #region 开始生成Word
      try
      {

        string strtitle = "任务导出";
        object oEndOfDoc = "//endofdoc";
        Object Nothing = System.Reflection.Missing.Value;
        Object filename = _filename;
        //创建Word文档

        Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);


        //设置页眉
        WordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
        WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
        WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("WPFOA任务导出");
        WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
        WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置
        //任务导出------名字
        Word.Paragraph oPara1;
        oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing);
        oPara1.Range.Text = strtitle;
        oPara1.Range.Font.Bold = 1;
        oPara1.Range.Font.Name = "宋体";
        oPara1.Range.Font.Size = 20;
        oPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
        oPara1.Format.SpaceAfter = 5;  //24 pt spacing after paragraph.
        oPara1.Range.InsertParagraphAfter();
        #region 循环每个表

        foreach (var v in lst_task)
        {
          #region 循环每一个列,产生一行数据
          //描述信息
          Word.Paragraph oPara3;
          object oRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
          oPara3 = WordDoc.Content.Paragraphs.Add(ref oRng);
          oPara3.Range.Text = "";
          oPara3.Range.Font.Bold = 0;
          oPara3.Range.Font.Name = "宋体";
          oPara3.Range.Font.Size = 9;
          oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
          oPara3.Format.SpaceBefore = 1;
          oPara3.Format.SpaceAfter = 1;
          oPara3.Range.InsertParagraphAfter();

          //插入表格     
          Word.Table newTable;
          Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
          newTable = WordDoc.Tables.Add(wrdRng, 5, 4, ref Nothing, ref Nothing);
          newTable.Columns[1].Width = 60;
          newTable.Columns[2].Width = 145;
          newTable.Columns[3].Width = 80;
          newTable.Columns[4].Width = 145;
          newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
          newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

          //填充表格内容
          newTable.Cell(1, 1).Range.Text = "项目名称";
          newTable.Cell(1, 2).Range.Text = "1";
          newTable.Cell(1, 3).Range.Text = "模块名称";
          newTable.Cell(1, 4).Range.Text = "2";
          newTable.Cell(2, 1).Range.Text = "负责人名";
          newTable.Cell(2, 2).Range.Text = "3";
          newTable.Cell(2, 3).Range.Text = "状态";
          newTable.Cell(2, 4).Range.Text = "4";
          newTable.Cell(3, 1).Range.Text = "发布时间";
          newTable.Cell(3, 2).Range.Text = "5";
          newTable.Cell(3, 3).Range.Text = "要求完成时间";
          newTable.Cell(3, 4).Range.Text = "6";
          newTable.Cell(4, 1).Range.Text = "完成时间";
          newTable.Cell(4, 2).Range.Text = "7";
          newTable.Cell(4, 3).Range.Text = "分配人";
          newTable.Cell(4, 4).Range.Text = "8";
          newTable.Cell(5, 1).Range.Text = "任务描述";
          newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));
          newTable.Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
          newTable.Cell(5, 2).Range.Text = "12313213123213";
          #endregion
        }


        WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing,
         ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
         ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
         ref Nothing, ref Nothing);

        WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        #endregion
        //关闭WinWord进程
        System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
        MyProcess[0].Kill();
        return true;

      }
      catch
      {

        System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
        MyProcess[0].Kill();
        return false;
      }
      #endregion
    }


更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多