zoukankan      html  css  js  c++  java
  • Asp.net下C#调用Word模版实例

    1,配置环境;将你要访问的文件夹的权限添加账户,这个账户是你在IIS配置的连接账户.也就是保证客户端访问服务器时使用的账户对你要操作的文件夹有足够的权限.建议将IIS默认的账户改为管理员账户.
     
    2,思路:
    在服务器端访问本地存在的Word文件,并根据他新建一个文件,利用Word的标签定位传值.客户端浏览器访问的就是这个文件.下次当用户再次点击打印按钮的时候删除原来的Word文件,保证服务器不会存在过多文件而产生负担.
     
      具体代码如下:
      private void Button1_Click(object sender, System.EventArgs e)
      {
      
         //查询数据库是否有已经存在的Word文件,如果有取出其最大Num值建立新文件名并删除原来文件
       string sql="select * from hse_user_doc where hud_name='"+this.UserID+"'and hud_sort='B'";
       string NewName="";
       string OldName="";
       int num=0;
       using( DataSet dataset = SqlHelper.ExecuteDataset( gen.conn, CommandType.Text,sql) )
       {
        DataTable table=dataset.Tables[0];
        foreach(DataRow dr in table.Rows)
        {
         if(Int32.Parse(dr["hud_num"].ToString()) > num)
         {
          num=Int32.Parse(dr["hud_num"].ToString());
         }
         try
         {
          OldName=@"\hseoa\PrintPages\"+"bwl"+this.UserID+dr["hud_num"]+".doc";
          File.Delete(Server.MapPath(OldName));
          string sqlDelete="delete from hse_user_doc where hud_name='"+this.UserID+"' and hud_num='"+num+"' and hud_sort='B'";
          SqlHelper.ExecuteNonQuery(gen.conn,CommandType.Text,sqlDelete);
         }
         catch( Exception o)
         {
          throw ( o );
         }
         
        }
       }
       num++;
       NewName=@"\hseoa\PrintPages\"+"bwl"+this.UserID+num.ToString()+".doc";
       
       //建立新的Word文件
       FillWordTemp(Server.MapPath(@"\hseoa\PrintPages\备忘录.doc").Replace("'","'+char(39)+'"),Server.MapPath(NewName).Replace("'","'+char(39)+'"));
              
      
       //在数据库中插入新的文件记录
          string sqlInsert="insert hse_user_doc (hud_name,hud_num,hud_sort) values('"+this.UserID+"','"+num+"','B')";
       SqlHelper.ExecuteNonQuery(gen.conn,CommandType.Text,sqlInsert);
     
       //定义在浏览器中打开的文件名
       string Name="/hseoa/PrintPages/"+"bwl"+this.UserID+num.ToString()+".doc";
       string strPage =  "<script>window.open('"+Name+"')</script>" ;
       Page.RegisterStartupScript("",strPage);
           
      }
     
     
    //以下是上面用到的函数
     
     
     public void SaveAs(string strFileName )
      {
       object missing = Type.Missing;
       object fileName = strFileName;
       WordDoc.SaveAs(ref fileName, 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);
      } 
     

      //定义一个Word.Application 对象
      public void activeWordApp()
      {
       WordApp = new Word.ApplicationClass();
      }
      
      public void Quit( )
      {
       object missing = System.Reflection.Missing.Value;
       WordApp.Application.Quit(ref missing, ref missing, ref missing); 
       
      } 
       
     

      //基于模版新建Word文件
      public void OpenTempelte(string strTemppath)
      {
       object Missing = Type.Missing;
       //object Missing = System.Reflection.Missing.Value;
       
       activeWordApp();
       WordApp.Visible = false;
     
       object oTemplate = (object)strTemppath;
       try
       {
        WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing,ref Missing, ref Missing);
        WordDoc.Activate();
       }
       catch(Exception Ex)
       {
        throw new Exception(Ex.Message);
       }
       
       
      }
     
     
     
      public void FillWordTemp(string tempName, string saveAsFileName)
      {
         
        //打开Word模版
       OpenTempelte(tempName);
       //对标签"Name"进行填充,即函件题目项
       object bkmC="Name";
       if(WordApp.ActiveDocument.Bookmarks.Exists("Name") == true)
       {
        WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
        
       } 
       WordApp.Selection.TypeText(this.tx_title.Text);
       //对标签"Content"进行填充,即内容项
       string strContent=this.tx_content.Text;
       object content="Content";
       if(WordApp.ActiveDocument.Bookmarks.Exists("Content") == true)
       {
        WordApp.ActiveDocument.Bookmarks.get_Item(ref content).Select();
        
       }  
       WordApp.Selection.TypeText(strContent);
      
       SaveAs(saveAsFileName);
       Quit();
        
      }
  • 相关阅读:
    Codeforces 1485C Floor and Mod (枚举)
    CodeForces 1195D Submarine in the Rybinsk Sea (算贡献)
    CodeForces 1195C Basketball Exercise (线性DP)
    2021年初寒假训练第24场 B. 庆功会(搜索)
    任务分配(dp)
    开发工具的异常现象
    Telink MESH SDK 如何使用PWM
    Telink BLE MESH PWM波的小结
    [LeetCode] 1586. Binary Search Tree Iterator II
    [LeetCode] 1288. Remove Covered Intervals
  • 原文地址:https://www.cnblogs.com/catvi/p/1953015.html
Copyright © 2011-2022 走看看