C#对Word文档的部分操作笔记
C#对word文档的部分操作,主要是在Web服务中对word进行的操作:
使用的Microsoft.Office.Interop.Word.dll (12版)对Office2003进行的一些操作。
在进行word文档操作前先使用:
using Microsoft.Office.Interop.Word;
定义下这几个变量:
ublic Application m_App = null; //表示一个word应用程序 public Document m_Doc = null; //表示一个word文档 private object m_NullObj = System.Reflection.Missing.Value;//参数
打开一个word文档:
/// <summary> /// 打开一个word文档,输入文件完整路径,成功返回true /// </summary> /// <returns>bool</returns> public bool OpenWordDoc(string path) { try { m_App = new Application(); object fileObj = path; //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了) m_Doc = m_App.Documents.Open( ref fileObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj, ref m_NullObj); return true; } catch (System.Exception ex) { if (m_Doc != null) { WdSaveOptions saveChange = WdSaveOptions.wdDoNotSaveChanges; //关闭文件 不保存 m_Doc.Close(saveChange, ref m_NullObj, ref m_NullObj); } if (m_App != null) { //关闭COM m_App.Quit(ref m_NullObj, ref m_NullObj, ref m_NullObj); } return false; } }
关闭word文档:
public bool CloseWordDoc() { try { WdSaveOptions saveChange = WdSaveOptions.wdDoNotSaveChanges; //关闭文件 不保存 m_Doc.Close(saveChange, ref m_NullObj, ref m_NullObj); //关闭COM m_App.Quit(ref m_NullObj, ref m_NullObj, ref m_NullObj); return true; } catch (System.Exception ex) { return false; } }
获取文档内容:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 获取word文档的文本内容 /// </summary> /// <returns></returns> public string GetContent() { string ConText = m_Doc.Content.Text; return ConText; }
获取word文档的中总页面数:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 获取word文档的中总页面数 /// </summary> /// <returns></returns> public int GetPagesCount() { WdStatistic stat = WdStatistic.wdStatisticPages; int num = m_Doc.ComputeStatistics(stat, ref m_NullObj); return num; }
生成并获取文档摘要:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 生成并获取文档摘要 /// </summary> /// <returns></returns> public string GetSummary() { string OutText = null; object Length = "25%"; object Mode = WdSummaryMode.wdSummaryModeInsert; bool typ = true; Range Rg = m_Doc.AutoSummarize(20, Mode, true); OutText = Rg.Text; if (OutText == null) { return "null"; } else { return OutText; } }
获取文档每一页的内容:(在这里尤其是需要注意在获取最后一页内容时的方法)
PageText即为每一页的内容
object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToNext; object m_NullObj = System.Reflection.Missing.Value; object start = null; object end = null; object PageN = 1; //PagesCount 为文档页面总数
start = WDoc.m_App.Selection.GoTo(ref What, ref Which, ref m_NullObj, ref PageN).Start;
for (int i = 1; i <= PagesCount; i++) { string PageText = "";
//获取文档的每一页的内容 //最后一页的获取方法 if (i==PagesCount) { PageN = i; start = WDoc.m_App.Selection.GoTo(ref What, ref Which, ref m_NullObj, ref PageN).Start; end = WDoc.m_App.Selection.EndKey(Microsoft.Office.Interop.Word.WdUnits.wdStory, Microsoft.Office.Interop.Word.WdMovementType.wdExtend); PageText = WDoc.m_App.Selection.Range.Text; } else { end = WDoc.m_Doc.ActiveWindow.Selection.GoToNext(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage).Start; Microsoft.Office.Interop.Word.Range Rg = WDoc.m_Doc.Range(ref start, ref end); PageText = Rg.Text; start = end; } }