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