根據模板產生word文檔《轉》

來源:互聯網
上載者:User

主要功能為根據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 是設定字型的格式的地方

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.