国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
HOW TO:利用 Visual C# .NET 使 Word 自動新建文檔
本文分步介紹如何利用 Visual C# .NET 的自動化功能在 Word 中創(chuàng)建新文檔。

代碼示例

本文中的代碼示例將說明如何完成以下任務:
  • 插入包含文本和格式的段落。
  • 瀏覽和修改文檔中的不同范圍。
  • 插入表格、設(shè)置表格格式并在表格中填充數(shù)據(jù)。
  • 添加圖表。
要利用 Visual C# .NET 的自動化功能創(chuàng)建新的 Word 文檔,請執(zhí)行以下步驟:
  1. 啟動 Microsoft Visual Studio .NET。在文件菜單上,單擊新建,然后單擊項目。在項目類型下,單擊 Visual C# 項目,然后單擊模板下的 Windows 應用程序。默認情況下會創(chuàng)建 Form1。
  2. 添加對 Microsoft Word 對象庫的引用。為此,請按照下列步驟操作:
    1. 項目菜單上,單擊添加引用
    2. COM 選項卡上,找到 Microsoft Word 對象庫,然后單擊選擇
    3. 添加引用對話框中單擊確定,接受您的選擇。如果系統(tǒng)提示您為選定的庫生成包裝,請單擊
  3. 視圖菜單上,選擇工具箱以顯示工具箱,然后向 Form1 添加一個按鈕。
  4. 雙擊 Button1。出現(xiàn)該窗體的代碼窗口。
  5. 在代碼窗口中,將以下代碼
    private void button1_Click(object sender, System.EventArgs e)    {    }    
    替換為:
    private void button1_Click(object sender, System.EventArgs e)    {    object oMissing = System.Reflection.Missing.Value;    object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */    //Start Word and create a new document.    Word._Application oWord;    Word._Document oDoc;    oWord = new Word.Application();    oWord.Visible = true;    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,    ref oMissing, ref oMissing);    //Insert a paragraph at the beginning of the document.    Word.Paragraph oPara1;    oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);    oPara1.Range.Text = "Heading 1";    oPara1.Range.Font.Bold = 1;    oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.    oPara1.Range.InsertParagraphAfter();    //Insert a paragraph at the end of the document.    Word.Paragraph oPara2;    object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);    oPara2.Range.Text = "Heading 2";    oPara2.Format.SpaceAfter = 6;    oPara2.Range.InsertParagraphAfter();    //Insert another paragraph.    Word.Paragraph oPara3;    oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);    oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";    oPara3.Range.Font.Bold = 0;    oPara3.Format.SpaceAfter = 24;    oPara3.Range.InsertParagraphAfter();    //Insert a 3 x 5 table, fill it with data, and make the first row    //bold and italic.    Word.Table oTable;    Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);    oTable.Range.ParagraphFormat.SpaceAfter = 6;    int r, c;    string strText;    for(r = 1; r <= 3; r++)    for(c = 1; c <= 5; c++)    {    strText = "r" + r + "c" + c;    oTable.Cell(r, c).Range.Text = strText;    }    oTable.Rows[1].Range.Font.Bold = 1;    oTable.Rows[1].Range.Font.Italic = 1;    //Add some text after the table.    Word.Paragraph oPara4;    oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);    oPara4.Range.InsertParagraphBefore();    oPara4.Range.Text = "And here's another table:";    oPara4.Format.SpaceAfter = 24;    oPara4.Range.InsertParagraphAfter();    //Insert a 5 x 2 table, fill it with data, and change the column widths.    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);    oTable.Range.ParagraphFormat.SpaceAfter = 6;    for(r = 1; r <= 5; r++)    for(c = 1; c <= 2; c++)    {    strText = "r" + r + "c" + c;    oTable.Cell(r, c).Range.Text = strText;    }    oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2    oTable.Columns[2].Width = oWord.InchesToPoints(3);    //Keep inserting text. When you get to 7 inches from top of the    //document, insert a hard page break.    object oPos;    double dPos = oWord.InchesToPoints(7);    oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();    do    {    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    wrdRng.ParagraphFormat.SpaceAfter = 6;    wrdRng.InsertAfter("A line of text");    wrdRng.InsertParagraphAfter();    oPos = wrdRng.get_Information    (Word.WdInformation.wdVerticalPositionRelativeToPage);    }    while(dPos >= Convert.ToDouble(oPos));    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;    object oPageBreak = Word.WdBreakType.wdPageBreak;    wrdRng.Collapse(ref oCollapseEnd);    wrdRng.InsertBreak(ref oPageBreak);    wrdRng.Collapse(ref oCollapseEnd);    wrdRng.InsertAfter("We're now on page 2. Here's my chart:");    wrdRng.InsertParagraphAfter();    //Insert a chart.    Word.InlineShape oShape;    object oClassType = "MSGraph.Chart.8";    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,    ref oMissing, ref oMissing, ref oMissing,    ref oMissing, ref oMissing, ref oMissing);    //Demonstrate use of late bound oChart and oChartApp objects to    //manipulate the chart object with MSGraph.    object oChart;    object oChartApp;    oChart = oShape.OLEFormat.Object;    oChartApp = oChart.GetType().InvokeMember("Application",    BindingFlags.GetProperty, null, oChart, null);    //Change the chart type to Line.    object[] Parameters = new Object[1];    Parameters[0] = 4; //xlLine = 4    oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,    null, oChart, Parameters);    //Update the chart image and quit MSGraph.    oChartApp.GetType().InvokeMember("Update",    BindingFlags.InvokeMethod, null, oChartApp, null);    oChartApp.GetType().InvokeMember("Quit",    BindingFlags.InvokeMethod, null, oChartApp, null);    //... If desired, you can proceed from here using the Microsoft Graph    //Object model on the oChart and oChartApp objects to make additional    //changes to the chart.    //Set the width of the chart.    oShape.Width = oWord.InchesToPoints(6.25f);    oShape.Height = oWord.InchesToPoints(3.57f);    //Add text after the chart.    wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;    wrdRng.InsertParagraphAfter();    wrdRng.InsertAfter("THE END.");    //Close this form.    this.Close();    }    
  6. 滾動到代碼窗口的頂部。將下面的代碼行添加到 using 指令列表的末尾:
    using Word = Microsoft.Office.Interop.Word;    using System.Reflection;    
  7. 按 F5 鍵生成并運行程序。
  8. 單擊 Button1,啟動 Word 自動化功能并創(chuàng)建文檔。
代碼執(zhí)行完成后,檢查為您創(chuàng)建的文檔。該文檔包含兩頁設(shè)置了格式的段落、表格和圖表。

使用模板

如果您要使用自動化功能創(chuàng)建的文檔都是通用格式,則利用基于預設(shè)格式的模板的新文檔來開始創(chuàng)建過程會更加容易。與從頭創(chuàng)建文檔相比,將某個模板與 Word 自動化客戶端配合使用有兩大優(yōu)點:
  • 您可以對整個文檔中的對象的格式設(shè)置和布局施加更多控制。
  • 可以使用較少的代碼創(chuàng)建文檔。
通過使用模板,可以精確地調(diào)整表格、段落和其他對象在文檔中的布局,并可為這些對象添加格式設(shè)置。通過使用自動化功能,可以基于包含下面這樣的代碼的模板創(chuàng)建新文檔:
object oTemplate = "c:\\MyTemplate.dot";oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,ref oMissing, ref oMissing);
在模板中,可以定義書簽,這樣,自動化客戶端就可以在文檔的特定位置加入可變文本,如下所示:
object oBookMark = "MyBookmark";oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
使用模板的另一個優(yōu)點在于,您可以創(chuàng)建和存儲希望在運行時應用的格式樣式,如下所示:
object oStyleName = "MyStyle";oDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
- 或者 -
object oStyleName = "MyStyle";oWord.Selection.set_Style(ref oStyleName);
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#操作Word完全功略!
C#編程技巧:讀取Word的方法
關(guān)于用Word.Application 打開了多個word文檔時,出錯,說Normal.dotm正由另一程序占用或者用戶占用 解決方法
合并N個Word文檔
word批量轉(zhuǎn)pdf文件快捷方法。
利用VBA 處理表格 - Word - 辦公自動化 - 書部落-計算機編程圖書資料分享 電...
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服