C#生成Word文档代码示例

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
    }
(0)

相关推荐

  • C#对文件/文件夹操作代码汇总

    C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw.WriteLine("追逐理想"); sw.WriteLine("kzlll"); sw.WriteLine(".NET笔记"); sw.Flush(); sw.Close(); C#拷贝文件 string OrignFile,NewFile; O

  • C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例

    思路:使用随机向量,把随机向量放入密文中,每次解密时从密文中截取前16位,其实就是我们之前加密的随机向量. 代码: public static string Encrypt(string plainText, string AESKey) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组

  • C#判断某个软件是否已安装实现代码分享

    private void button1_Click(object sender, EventArgs e) { if (checkAdobeReader() == true) { MessageBox.Show("有安裝 Adobe Reader "); } else { MessageBox.Show("沒有安裝 Adobe Reader "); } } /// <summary> /// 確認是否有安裝 Adobe Reader /// </

  • C#超实用代码段合集

    本文实例总结了几个非常实用的C#代码.分享给大家供大家参考,具体如下: 1. C#在托盘显示图标 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace notifyI

  • C#实现开机自动启动设置代码分享

    /// <summary> /// 设置程序开机启动 /// 或取消开机启动 /// </summary> /// <param name="started">设置开机启动,或者取消开机启动</param> /// <param name="exeName">注册表中程序的名字</param> /// <param name="path">开机启动的程序路径<

  • C#代码性能测试类(简单实用)

    介绍: 可以很方便的在代码里循环执行 需要测试的函数  自动统计出执行时间,支持多线程. 使用方法: PerformanceTest p = new PerformanceTest(); p.SetCount(10);//循环次数(默认:1) p.SetIsMultithread(true);//是否启动多线程测试 (默认:false) p.Execute( i => { //需要测试的代码 Response.Write(i+"<br>"); System.Threa

  • C#代码实现PDF文档操作类

    本文纯干货,贴上PDF文档操作类C#代码,需要添加iTextSharp.dll引用才可以正常通过编译. 废话不多说了,直接给大家贴代码了. 代码如下: using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace DotNet.Utilities { /// <summary> /// PDF文档操作类 /// </summary> //---------------------------

  • C#实现的json序列化和反序列化代码实例

    using System; using System.Collections.Generic; using System.Web.Script.Serialization; using System.Configuration; using System.Runtime.Serialization.Json; using System.Runtime.Serialization; using System.IO; using System.Text; namespace WebApplicati

  • C#获取网页源代码的方法

    本文实例讲述了C#获取网页源代码的方法.分享给大家供大家参考.具体如下: public string GetPageHTML(string url) { try { HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest; wr.Method = "get"; wr.Accept = "*/*"; wr.Headers.Add("Accept-Language: zh-cn");

  • 10个C#程序员经常用到的实用代码片段

    1 读取操作系统和CLR的版本 OperatingSystem os = System.Environment.OSVersion; Console.WriteLine("Platform: {0}", os.Platform); Console.WriteLine("Service Pack: {0}", os.ServicePack); Console.WriteLine("Version: {0}", os.Version); Consol

  • C#之IO读写文件方法封装代码

    具体不做详细介绍了,直接上代码 /// <summary> /// 功能:FileStream文件流读取文件 /// </summary> /// <param name="filePath">参数:文件路径</param> /// <returns>返回值:StreamReader对象</returns> public static StreamReader ReadFileByFs(string filePat

随机推荐