zoukankan      html  css  js  c++  java
  • 根据模板生成word文档《转》

    主要功能为根据word模板生成word报表文档,注意引用Interop.Word.dll;
    首先要生成word程序对象
    Word.Application app = new Word.Application();
    根据模板文件生成新文件框架
    File.Copy(TemplateFile, FileName);
    生成documnet对象
    ord.Document doc = new Word.Document();
            打开新文挡
            doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref Visible,
                ref missing, ref missing, ref missing,
                ref missing);
            doc.Activate();
    将光标定位到新的书签(模板中定义了书签的位置),下面代码为在光标位置输出一行,然后回车
            //光标转到书签
            for (int bookIndex = 0; bookIndex < 5; bookIndex++)
            {
                object BookMarkName = "BookMark" + bookIndex.ToString();
                object what = Word.WdGoToItem.wdGoToBookmark;
                doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                doc.ActiveWindow.Selection.TypeText("文明单位" + bookIndex.ToString() + "zaddd    25      大学");
                doc.ActiveWindow.Selection.TypeParagraph();
            }
    输出完毕后,最后关闭doc对象
            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);

    完整事例代码如下:
    using System;
    using System.IO;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Word.Application app = new Word.Application();
            //模板文件
            string TemplateFile = @"D:\Mywork\ExcelReportsServer\ReportServer\Tempalte\SmallList.doc";
            //生成的具有模板样式的新文件
            string FileName = @"C:\Documents and Settings\Administrator\桌面\" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff")+".doc";
            //模板文件拷贝到新文件
            File.Copy(TemplateFile, FileName);
            Word.Document doc = new Word.Document();
            object Obj_FileName = FileName;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;
            //打开文件
            doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref Visible,
                ref missing, ref missing, ref missing,
                ref missing);
            doc.Activate();

            //光标转到书签
            for (int bookIndex = 0; bookIndex < 5; bookIndex++)
            {
                object BookMarkName = "BookMark" + bookIndex.ToString();
                object what = Word.WdGoToItem.wdGoToBookmark;
                doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                doc.ActiveWindow.Selection.TypeText("文明单位" + bookIndex.ToString() + "zaddd    25      大学");
                doc.ActiveWindow.Selection.TypeParagraph();
            }
            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);


            Response.Write("<script language='javascript'>alert('生成模板成功!')</script>");
        }
    }

    附:
    光标到 书签Title 的位置
    object BookMarkName="Title";
    object what =Word.WdGoToItem.wdGoToBookmark;
    Doc.ActiveWindow.Selection.GoTo(ref what ,ref missing,ref missing,ref BookMarkName);                       
    在当前的光标写文本
    Doc.ActiveWindow.Selection.TypeText("变更通知");

    当前的光标换行
    Doc.ActiveWindow.Selection.TypeParagraph();

    当前的光标设置格式(举例 对齐方式)                                                              Doc.ActiveWindow.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight;

    注意 ParagraphFormat 是设置字体的格式的地方

  • 相关阅读:
    LAMP实例搭建wordpress博客步骤
    MySQL Replication
    Mariadb源码和二进制安装
    Linux九阴真经之九阴白骨爪残卷13(LVM的备份还原,恢复最新状态)
    Linux九阴真经之九阴白骨爪残卷12(日志功能)
    Linux九阴真经之九阴白骨爪残卷11(并发访问控制和事务Transactions)
    Linux九阴真经之九阴白骨爪残卷10(MySQL架构、缓存及索引)
    Linux九阴真经之九阴白骨爪残卷9(存储引擎MyISAM、MySQL服务器变量)
    Linux九阴真经之九阴白骨爪残卷8(存储函数、存储过程、触发器)
    Linux九阴真经之九阴白骨爪残卷7(Mariadb的三种安装方法)
  • 原文地址:https://www.cnblogs.com/kingwangzhen/p/1702632.html
Copyright © 2011-2022 走看看