asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法
更新時(shí)間:2010年10月20日 19:04:23 作者:
asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法,需要的朋友可以參考下。
1,新建word模版,方法是找需要用程序填充的word文檔,在需要輸入的地方用"書簽"(插入-->書簽-->輸入id-->ok)標(biāo)記后保存既可.
2,在word模版上修改安全添加everyone 可讀,以防文件無法打開.
3,在工程里添加"引用"找到"Microsoft Word 10.0 Object Library"或"Microsoft Word 11.0 Object Library"點(diǎn)確定.
4,新建類叫WordOp.cs 意思是操作word的類.
內(nèi)容如下:
using System;
using System.Web.Security;
using Microsoft.Office.Interop.Word;
using System.IO;
/// <summary>
/// Word 的摘要說明
/// </summary>
public class WordOp
{
public WordOp()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
private ApplicationClass WordApp ;
private Document WordDoc;
private static bool isOpened=false;//判斷word模版是否被占用
public void SaveAs(string strFileName,bool isReplace)
{
if (isReplace && File.Exists(strFileName))
{
File.Delete(strFileName);
}
object missing = Type.Missing;
object fileName = strFileName;
WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
//定義一個(gè)Word.Application 對(duì)象
public void activeWordApp()
{
WordApp = new ApplicationClass();
}
public void Quit()
{
object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit(ref missing, ref missing, ref missing);
isOpened = false;
}
//基于模版新建Word文件
public void OpenTempelte(string strTemppath)
{
object Missing = Type.Missing;
//object Missing = System.Reflection.Missing.Value;
activeWordApp();
WordApp.Visible = false;
object oTemplate = (object)strTemppath;
try
{
while (isOpened)
{
System.Threading.Thread.Sleep(500);
}
WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
isOpened = true;
WordDoc.Activate();
}
catch (Exception Ex)
{
Quit();
isOpened = false;
throw new Exception(Ex.Message);
}
}
public void FillLable(string LabelId,string Content)
{
//打開Word模版
// OpenTempelte(tempName); //對(duì)LabelId的標(biāo)簽進(jìn)行填充內(nèi)容Content,即函件題目項(xiàng)
object bkmC = LabelId;
if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
}
WordApp.Selection.TypeText(Content);
//SaveAs(saveAsFileName);
//Quit();
}
}
5,在需要使用的頁面后臺(tái)調(diào)用既可,舉例如下:
string path = Server.MapPath("download");
string templatePath = path + "file://downloadczql.doc/";
WordOp wop = new WordOp();
wop.OpenTempelte(templatePath);
wop.FillLable("id", "1");
wop.FillLable("usr_name", "測(cè)試");
wop.SaveAs(path + "file://savetest.doc",true/);
wop.Quit();
Response.redirect(@"/download/savetest.doc");//做個(gè)跳轉(zhuǎn)用于下載.
2,在word模版上修改安全添加everyone 可讀,以防文件無法打開.
3,在工程里添加"引用"找到"Microsoft Word 10.0 Object Library"或"Microsoft Word 11.0 Object Library"點(diǎn)確定.
4,新建類叫WordOp.cs 意思是操作word的類.
內(nèi)容如下:
復(fù)制代碼 代碼如下:
using System;
using System.Web.Security;
using Microsoft.Office.Interop.Word;
using System.IO;
/// <summary>
/// Word 的摘要說明
/// </summary>
public class WordOp
{
public WordOp()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
private ApplicationClass WordApp ;
private Document WordDoc;
private static bool isOpened=false;//判斷word模版是否被占用
public void SaveAs(string strFileName,bool isReplace)
{
if (isReplace && File.Exists(strFileName))
{
File.Delete(strFileName);
}
object missing = Type.Missing;
object fileName = strFileName;
WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
//定義一個(gè)Word.Application 對(duì)象
public void activeWordApp()
{
WordApp = new ApplicationClass();
}
public void Quit()
{
object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit(ref missing, ref missing, ref missing);
isOpened = false;
}
//基于模版新建Word文件
public void OpenTempelte(string strTemppath)
{
object Missing = Type.Missing;
//object Missing = System.Reflection.Missing.Value;
activeWordApp();
WordApp.Visible = false;
object oTemplate = (object)strTemppath;
try
{
while (isOpened)
{
System.Threading.Thread.Sleep(500);
}
WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
isOpened = true;
WordDoc.Activate();
}
catch (Exception Ex)
{
Quit();
isOpened = false;
throw new Exception(Ex.Message);
}
}
public void FillLable(string LabelId,string Content)
{
//打開Word模版
// OpenTempelte(tempName); //對(duì)LabelId的標(biāo)簽進(jìn)行填充內(nèi)容Content,即函件題目項(xiàng)
object bkmC = LabelId;
if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
}
WordApp.Selection.TypeText(Content);
//SaveAs(saveAsFileName);
//Quit();
}
}
5,在需要使用的頁面后臺(tái)調(diào)用既可,舉例如下:
復(fù)制代碼 代碼如下:
string path = Server.MapPath("download");
string templatePath = path + "file://downloadczql.doc/";
WordOp wop = new WordOp();
wop.OpenTempelte(templatePath);
wop.FillLable("id", "1");
wop.FillLable("usr_name", "測(cè)試");
wop.SaveAs(path + "file://savetest.doc",true/);
wop.Quit();
Response.redirect(@"/download/savetest.doc");//做個(gè)跳轉(zhuǎn)用于下載.
您可能感興趣的文章:
- ASP.NET之Excel下載模板、導(dǎo)入、導(dǎo)出操作
- asp.net GridView控件中模板列CheckBox全選、反選、取消
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- 使用ASP.NET模板生成HTML靜態(tài)頁面的五種方案
- asp.net TemplateField模板中的Bind方法和Eval方法
- asp.net GridView模板列中實(shí)現(xiàn)選擇行功能
- asp.net(C#)壓縮圖片,可以指定圖片模板高寬
- asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
- ASP.NET中實(shí)現(xiàn)模板頁
- MVC使用T4模板生成其他類的具體實(shí)現(xiàn)學(xué)習(xí)筆記2
相關(guān)文章
獲取客戶端IP地址c#/vb.net各自實(shí)現(xiàn)代碼
項(xiàng)目要求獲取客戶端的IP地址,分析并且用c#與vb各自實(shí)現(xiàn)了此要求,有需要的朋友可以了解下啊,希望本文對(duì)你們有所幫助2013-01-01asp.net模板引擎Razor中cacheName的問題分析
這篇文章主要介紹了asp.net模板引擎Razor中cacheName的問題,實(shí)例分析了cacheName在提高編譯效率方面的使用技巧,需要的朋友可以參考下2015-06-06asp.net SqlDataAdapter對(duì)象使用札記
如果 DataAdapter 遇到多個(gè)結(jié)果集,它將在 DataSet 中創(chuàng)建多個(gè)表。將向這些表提供遞增的默認(rèn)名稱 TableN,以表示 Table0 的“Table”為第一個(gè)表名。2009-04-04c# 可變數(shù)目參數(shù)params實(shí)例
一般來說,參數(shù)個(gè)數(shù)都是固定的,定義為集群類型的參數(shù)可以實(shí)現(xiàn)可變數(shù)目參數(shù)的目的,但是.NET提供了更靈活的機(jī)制來實(shí)現(xiàn)可變數(shù)目參數(shù),這就是使用params修飾符2012-11-11