zoukankan      html  css  js  c++  java
  • C#实现动态生成Word

    1. 一个控制台例子,实现动态生成Word。

    首先,添加引用:COM->Microsoft Word 11.0 Object Library。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Microsoft.Office.Interop.Word;
    using System.Reflection;
    
    namespace TestWord
    {
        class Program
        {
            static void Main(string[] args)
            {
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc";     //endofdoc是预定义的bookmark
    
                //创建一个document.
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
    
                //在document的开始部分添加一个paragraph.
                Microsoft.Office.Interop.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 行间距
                oPara1.Range.InsertParagraphAfter();
    
                //在当前document的最后添加一个paragraph
                Microsoft.Office.Interop.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();
    
                //接着添加一个paragraph
                Microsoft.Office.Interop.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();
    
                //添加一个3行5列的表格,填充数据,并且设定第一行的样式
                Microsoft.Office.Interop.Word.Table oTable;
                Microsoft.Office.Interop.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;
    
                //接着添加一些文字
                Microsoft.Office.Interop.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();
    
                //添加一个5行2列的表,填充数据并且改变列宽
                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); //设置列宽
                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
                                               (Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);
                }
                while (dPos >= Convert.ToDouble(oPos));
                object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
                object oPageBreak = Microsoft.Office.Interop.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();
    
                //添加一个chart
                Microsoft.Office.Interop.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.");
    
                Console.ReadLine();
            }
        }
    }
    

    2. 介绍几篇牛人写的关于操作Word的文章

    [分享]一段导出到word模版的代码

    http://www.cnblogs.com/goody9807/archive/2005/08/25/222526.html


    再谈word2003编程

    http://www.cnblogs.com/Andmm/archive/2008/06/18/1224422.html


    最近一直在做C#操作office方面的工作!总结一下!Word()

    http://www.cnblogs.com/wngwz/archive/2004/08/19/34678.html


    C#也能动态生成Word文档并填充数据

    http://www.cnblogs.com/qyfan82/archive/2007/09/14/893293.html


  • 相关阅读:
    Windows下Git使用记录03 Git GUI与金山快盘组建私人代码仓库进行项目开发
    windows下GIT使用记录00准备阶段
    setnx分布式锁原理
    Redis 中 bgsave 方式持久化的细节问题
    IOS开发(十一):场景(3)模态切换示例
    Android:TextView属性大全
    Android:ActionBar官方指导+个人整理
    IOS开发(十二):UIDatePicker
    IOS开发(十三):UIDatePicker、场景切换综合示例
    IOS开发(十):场景(2)模态切换示例
  • 原文地址:https://www.cnblogs.com/tianzhiliang/p/2099800.html
Copyright © 2011-2022 走看看