asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法
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 對象
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); //對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,在需要使用的頁面后臺調(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", "測試");
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)文章
TreeView創(chuàng)建IHierarchicalDataSource類型的數(shù)據(jù)源實(shí)現(xiàn)
為TreeView創(chuàng)建IHierarchicalDataSource類型的數(shù)據(jù)源實(shí)現(xiàn)2009-01-01
詳解Asp.net Core 使用Redis存儲Session
本篇文章主要介紹了Asp.net Core 使用Redis存儲Session ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧。2016-12-12
asp.net中Fine Uploader文件上傳組件使用介紹
最近在處理后臺數(shù)據(jù)時(shí)需要實(shí)現(xiàn)文件上傳.考慮到對瀏覽器適配上采用Fine Uploader. Fine Uploader 采用ajax方式實(shí)現(xiàn)對文件上傳.同時(shí)在瀏覽器中直接支持文件拖拽[對瀏覽器版本有要求類似IE版本必須是9或是更高的IE10].2013-01-01
ASP.NET Core環(huán)境變量和啟動設(shè)置的配置教程
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core環(huán)境變量和啟動設(shè)置的配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
asp.net計(jì)算每個(gè)頁面執(zhí)行時(shí)間的方法
這篇文章主要介紹了asp.net計(jì)算每個(gè)頁面執(zhí)行時(shí)間的方法,涉及asp.net操作時(shí)間的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
.net core 1.0 實(shí)現(xiàn)單點(diǎn)登錄負(fù)載多服務(wù)器
這篇文章主要介紹了.net core 1.0 實(shí)現(xiàn)單點(diǎn)登錄負(fù)載多服務(wù)器的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友可以參考下2016-07-07
ASP.NET用SignalR建立瀏覽器和服務(wù)器的持久連接詳解
這篇文章主要給大家介紹了ASP.NET用SignalR如何建立瀏覽器和服務(wù)器的持久連接,文章先給大家簡單介紹了配置環(huán)境,而后通過實(shí)戰(zhàn)來給大家詳細(xì)的介紹了實(shí)現(xiàn)的過程,文中通過一步步的步驟介紹的很詳細(xì),感興趣的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
Asp.net在頁面間傳遞大量數(shù)據(jù)(數(shù)據(jù)表)建議采用的方法
能讓數(shù)據(jù)在 兩個(gè)不同站點(diǎn)之間傳遞嗎,針對這個(gè)問題將會展開本文的探討有想在A站點(diǎn)的數(shù)據(jù)傳遞到B站點(diǎn)的朋友們可以適當(dāng)參考下,或許本文對你有所幫助2013-02-02
asp.net 自制的單選、多選列表實(shí)現(xiàn)代碼
在ASP.NET的頁面上,ListBox最終是渲染成select元素,而CheckListBox最終被渲染成div或者是table,使得二者的樣式無法統(tǒng)一,或者說要統(tǒng)一很麻煩。2009-08-08

