ASP.NET實(shí)現(xiàn)word文檔在線預(yù)覽功能代碼
更新時(shí)間:2010年07月11日 21:16:04 作者:
文檔管理系統(tǒng)需要實(shí)現(xiàn)WORD能在線預(yù)覽功能,以前覺得挺難的,經(jīng)過一番研究發(fā)現(xiàn),WORD自帶的另存為可以保存為HTMl文件。
于是考慮在每個(gè)文件上傳時(shí)為其生存一份HTMl文件,這樣就能實(shí)現(xiàn)在線預(yù)覽功能。主要代碼如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Word = Microsoft.Office.Interop.Word;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WordToHtml("d:\\yijian.doc");
}
/// <summary>
/// word轉(zhuǎn)成html
/// </summary>
/// <param name="wordFileName"></param>
private string WordToHtml(object wordFileName)
{
//在此處放置用戶代碼以初始化頁面
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
//打開文件
Type docsType = docs.GetType();
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//轉(zhuǎn)換格式,另存為
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return saveFileName.ToString();
}
}
復(fù)制代碼 代碼如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Word = Microsoft.Office.Interop.Word;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WordToHtml("d:\\yijian.doc");
}
/// <summary>
/// word轉(zhuǎn)成html
/// </summary>
/// <param name="wordFileName"></param>
private string WordToHtml(object wordFileName)
{
//在此處放置用戶代碼以初始化頁面
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
//打開文件
Type docsType = docs.GetType();
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//轉(zhuǎn)換格式,另存為
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return saveFileName.ToString();
}
}
您可能感興趣的文章:
- ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫入、保存)
- asp.net下將圖片保存到XML文件的方法
- asp.net 下載文件時(shí)根據(jù)MIME類型自動(dòng)判斷保存文件的擴(kuò)展名
- Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
- asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的數(shù)據(jù)表格的方法
- asp.net中Word轉(zhuǎn)Html的辦法(不需要WORD組件)
- asp.net 在線編輯word文檔 可保存到服務(wù)器
- ASP.NET實(shí)現(xiàn)將word文檔轉(zhuǎn)換成pdf的方法
- asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
- ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫
相關(guān)文章
ASP.NET Core應(yīng)用程序配置文件AppSetting.json
這篇文章介紹了ASP.NET Core應(yīng)用程序配置文件AppSetting.json,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02Asp.Net 不同的OnClick事件區(qū)別小結(jié)(onserverclick,onclientclick)
下面以 HTML的按鈕( system.web.ui.htmlcontrols ) 和 ASP.NET服務(wù)端按鈕 ( system.web.ui.webcontrols ) 為例2012-05-05.Net結(jié)構(gòu)型設(shè)計(jì)模式之組合模式(Composite)
這篇文章介紹了.Net結(jié)構(gòu)型設(shè)計(jì)模式之組合模式(Composite),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05ASP.NET Core基礎(chǔ)之啟動(dòng)設(shè)置
這篇文章介紹了ASP.NET Core基礎(chǔ)之啟動(dòng)設(shè)置,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02ASP.NET?Core?MVC路由(Routing)的用法
這篇文章介紹了ASP.NET?Core?MVC路由(Routing)的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04ASP.NET Core中的Options選項(xiàng)模式
這篇文章介紹了ASP.NET Core中的Options選項(xiàng)模式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04