asp.net實現word文檔線上預覽功能的方法_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了asp.net實現word文檔線上預覽功能的方法。分享給大家供大家參考。具體實現方法如下:

實現方式:office文檔轉html,再在瀏覽器裡面線上瀏覽

1、首先引入com組件中office庫,然後在程式集擴充中引入word的dll

2、將Microsoft.Office.Interop.Word的嵌入互操作類型設定為 false,如圖

3、主要代碼:

複製代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
namespace Wolfy.OfficePreview
{
    public class Office2HtmlHelper
    {
        /// <summary>
        /// Word轉成Html
        /// </summary>
        /// <param name="path">要轉換的文檔的路徑</param>
        /// <param name="savePath">轉換成html的儲存路徑</param>
        /// <param name="wordFileName">轉換成html的檔案名稱字</param>
        public static void Word2Html(string path, string savePath, string 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[] { (object)path, true, true });
            Type docType = doc.GetType();
            string strSaveFileName = savePath + wordFileName + ".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);
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
    }
}

調用:
複製代碼 代碼如下:
Office2HtmlHelper.Word2Html(MapPath("/Doc/分析某網站的SEO策略(外鏈篇).doc"), MapPath("/Html/"), "分析某網站的SEO策略(外鏈篇)");

希望本文所述對大家的asp.net程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.