国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法

2024-07-10 12:53:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

我們?cè)陂_發(fā)asp.net項(xiàng)目時(shí),會(huì)遇到導(dǎo)出文件的情況,如將數(shù)據(jù)從datagrid導(dǎo)出到excel文件,這個(gè)時(shí)候我們?cè)撛趺醋瞿兀肯旅驽e(cuò)新技術(shù)頻道小編給大家介紹asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法。

1,新建word模版,方法是找需要用程序填充的word文檔,在需要輸入的地方用"書簽"(插入-->書簽-->輸入id-->ok)標(biāo)記后保存既可.
2,在word模版上修改安全添加everyone 可讀,以防文件無(wú)法打開.
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 的摘要說(shuō)明
/// </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,在需要使用的頁(yè)面后臺(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)用于下載.

上面就是對(duì)錯(cuò)新技術(shù)頻道小編給大家?guī)?lái)的asp.net下將頁(yè)面內(nèi)容導(dǎo)入到word模板中的方法,請(qǐng)各位認(rèn)真閱讀,如果有什么疑惑的地方可以提出來(lái),錯(cuò)新技術(shù)頻道小編將會(huì)認(rèn)真解答,謝謝各位的閱讀。?

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大石桥市| 房产| 五原县| 宣城市| 海门市| 容城县| 新晃| 新干县| 黄龙县| 墨竹工卡县| 滦南县| 汝阳县| 庄河市| 渭源县| 涟源市| 五莲县| 东乌| 安图县| 济宁市| 游戏| 开原市| 塘沽区| 通许县| 佛山市| 长垣县| 滦平县| 梅州市| 四子王旗| 镇原县| 木兰县| 瑞昌市| 长子县| 沾益县| 南城县| 吉木乃县| 金乡县| 砀山县| 富锦市| 台北县| 怀集县| 巩留县|