程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 直接在線預覽Word、Excel、TXT文件之ASP.NET

直接在線預覽Word、Excel、TXT文件之ASP.NET

編輯:C#入門知識

直接在線預覽Word、Excel、TXT文件之ASP.NET。本站提示廣大學習愛好者:(直接在線預覽Word、Excel、TXT文件之ASP.NET)文章只能為提供參考,不一定能成為您想要的結果。以下是直接在線預覽Word、Excel、TXT文件之ASP.NET正文


詳細完成進程不多說了,直接貼代碼了。



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace Suya.Web.Apps.Areas.PMP.Controllers
{
  /// <summary>
  /// 在線預覽Office文件
  /// </summary>
  public class OfficeViewController : Controller
  {
    #region Index頁面
    /// <summary>
    /// Index頁面
    /// </summary>
    /// <param name="url">例:/uploads/......XXX.xls</param>
    public ActionResult Index(string url)
    {
      string physicalPath = Server.MapPath(Server.UrlDecode(url));
      string extension = Path.GetExtension(physicalPath);
      string htmlUrl = "";
      switch (extension.ToLower())
      {
        case ".xls":
        case ".xlsx":
          htmlUrl = PreviewExcel(physicalPath, url);
          break;
        case ".doc":
        case ".docx":
          htmlUrl = PreviewWord(physicalPath, url);
          break;
        case ".txt":
          htmlUrl = PreviewTxt(physicalPath, url);
          break;
        case ".pdf":
          htmlUrl = PreviewPdf(physicalPath, url);
          break;
      }
      return Redirect(Url.Content(htmlUrl));
    }
    #endregion
    #region 預覽Excel
    /// <summary>
    /// 預覽Excel
    /// </summary>
    public string PreviewExcel(string physicalPath, string url)
    {
      Microsoft.Office.Interop.Excel.Application application = null;
      Microsoft.Office.Interop.Excel.Workbook workbook = null;
      application = new Microsoft.Office.Interop.Excel.Application();
      object missing = Type.Missing;
      object trueObject = true;
      application.Visible = false;
      application.DisplayAlerts = false;
      workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing, missing);
      //Save Excel to Html
      object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
      string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
      String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
      workbook.SaveAs(outputFile, format, missing, missing, missing,
               missing, XlSaveAsAccessMode.xlNoChange, missing,
               missing, missing, missing, missing);
      workbook.Close();
      application.Quit();
      return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName;
    }
    #endregion
    #region 預覽Word
    /// <summary>
    /// 預覽Word
    /// </summary>
    public string PreviewWord(string physicalPath, string url)
    {
      Microsoft.Office.Interop.Word._Application application = null;
      Microsoft.Office.Interop.Word._Document doc = null;
      application = new Microsoft.Office.Interop.Word.Application();
      object missing = Type.Missing;
      object trueObject = true;
      application.Visible = false;
      application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
      doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
      //Save Excel to Html
      object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
      string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
      String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
      doc.SaveAs(outputFile, format, missing, missing, missing,
               missing, XlSaveAsAccessMode.xlNoChange, missing,
               missing, missing, missing, missing);
      doc.Close();
      application.Quit();
      return Path.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName;
    }
    #endregion
    #region 預覽Txt
    /// <summary>
    /// 預覽Txt
    /// </summary>
    public string PreviewTxt(string physicalPath, string url)
    {
      return Server.UrlDecode(url);
    }
    #endregion
    #region 預覽Pdf
    /// <summary>
    /// 預覽Pdf
    /// </summary>
    public string PreviewPdf(string physicalPath, string url)
    {
      return Server.UrlDecode(url);
    }
    #endregion
  }
}

以上就是針對直接在線預覽word、excel、text、pdf文件的全體內容,願望年夜家愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved