首页 > 代码库 > C# 操作 Word 小例

C# 操作 Word 小例

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

(2)如何插入表格
使用Word的Table类,有人还使用DataTable类进行辅助
代码:
        public void AddTitle(string s)
        {
            //Word段落
            Word.Paragraph p;
            p = oDoc.Content.Paragraphs.Add(ref missing);
            //设置段落中的内容文本
            p.Range.Text = s;
            //设置为一号标题
            object style = Word.WdBuiltinStyle.wdStyleHeading1;
            p.set_Style(ref style);
            //添加到末尾
            p.Range.InsertParagraphAfter();  //在应用 InsertParagraphAfter 方法之后,所选内容将扩展至包括新段落。
        }
        /// <summary>
        /// 添加普通段落
        /// </summary>
        /// <param name="s"></param>
        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();
        }

(3)如何插入图片
InlineShapes是Word中内嵌的图形等资源
代码:
public void InsertImage(string strPicPath, float picWidth, float picHeight)
        {
            string FileName = strPicPath;
            object LinkToFile = false;
            object SaveWithDocument = true;
            object Anchor = oWord.Selection.Range;
            oWord.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor).Select();
            oWord.Selection.InlineShapes[1].Width = picWidth; // 图片宽度
            oWord.Selection.InlineShapes[1].Height = picHeight; // 图片高度
        }

? 插入图片后为什么又没了?
这可能是由于你在插入图片后,又插入东西,但是你没有移动光标,所以把图片给覆盖掉了。
解决方法:光标移动

(4)光标移动
A:标签:
系统预定义标签:object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined bookmark 系统预定义的书签?*/
自定义标签:

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);
代码:
// Go to a predefined bookmark, if the bookmark doesn‘t exists the application will raise an error
        public void GotoBookMark(string strBookMarkName)
        {
            // VB :  Selection.GoTo What:=wdGoToBookmark, Name:="nome"
            object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
            object NameBookMark = strBookMarkName;
            oWord.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
        }       
        public void GoToTheEnd()
        {
            // VB :  Selection.EndKey Unit:=wdStory
            object unit;
            unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            oWord.Selection.EndKey(ref unit, ref missing);
        }
        public void GoToTheBeginning()
        {
            // VB : Selection.HomeKey Unit:=wdStory
            object unit;
            unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            oWord.Selection.HomeKey(ref unit, ref missing);
        }

(5)生成目录
代码:
public void insertContent() //利用标题样式生成目录
        {
            GoToTheBeginning();
            object start = 0;
            object end = 0;
            Word.Range myRange = oWord.ActiveDocument.Range(ref start, ref end); //位置区域
            object useHeadingStyle = true; //使用Head样式
            object upperHeadingLevel = 1;  //最大一级
            object lowerHeadingLevel = 3;  //最小三级
            object useHypeLinks = true;
            //TablesOfContents的Add方法添加目录
            oDoc.TablesOfContents.Add(myRange, ref useHeadingStyle,
                ref upperHeadingLevel, ref lowerHeadingLevel,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref useHypeLinks, ref missing, ref missing);
            oDoc.TablesOfContents[1].UpdatePageNumbers(); //更新页码
        }
        #endregion

(6)目录格式怎么设置?比如加粗、倾斜等
利用段落格式设置
代码:
public void formatContent() {
            Word.TableOfContents myContent = oDoc.TablesOfContents[1]; //目录
            Word.Paragraphs myParagraphs = myContent.Range.Paragraphs; //目录里的所有段,一行一段
            int[] FirstParaArray = new int[3]{ 1, 8, 9 }; //一级标题,直接指定
            foreach (int i in FirstParaArray) {
                myParagraphs[i].Range.Font.Bold = 1;  //加粗
                myParagraphs[i].Range.Font.Name = "黑体"; //字体
                myParagraphs[i].Range.Font.Size = 12; //小四
                myParagraphs[i].Range.ParagraphFormat.SpaceBefore = 6; //段前
                myParagraphs[i].Range.ParagraphFormat.SpaceAfter = 6; //段后间距
            }
}

C# 操作 Word 小例