小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

HOW TO:利用 Visual C# .NET 使 Word 自動(dòng)新建文檔

 sirianyan 2010-09-21
本文分步介紹如何利用 Visual C# .NET 的自動(dòng)化功能在 Word 中創(chuàng)建新文檔。

代碼示例

本文中的代碼示例將說(shuō)明如何完成以下任務(wù):
  • 插入包含文本和格式的段落。
  • 瀏覽和修改文檔中的不同范圍。
  • 插入表格、設(shè)置表格格式并在表格中填充數(shù)據(jù)。
  • 添加圖表。
要利用 Visual C# .NET 的自動(dòng)化功能創(chuàng)建新的 Word 文檔,請(qǐng)執(zhí)行以下步驟:
  1. 啟動(dòng) Microsoft Visual Studio .NET。在文件菜單上,單擊新建,然后單擊項(xiàng)目。在項(xiàng)目類(lèi)型下,單擊 Visual C# 項(xiàng)目,然后單擊模板下的 Windows 應(yīng)用程序。默認(rèn)情況下會(huì)創(chuàng)建 Form1。
  2. 添加對(duì) Microsoft Word 對(duì)象庫(kù)的引用。為此,請(qǐng)按照下列步驟操作:
    1. 項(xiàng)目菜單上,單擊添加引用。
    2. COM 選項(xiàng)卡上,找到 Microsoft Word 對(duì)象庫(kù),然后單擊選擇
    3. 添加引用對(duì)話(huà)框中單擊確定,接受您的選擇。如果系統(tǒng)提示您為選定的庫(kù)生成包裝,請(qǐng)單擊
  3. 視圖菜單上,選擇工具箱以顯示工具箱,然后向 Form1 添加一個(gè)按鈕。
  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. 滾動(dòng)到代碼窗口的頂部。將下面的代碼行添加到 using 指令列表的末尾:
    using Word = Microsoft.Office.Interop.Word;
        using System.Reflection;
        
  7. 按 F5 鍵生成并運(yùn)行程序。
  8. 單擊 Button1,啟動(dòng) Word 自動(dòng)化功能并創(chuàng)建文檔。
代碼執(zhí)行完成后,檢查為您創(chuàng)建的文檔。該文檔包含兩頁(yè)設(shè)置了格式的段落、表格和圖表。

使用模板

如果您要使用自動(dòng)化功能創(chuàng)建的文檔都是通用格式,則利用基于預(yù)設(shè)格式的模板的新文檔來(lái)開(kāi)始創(chuàng)建過(guò)程會(huì)更加容易。與從頭創(chuàng)建文檔相比,將某個(gè)模板與 Word 自動(dòng)化客戶(hù)端配合使用有兩大優(yōu)點(diǎn):
  • 您可以對(duì)整個(gè)文檔中的對(duì)象的格式設(shè)置和布局施加更多控制。
  • 可以使用較少的代碼創(chuàng)建文檔。
通過(guò)使用模板,可以精確地調(diào)整表格、段落和其他對(duì)象在文檔中的布局,并可為這些對(duì)象添加格式設(shè)置。通過(guò)使用自動(dòng)化功能,可以基于包含下面這樣的代碼的模板創(chuàng)建新文檔:
object oTemplate = "c:\\MyTemplate.dot";
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);
在模板中,可以定義書(shū)簽,這樣,自動(dòng)化客戶(hù)端就可以在文檔的特定位置加入可變文本,如下所示:
object oBookMark = "MyBookmark";
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
使用模板的另一個(gè)優(yōu)點(diǎn)在于,您可以創(chuàng)建和存儲(chǔ)希望在運(yùn)行時(shí)應(yīng)用的格式樣式,如下所示:
object oStyleName = "MyStyle";
oDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
- 或者 -
object oStyleName = "MyStyle";
oWord.Selection.set_Style(ref oStyleName);

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多