asp.net下將頁面內容匯入到word模板中的方法

來源:互聯網
上載者:User

1,建立word模版,方法是找需要用程式填充的word文檔,在需要輸入的地方用"書籤"(插入-->書籤-->輸入id-->ok)標記後儲存既可.
2,在word模版上修改安全添加everyone 可讀,以防檔案無法開啟.
3,在工程裡添加"引用"找到"Microsoft Word 10.0 Object Library"或"Microsoft Word 11.0 Object Library"點確定.
4,建立類叫WordOp.cs 意思是操作word的類.
內容如下: 複製代碼 代碼如下:using System;
using System.Web.Security;
using Microsoft.Office.Interop.Word;
using System.IO;
/// <summary>
/// Word 的摘要說明
/// </summary>
public class WordOp
{
public WordOp()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
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);
}
//定義一個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的標籤進行填充內容Content,即函件題目項
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,在需要使用的頁面後台調用既可,舉例如下: 複製代碼 代碼如下: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");//做個跳轉用於下載.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.