[C#]利用VSTO操作Office文档而无需安装Office
VSTO,就是Visual Studio Tools for the Microsoft Office System。可以在这里找到更多信息:
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打开指定的文档很简单。
// 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, 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();
/// 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);
记得调用Microsoft.Office.Interop.Word.Document.Activate()将当前打开的文档激活。
ConvertNumbersToText方法是用来把文档中所有的编号符号转换为文本的。
1.3. Word.Range选定文档范围还有Word.Range这个接口,可以选定某一段文字,按照指定的方式复制出来。
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;
无论发生了什么事情,都必须保证WinWord.exe实例被释放,这是一个服务的基本要求。
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;
}
郑昀 ☑移动数据业务 times;6年 ☑语义聚合 times;4年 ☑O2O times;5年的一个老兵。
Infecting-The-Enterprise-Abusing-Office365-Powershell-For-Covert-C2 立即下载
相关文章
- 四种Sandcastle方法生成c#.net帮助类帮助文档
- C#分析URL参数获取参数和值得对应列表(二)
- C#实现人脸识别【Users】
- Win10系列:C#应用控件基础8
- 《C#高级编程》学习笔记----c#内存管理--栈VS堆
- C# .NET Visual Studio VS2008如何显示行号
- C#快速随机按行读取大型文本文件
- C#实现office文档转换为PDF格式
- 重新整理数据结构与算法(c#)—— 二叉树排序树[二十二]
- c# aspose操作word文档
- C# Aop简单扫盲及ORM实体类属性拦截示例
- 在C#代码中应用Log4Net(五)将Log4Net正确地封装在自己的类库中并进行调用
- c# 反编译对比(旧)
- C# 对Word文档进行处理
- C# 对Excel文档打印时的页面设置
- C# 在根据窗体中的表格数据生成word文档时出错
- C# 对Word文档进行处理
- atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结
- C# 将字符串中的多个连续空格变成一个
- C#基础——字符串、数字之间的转换
- 经验36--C#无名(大事,物...)
- C#+OpenGL+FreeType显示3D文字(1) - 从TTF文件导出字形贴图