C#word
主要功能為根據(jù)word模板生成word報(bào)表文檔,注意引用Interop.Word.dll;
首先要生成word程序?qū)ο?br />Word.Application app = new Word.Application();
根據(jù)模板文件生成新文件框架
File.Copy(TemplateFile, FileName);
生成documnet對(duì)象
ord.Document doc = new Word.Document();
??????? 打開(kāi)新文擋
??????? 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();
將光標(biāo)定位到新的書(shū)簽(模板中定義了書(shū)簽的位置),下面代碼為在光標(biāo)位置輸出一行,然后回車
??????? //光標(biāo)轉(zhuǎn)到書(shū)簽
??????? 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????? 大學(xué)");
??????????? doc.ActiveWindow.Selection.TypeParagraph();
??????? }
輸出完畢后,最后關(guān)閉doc對(duì)象
??????? 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:MyworkExcelReportsServerReportServerTempalteSmallList.doc";
????????//生成的具有模板樣式的新文件
????????string?FileName?=?@"C:Documents?and?SettingsAdministrator桌面"?+?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;
????????//打開(kāi)文件
????????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();
????????//光標(biāo)轉(zhuǎn)到書(shū)簽
????????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??????大學(xué)");
????????????doc.ActiveWindow.Selection.TypeParagraph();
????????}
????????object?IsSave?=?true;
????????doc.Close(ref?IsSave,?ref?missing,?ref?missing);
????????Response.Write("<script?language='javascript'>alert('生成模板成功!')</script>");
????}
}
?
附:
光標(biāo)到 書(shū)簽Title 的位置
object BookMarkName="Title";
object what =Word.WdGoToItem.wdGoToBookmark;
Doc.ActiveWindow.Selection.GoTo(ref what ,ref missing,ref missing,ref BookMarkName);???????????????????????
在當(dāng)前的光標(biāo)寫(xiě)文本
Doc.ActiveWindow.Selection.TypeText("變更通知");
當(dāng)前的光標(biāo)換行
Doc.ActiveWindow.Selection.TypeParagraph();
當(dāng)前的光標(biāo)設(shè)置格式(舉例 對(duì)齊方式)????????????????????????????????????????????????????????????? Doc.ActiveWindow.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight;
注意 ParagraphFormat 是設(shè)置字體的格式的地方
總結(jié)
- 上一篇: word模板生成word报表文档
- 下一篇: C# WebBrowser自动填表与提交