C# 操作 Word 小例

來源:互聯網
上載者:User

標籤:style   os   使用   io   ar   for   art   cti   代碼   

(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 小例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.