[C#]利用VSTO操作Office文档而无需安装Office
2008-01-03 17:48
453 查看
[C#]利用VSTO操作Office文档而无需安装Office
编写者 | 日期 | 关键词 |
郑昀@ultrapower | 2005-8 | Vsto office c# Interop word |
1.1. VSTO
VSTO,就是Visual Studio Tools for the Microsoft Office System[/b]。可以在这里找到更多信息:http://msdn.microsoft.com/office/understanding/vsto/default.aspx
首先,必须在系统中安装VSTO。(不用安装Office即可使用)
为了使用VSTO,我们的工程需要引入如下引用:
图1-1 引用示范
其中指的是“Microsoft.Office.Interop.Word”,你可以通过下面的图样了解如何添加这个COM引用:
图1-2 添加Word引用
其中指的是“Microsoft Office 11.0 Object Library”,你可以通过下面的图样了解如何添加这个COM引用:
图1-3 添加Office.Core引用
1.2. Word.ApplicationClass打开文档
用Word打开指定的文档很简单。代码[/b] [/b] |
// a reference to Word application private Microsoft.Office.Interop.Word.ApplicationClass m_oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); // a reference to the document private Microsoft.Office.Interop.Word.Document m_oDoc; object fileName = strDocumentFilePath; m_oWordApp.Visible = false; m_oDoc = m_oWordApp.Documents.Open(ref fileName[/b], 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); m_oDoc.Activate(); [/b] /// http://msdn2.microsoft.com/library/wt26ady8(en-us,vs.80).aspx /// convert all list numbers and LISTNUM fields in the document to text object numberType = Microsoft.Office.Interop.Word.WdNumberType.wdNumberAllNumbers; m_oDoc.ConvertNumbersToText(ref numberType); |
ConvertNumbersToText方法是用来把文档中所有的编号符号转换为文本的。
1.3. Word.Range选定文档范围
还有Word.Range这个接口,可以选定某一段文字,按照指定的方式复制出来。代码[/b] [/b] |
object rangeStart = begin; object rangeEnd = (end < nCount)?end:nCount; Microsoft.Office.Interop.Word.Range rng = m_oDoc.Range(ref rangeStart, ref rangeEnd); rng.Select(); ///////////////////////////////////////////////////// /// Microsoft.Office.Interop.Word.TextRetrievalMode RetrievalMode = rng.FormattedText.TextRetrievalMode; RetrievalMode.IncludeHiddenText = false; RetrievalMode.IncludeFieldCodes = false; /// sets the view for text retrieval to Web view RetrievalMode.ViewType = Microsoft.Office.Interop.Word.WdViewType.wdWebView; /// ///////////////////////////////////////////////////// String strYourWord = rng.FormattedText.Text; |
1.4. 销毁一切
无论发生了什么事情,都必须保证WinWord.exe实例被释放,这是一个服务的基本要求。代码[/b] [/b] |
/// 关闭打开的文档: if(m_oDoc != null) { m_oDoc.Close(ref saveChanges, ref missing, ref missing); m_oDoc = null; } if(m_oWordApp != null) { // 这里就不要再判断if(m_oWordApp.Application.ActiveDocument != null)了 // 否则会出现“System.Runtime.InteropServices.COMException (0x800A1098): 因为没有打开的文档,所以这一命令无效。” // 这样的异常! m_oWordApp.Application.Quit(ref saveChanges, ref missing, ref missing); m_oWordApp = null; } |
编写者 | 日期 | 关键词 |
郑昀@ultrapower | 2005-8 | Vsto office c# Interop word |
相关文章推荐
- [C#]利用VSTO操作Office文档而无需安装Office
- [C#]利用VSTO操作Office文档而无需安装Office
- [C#]利用VSTO操作Office文档而无需安装Office
- DocX.dll(无需安装OFFICE操作word文档)
- C#的office文档操作(3)
- C#操作Word文档(Office 2007)
- 基于C#语言利用Microsoft.office.introp.excel操作Excel总结
- 关于C#操作EXCEL,生成图表的全面应用之二(利用Microsoft.Office.Interop.OWC11)
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- C#的office文档操作
- C#操作Word文档(Office 2007)
- 2012,我的C#全能Excel操作(无需Office,不使用XML)
- C#操作Word文档(Office 2007)
- 用C#操作office组件库实现word文档的创建及文字 表格 图片的插入
- C#操作Word文档(Office 2007)
- C#操作Word文档(Office 2007)
- C#中操作Word(5)—— 利用BookMark对象写Word文档
- 在asp.net中无需服务器安装word,利用xml生成word文档
- C#导入Excel到DataSet(客户端无需安装Office)
- C#的office文档操作(4)