winsex

大家都来DOTNET
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c#操作word的例子1

Posted on 2005-11-24 11:03  浪地  阅读(1280)  评论(0编辑  收藏  举报
Word.ApplicationClass word = new Word.ApplicationClass();
   Type wordType = word.GetType(); 
   Word.Documents docs = word.Documents; 
   // 打开文件  
   Type docsType = docs.GetType(); 
   object filename = "c:\\";  
   Word.Document doc = (Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {filename, true, true});
   // 转换格式,另存为 
   Type docType = doc.GetType(); 
   object saveFileName = Server.MapPath("../") + "OfficeFile\\HTML\\"+ "aa.html";
   //下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成: 
   //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
   ///其它格式:
   ///wdFormatHTML 
   ///wdFormatDocument 
   ///wdFormatDOSText
   ///wdFormatDOSTextLineBreaks
   ///wdFormatEncodedText
   ///wdFormatRTF
   ///wdFormatTemplate
   ///wdFormatText 
   ///wdFormatTextLineBreaks
   ///wdFormatUnicodeText
   docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML});
   // 退出 Word
   wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);