惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
小众软件
小众软件
O
OpenAI News
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
Hacker News: Ask HN
Hacker News: Ask HN
D
Docker
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Hacker News - Newest:
Hacker News - Newest: "LLM"
G
Google Developers Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
GRAHAM CLULEY
S
Schneier on Security
T
Tor Project blog
Spread Privacy
Spread Privacy
PCI Perspectives
PCI Perspectives
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Fortinet All Blogs
L
Lohrmann on Cybersecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
The Exploit Database - CXSecurity.com
TaoSecurity Blog
TaoSecurity Blog
Apple Machine Learning Research
Apple Machine Learning Research
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
罗磊的独立博客

博客园 - Candyxiaoqiang

使用 ADO.NET 访问 Oracle 9i 存储过程(转) 使用 ADO.NET 和 Oracle 进行高级数据访问 (转自) HttpRequest, HttpRuntime, AppDomain and friends() 在showModalDialog窗体里,单击服务端Button产生新页面解决方法 - Candyxiaoqiang - 博客园 asp.net按钮 button的onclick事件 与oncommand 事件的区别 汇总 SQL 连接字符串的说明(转) JavaScript相关资料( 收集中.......) - Candyxiaoqiang - 博客园 Visual Studio常用小技巧 觉得不错 转一下 Web开发中用的工具(不断更新) C#实现Web文件上传的两种方法 (转) document,event javascript中常用对象介绍 带中文说明的 (转) ASP.Net实现将Word转换PDF格式 (转自:思绪随风~~~) 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料) C# 拆分word(根据标题或书签拆分) (转自zrx401558287) 完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件 (转自zellzhang) 如何:将文档发送到打印机(转) 在IIS上启用Gzip压缩(HTTP压缩) (转自子秋的博客) 如何采用Local方式连接到ArcGIS Server(转) VS2005 直接调试网页不能显示
利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)(转)
Candyxiaoqiang · 2009-05-21 · via 博客园 - Candyxiaoqiang

hong

转自:

 
整个过程是这样的

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上的帮助,那里面讲的很详细.希望对大家有所帮助