zoukankan      html  css  js  c++  java
  • 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)

    我在现在的项目中,用户需要打印办文单,一开始做的时候,就用是用表格,按照他们的格式划了一个,可是,打出来的东西总是和他们自己的不一样,(一句话,不好看),我也头疼一阶段没什么好的办法,后来想,干脆把他们word模板拿过来,自己来替换.刚开始的时候,不知道怎么做问了,google找了,但没有一样的满意的.后来还是在msdn上找了一点点,然后请教了同事.在总算搞定.效果还不错.

    整个过程是这样的:(本人用的是asp.net+C#)
    1.
        首先需要将word的dll引入进来,如果装了word的话,会在他的安装目录下面有一个MSWORD9.OLB文件(通过添加引用即可)这里需要注意的是上面这个文件,可能会因为office的版本不一样,文件名有所不同,而且在下面的open和save方法的参数也会因为版本的不同而不同,office2003中的open方面的参数好象是16个,而2000里的参数大概只有12个,调用的时候一定要注意
    2.
    要在webconfig文件里面加上一句:   <identity impersonate="true"/> 主要是模拟身份的吧,如果不加的话,程序运行的时候会报出拒绝访问的错误的.(而且你需要预先做好一个带书签的word模板)
    3.
        新建立一个也面,在面上部加如using Word;
        下面就是具体的函数了:(我这里的函数没有整理过,可能有些没用)
    打开文件:
     private Word.Document OpenDoc(string strDocPath,ref Word.Application WordApp,int flag)
      {
       if (!File.Exists(strDocPath))
        return null;
       object fileName = (object)strDocPath;   
       object isVisible = missing;
       object readOnly = missing;
       //Make word visible, so you can see what's happening
       WordApp.Visible = false;
       //通过open创建一个Word.Document的实例
       Word.Document doc = null;
       try
       {
        //doc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing,ref missing);
        doc = WordApp.Documents.Open(ref fileName, ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing);
        //if (flag == 1)
         //ListStyle(doc);
        return doc;
       }
       catch(Exception Ex)
       {
        throw new Exception(Ex.Message);
        return null;
       } 
      }

    替换模板内容:
    string strWordTemplate = Server.MapPath("../test/办文单.doc"); //这里是你的模板文件
       Word.Application WordApp = new Word.ApplicationClass();  //  定义一个Word.Application 对象
       Word.Document doc = OpenDoc(strWordTemplate,ref WordApp,1); //定义一个Word.Document 对象
       try
       {
    //下面是从数据库取数据,不好意思这个代码有点烂
        DataTable TempTable=this.CreateTable("Select * from Workflow_BW where AppID="+Convert.ToInt32(AppID)+" and ContentID="+Convert.ToInt32(ContentID));  
        if(TempTable.Rows.Count>0)
        {
         string TempTime=TempTable.Rows[0]["SWTime"].ToString().Trim();
         int Pos=TempTime.IndexOf(" ");
         string All=TempTime.Substring(0,Pos);
         int Pre=All.IndexOf("-");
         int Next=All.LastIndexOf("-");
         string Year=All.Substring(0,Pre).Trim();
         string Month=All.Substring(Pre+1,Next-Pre-1).Trim();
         string Day=All.Substring(Next+1,All.Length-Next-1).Trim();
         foreach(Word.Bookmark BM in doc.Bookmarks)  //这是最关键的地方:对文档的所有书签进行便利匹配
         {
          switch(BM.Name)
          {
           case "Advice": //替换Advice书签的内容,其他一样
            BM.Select();
            BM.Range.Text=this.CreateTable("Select Advice from Workflow_Advice where AppID="+Convert.ToInt32(AppID)+" and ContentID="+Convert.ToInt32(this.ContentID)+" and StepID=1").Rows[0]["Advice"].ToString().Trim();
            break;
           case "Day":
            BM.Select();
            BM.Range.Text=Day;
            break;
           case "LWDW":
            BM.Select();
            BM.Range.Text=TempTable.Rows[0]["LWDW"].ToString().Trim();
            break;
           case "LWH":
            BM.Select();
            BM.Range.Text=TempTable.Rows[0]["SWH"].ToString().Trim();
            break;
           case "Month":
            BM.Select();
            BM.Range.Text= Month;
            break;
           case "NowYear":
            BM.Select();
            BM.Range.Text=Year;
            break;
           case "Subject":
            BM.Select();
            BM.Range.Text=TempTable.Rows[0]["Subject"].ToString().Trim();
            break;
           case "SWH":
            BM.Select();
            BM.Range.Text=TempTable.Rows[0]["LSH"].ToString().Trim().Substring(4,TempTable.Rows[0]["LSH"].ToString().Trim().Length-4);
            break;
          }         
         }
        }
        object fn = (object)Server.MapPath("../test/temp.doc");    
        doc.SaveAs(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing); //这里是另存为一个一个文件
        Response.Redirect("../test/temp.doc"); //直接打开用ie打开另存的文件,然后可直接调用ie里的打印功能
        //doc.SaveAs(ref fn, 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);    
       }
       catch(Exception err)
       {
        this.tbXml.Text = err.ToString();
       }
       finally
       {
        doc.Close(ref missing,ref missing,ref missing); 
        WordApp.Quit(ref missing,ref missing,ref missing);
        WordApp = null; 
       }
      }

    这里面一个最主要的问题, doc.Close(ref missing,ref missing,ref missing); 
        WordApp.Quit(ref missing,ref missing,ref missing);
        WordApp = null; 
    这个代码好象不起作用,每次关闭打印的时候都会抱word错误,而且进程里面winword.exe也没关,不知道怎么回事.
    关于word里面的一些对象如:Application document Selection Range BookMark对象,本人也不是很熟悉,请参考MSDN上的帮助,那里面讲的很详细.希望对大家有所帮助

  • 相关阅读:
    MySQL日志设置及查看方法
    delphi 程序强制结束自身(两种方法都暴力)
    BAT-把当前用户以管理员权限运行(用户帐户控制:用于内置管理员帐户的管理员批准模式)
    SetWindowLong
    修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot – please use a kernel appropriate for your CPU"(安装深度Linux的时候就需要)
    国家网络与信息安全中心紧急通报:请升级安装补丁!
    Web Api 2(Cors)Ajax跨域访问
    理解依赖注入
    www.centos.org
    VMware Player安装centos
  • 原文地址:https://www.cnblogs.com/luluping/p/1908967.html
Copyright © 2011-2022 走看看