自gmail推出office文檔的預覽功能以來,各大郵箱紛紛跟進,可是居然找不到成熟的文檔,感到匪夷所思,商業機密?
這次碰到個小需求是要在網頁裡預覽doc文檔,
其中一種解決方式,就是在上傳doc檔案的時候,用代碼操作word的“另存新檔”功能,存成html,
相關資訊,可google"asp.net 預覽 doc文檔"關鍵詞,查出來的都是同一篇。
這種方式的局限在
1,每一份文檔會產生一個html副本和資源檔夾,空間浪費嚴重
2,伺服器一定要安裝office,當然你也可以嘗試引入必要dll,然後根據錯誤提示進行複製的DCOM配置,這一點每次把我把折騰瘋了,最後都是裝office解決的。。。
3,目前未測試docx
大致實現如下,非常有誠意地提醒大家,最好還是安裝office後再試
引入相關DLL後(比如word,或excel),參考下述代碼:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Diagnostics; using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using System.Reflection; using Microsoft.Office.Interop.Excel; public partial class upload_preview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GenerationWordHTML("E:\\20110502.doc", "E:\\20110502.html"); GenerationExcelHTML("E:\\20110502.xls", "E:\\20110502.html"); } /// <summary> /// Ecxel檔案產生HTML並儲存 /// </summary> /// <param name="FilePath">需要產生的ecxel檔案的路徑</param> /// <param name="saveFilePath">產生以後儲存HTML檔案的路徑</param> /// <returns>是否產生成功,成功為true,反之為false</returns> protected bool GenerationExcelHTML(string FilePath, string saveFilePath) { try { Excel.Application app = new Excel.Application(); app.Visible = false; Object o = Missing.Value; ///開啟檔案 /*下面是Microsoft Excel 9 Object Library的寫法: */ /*_Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o);*/ /*下面是Microsoft Excel 10 Object Library的寫法: */ _Workbook xls = app.Workbooks.Open(FilePath, o, o, o, o, o, o, o, o, o, o, o, o, o, o); ///轉換格式,另存新檔 HTML /*下面是Microsoft Excel 9 Object Library的寫法: */ /*xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o);*/ /*下面是Microsoft Excel 10 Object Library的寫法: */ xls.SaveAs(saveFilePath, Excel.XlFileFormat.xlHtml, o, o, o, o, XlSaveAsAccessMode.xlExclusive, o, o, o, o, o); ///退出 Excel app.Quit(); return true; } catch { return false; } finally { //最後關閉開啟的excel 進程 Process[] myProcesses = Process.GetProcessesByName("EXCEL"); foreach (Process myProcess in myProcesses) { myProcess.Kill(); } } } /// <summary> /// WinWord檔案產生HTML並儲存 /// </summary> /// <param name="FilePath">需要產生的word檔案的路徑</param> /// <param name="saveFilePath">產生以後儲存HTML檔案的路徑</param> /// <returns>是否產生成功,成功為true,反之為false</returns> private bool GenerationWordHTML(string FilePath, string saveFilePath) { try { 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[] { FilePath, true, true }); /// 轉換格式,另存新檔 HTML Type docType = doc.GetType(); /*下面是Microsoft Word 9 Object Library的寫法: */ /*docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatHTML });*/ /*下面是Microsoft Word 10 Object Library的寫法: */ docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFilePath, Word.WdSaveFormat.wdFormatFilteredHTML }); /// 退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); return true; } catch { return false; } finally { //最後關閉開啟的winword 進程 Process[] myProcesses = Process.GetProcessesByName("WINWORD"); foreach (Process myProcess in myProcesses) { myProcess.Kill(); } } } }
=============
至此中文資源沒有了,於是繼續尋找,發現了一個老外的實現,但是只支援word2007,因為office2007是基於openXML的,當然可以自行解析
見此地址
http://blog.maartenballiauw.be/post/2008/01/11/Preview-Word-files-(docx)-in-HTML-using-ASPNET-OpenXML-and-LINQ-to-XML.aspx
那就折衷吧,至少這種解決方案可以實現.docx結尾的url直接預覽,並且提供了產生下載連結的規則。