• 大小: 46KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-18
  • 语言: C#
  • 标签: C#  生成Word  保存Word  

资源简介

动态生成Word,此示例程序中的Word含有文字、图片、表格、图标、格式等元素。 生成完之后打印预览、保存到C盘根目录,最后退出。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;


namespace WordDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {

        }
        /// 
        /// 声明:此方法Copy自互联网,代码注释原本为English
        /// 由“奔放的胸毛。(luoyuan@ly-zane.com)”笨拙翻译,凑合着看吧
        /// 项目引用了Office自带的COM组件Microsoft Word
        /// 

        /// 
        /// 
        private void button1_Click(object sender System.EventArgs e)
        {
            object oMissing = Missing.Value;
            object oEndOfDoc = “\\endofdoc“; //指定编码

            //开始
            Word._Application oWord;//声明一个Word应用程序
            Word._Document oDoc;//声明一个Word文档
            oWord = new Word.Application();//实例化
            oWord.Visible = true;//显示此文档
            oDoc = oWord.Documents.Add(ref oMissing ref oMissing
                ref oMissing ref oMissing);


            //添加页眉
            oWord.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
            oWord.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;
            oWord.ActiveWindow.ActivePane.Selection.InsertAfter(“[页眉内容]“);
            oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
            oWord.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//跳出页眉设置

            oWord.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距



            //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;//加粗1
            oPara1.Format.SpaceAfter = 24;      //24 pt spacing after paragraph.(在此段落之后添加24磅的行距)
            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);//将oRng这个行添加到文档中并返回
            oPara2.Range.Text = “Heading 2“;//在此段落的行中添加字符串
            oPara2.Format.SpaceAfter = 6;//在此段落之后添加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 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        913  2010-08-09 16:32  WordDemo.sln

    ..A..H.     14336  2010-08-10 13:04  WordDemo.suo

     文件       4139  2010-08-09 16:38  WordDemo\WordDemo.csproj

     文件        467  2010-08-09 16:32  WordDemo\Program.cs

     文件       2091  2010-08-09 16:38  WordDemo\Form1.Designer.cs

     文件       5814  2010-08-09 16:38  WordDemo\Form1.resx

     文件       1970  2010-08-10 13:01  WordDemo\obj\WordDemo.csproj.FileListAbsolute.txt

     文件        680  2010-08-10 10:18  WordDemo\obj\Debug\WordDemo.csproj.ResolveComReference.cache

     文件        180  2010-08-10 10:18  WordDemo\obj\Debug\WordDemo.Form1.resources

     文件        180  2010-08-10 10:18  WordDemo\obj\Debug\WordDemo.Properties.Resources.resources

     文件        842  2010-08-10 10:18  WordDemo\obj\Debug\WordDemo.csproj.GenerateResource.Cache

     文件      26112  2010-08-10 13:01  WordDemo\obj\Debug\WordDemo.pdb

     文件      24576  2010-08-10 13:01  WordDemo\obj\Debug\WordDemo.exe

     文件       5632  2005-11-11 22:25  WordDemo\bin\Debug\WordDemo.vshost.exe

     文件       4814  2010-08-09 10:46  WordDemo\bin\Debug\tempcode.bmp

     文件      24576  2010-08-10 13:01  WordDemo\bin\Debug\WordDemo.exe

     文件      26112  2010-08-10 13:01  WordDemo\bin\Debug\WordDemo.pdb

     文件      32768  2010-08-10 13:01  WordDemo\bin\Debug\zane.doc

     文件       5612  2010-08-09 16:32  WordDemo\Properties\Resources.resx

     文件        249  2010-08-09 16:32  WordDemo\Properties\Settings.settings

     文件       1200  2010-08-09 16:32  WordDemo\Properties\AssemblyInfo.cs

     文件       2872  2010-08-09 16:32  WordDemo\Properties\Resources.Designer.cs

     文件       1093  2010-08-09 16:32  WordDemo\Properties\Settings.Designer.cs

     文件      11575  2010-08-10 13:04  WordDemo\Form1.cs

     目录          0  2010-08-09 23:03  WordDemo\obj\Debug\TempPE

     目录          0  2010-08-09 23:03  WordDemo\obj\Debug

     目录          0  2010-08-09 23:03  WordDemo\bin\Debug

     目录          0  2010-08-09 23:03  WordDemo\obj

     目录          0  2010-08-09 23:03  WordDemo\bin

     目录          0  2010-08-09 23:03  WordDemo\Properties

............此处省略4个文件信息

评论

共有 条评论