·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> office编程必不可少[转]

office编程必不可少[转]

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

1. 微软官方实例:

段落、表格、图表

HOW TO:利用 Visual C# .NET 使 Word 自动新建文档

 

2. 学习资源

(1)Word in the Office 基础知识,必读,下面的总结里有内容摘要

http://msdn.microsoft.com/en-us/library/Aa201330

网友翻译版:http://blog.csdn.net/hustliangchen/archive/2011/01/05/6118459.aspx

(2)Word类的结构图,application、document、Range、Selection等,必读

http://msdn.microsoft.com/en-us/library/aa164012

(3)word 2007 对象模型 :

http://msdn.microsoft.com/en-us/library/bb244515(v=office.12).aspx

(4)Microsoft.Office.Interop.Word

http://msdn.microsoft.com/zh-cn/library/ms254954(v=Office.11).aspx

(5)wps 二次开发接口文档 wpsapi.chm

中文方便阅读,CSDN下载

(6)飞蛾 Word VBA 参考教程

全中文Word类库,必读

http://www.feiesoft.com/vba/word/

(7)用VBA宏提高Word操作效率——需要精研的20个经典实例

http://pcmag.hexun.com/2010-03-18/123031946.html

 

3.一些总结

(1)Document 代表一个具体的word文档, Documents 是 Document的集合,用index索引来表示某一个document。ActiveDocument属性是当前焦点(focus)的document。我们一般不会用索引来引用文档,因为索引值会随着文档的打开和关闭而改变;通常我们用 ActiveDocument属性,或者 Documents 集合的Add 或 Open方法返回的document对象变量来引用。Add或Open的document会成为 ActiveDocument,如果想使其它document成为activeDocument,则使用 document对象的ActiveDocument方法。

用文件名指明具体Documnet,Documents("Report.doc").Activate();

 

(2) characters组成words,words组成sentences,sentences组成paragraphs,因此一个document中会包含这样四个集合: Characters Words, Sentences ,Paragraphs collection。此外,document还可能包含sections的集合,而一个section又会有HeadersFooters 页眉页脚集合。

 

(3)Paragraph段落,由一个段落标志和所有文本组成。拷贝段落时如果包含了段落标志,则段落格式也会一同拷贝。不想拷贝格式的话就不要拷贝段落标志。

 

(3)Range对象,代表一块连续的由起始字符和结束字符定义的区域,可以小到只有一个插入光标或大至整个文档内容,它也可以是但并不必须是当前selection代表的区域。可以在一个文档中定义多个Range对象。

我们通常用Range类定义一个变量来创建Range对象,然后用Document的Range方法或其它对象的Range属性来实例化这个Range对象。

 

(4)Selection对象

可代表光标

该对象代表窗口或窗格中的当前所选内容。所选内容代表文档中被选定(或突出显示的)的区域,若文档中没有所选内容,则代表插入点。每个文档窗格只能有一个活动的 Selection对象,并且整个应用程序中只能有一个活动的 Selection对象。
使用 Selection对象
使用Selection属性可返回 Selection对象。如果没有使用 Selection属性的对象识别符,Word 将返回活动文档窗口的活动窗格中的所选内容。

Text属性是其选中的内容

Copy、Cut、Paste方法进行复制、剪切、粘贴等操作

 

(5)sections

Sections.Add 方法

该方法用于返回一个 Section 对象,该对象表示添加至文档中的新节。
Function Add([Range As Range = 0],  [Start As WpsSectionStart = 2]) As Section

参数说明
Range    Variant 类型,可选。在其之前插入分节符的区域。如果忽略本参数,则将分节符插至文档末尾。
Start    Variant 类型,可选。要添加的分节符类型。WpsSectionStart 类型。如果忽略本参数,则添加“下一页”类型的分节符。
    WpsSectionStart 类型可以是下列常量之一:
    值     描述
    wpsSectionContinuous     连续分节符
    wpsSectionEvenPage     偶数页分节符
    wpsSectionNewColumn     节的结尾
    wpsSectionNewPage     下一页分节符(默认)
    wpsSectionOddPage     奇数页分节符

Sections 参考MSDN

Section  参考MSDN

 

4. 具体使用

(1)如何设置标题样式,“标题一”,“标题二”等 参考

[c-sharp] view plaincopyPRint?
  1. public void AddTitle(string s)  
  2. {  
  3.     //Word段落   
  4.     Word.Paragraph p;  
  5.     p = oDoc.Content.Paragraphs.Add(ref missing);  
  6.     //设置段落中的内容文本   
  7.     p.Range.Text = s;  
  8.     //设置为一号标题   
  9.     object style = Word.WdBuiltinStyle.wdStyleHeading1;  
  10.     p.set_Style(ref style);  
  11.     //添加到末尾   
  12.     p.Range.InsertParagraphAfter();  //在应用 InsertParagraphAfter 方法之后,所选内容将扩展至包括新段落。   
  13. }  
  14. ///    
  15. /// 添加普通段落   
  16. ///    
  17. ///    
  18. public void AddParagraph(string s)  
  19. {  
  20.     Word.Paragraph p;  
  21.     p = oDoc.Content.Paragraphs.Add(ref missing);  
  22.     p.Range.Text = s;  
  23.     object style = Word.WdBuiltinStyle.wdStyleBodyText;  
  24.     p.set_Style(ref style);  
  25.     p.Range.InsertParagraphAfter();  
  26. }  

/// 添加普通段落 /// /// public void AddParagraph(string s) { Word.Paragraph p; p = oDoc.Content.Paragraphs.Add(ref missing); p.Range.Text = s; object style = Word.WdBuiltinStyle.wdStyleBodyText; p.set_Style(ref style); p.Range.InsertParagraphAfter(); }

 

(2)如何插入表格

使用Word的Table类,有人还使用DataTable类进行辅助

 

(3)如何插入图片

InlineShapes是Word中内嵌的图形等资源

[c-sharp] view plaincopyprint?
  1. public void InsertImage(string strPicPath, float picWidth, float picHeight)  
  2.         {  
  3.             string FileName = strPicPath;  
  4.             object LinkToFile = false;  
  5.             object SaveWithDocument = true;  
  6.             object Anchor = oWord.Selection.Range;  
  7.             oWord.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor).Select();  
  8.             oWord.Selection.InlineShapes[1].Width = picWidth; // 图片宽度    
  9.             oWord.Selection.InlineShapes[1].Height = picHeight; // 图片高度   
  10.         }  

 

? 插入图片后为什么又没了?

这可能是由于你在插入图片后,又插入东西,但是你没有移动光标,所以把图片给覆盖掉了。

解决方法:光标移动

 

(4)光标移动

A:标签:

系统预定义标签:object oEndOfDoc = "//endofdoc";

自定义标签:

B:利用标签获取位置

Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

插入段落、表格时都会用到这个位置:

oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);

oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);

[c-sharp] view plaincopyprint?
  1. // Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error   
  2.         public void GotoBookMark(string strBookMarkName)  
  3.         {  
  4.             // VB :  Selection.GoTo What:=wdGoToBookmark, Name:="nome"   
  5.             object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;  
  6.             object NameBookMark = strBookMarkName;  
  7.             oWord.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);  
  8.         }          
  9.         public void GoToTheEnd()  
  10.         {  
  11.             // VB :  Selection.EndKey Unit:=wdStory   
  12.             object unit;  
  13.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  14.             oWord.Selection.EndKey(ref unit, ref missing);  
  15.         }  
  16.         public void GoToTheBeginning()  
  17.         {  
  18.             // VB : Selection.HomeKey Unit:=wdStory   
  19.             object unit;  
  20.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  21.             oWord.Selection.HomeKey(ref unit, ref missing);  
  22.         }  

 

(5)生成目录

[c-sharp] view plaincopyprint?
  1. public void insertContent() //利用标题样式生成目录   
  2.         {  
  3.             GoToTheBeginning();  
  4.             object start = 0;  
  5.             object end = 0;  
  6.             Word.Range myRange = oWord.ActiveDocument.Range(ref start, ref end); //位置区域   
  7.             object useHeadingStyle = true; //使用Head样式   
  8.             object upperHeadingLevel = 1;  //最大一级   
  9.             object lowerHeadingLevel = 3;  //最小三级   
  10.             object useHypeLinks = true;  
  11.             //TablesOfContents的Add方法添加目录   
  12.             oDoc.TablesOfContents.Add(myRange, ref useHeadingStyle,  
  13.                 ref upperHeadingLevel, ref lowerHeadingLevel,  
  14.                 ref missing, ref missing, ref missing, ref missing,  
  15.                 ref missing, ref useHypeLinks, ref missing, ref missing);  
  16.             oDoc.TablesOfContents[1].UpdatePageNumbers(); //更新页码   
  17.         }  
  18.         #endregion  

 

(6)目录格式怎么设置?比如加粗、倾斜等

利用段落格式设置

[c-sharp] view plaincopyprint?
    1. public void formatContent() {  
    2.             Word.TableOfContents myContent = oDoc.TablesOfContents[1]; //目录   
    3.             Word.Paragraphs myParagraphs = myContent.Range.Paragraphs; //目录里的所有段,一行一段   
    4.             int[] FirstParaArray = new int[3]{ 1, 8, 9 }; //一级标题,直接指定   
    5.             foreach (int i in FirstParaArray) {  
    6.                 myParagraphs[i].Range.Font.Bold = 1;  //加粗   
    7.                 myParagraphs[i].Range.Font.Name = "黑体"; //字体   
    8.                 myParagraphs[i].Range.Font.Size = 12; //小四   
    9.                 myParagraphs[i].Range.ParagraphFormat.SpaceBefore = 6; //段前   
    10.                 myParagraphs[i].Range.ParagraphFormat.SpaceAfter = 6; //段后间距   
    11.             }