最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年08月04日 10:46:57   作者:秋荷雨翔  
這篇文章主要用asp.net技術(shù)實現(xiàn)直接在線預(yù)覽word、excel、txt文件,有需要的朋友可以參考下

具體實現(xiàn)過程不多說了,直接貼代碼了。



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>
  /// 在線預(yù)覽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 預(yù)覽Excel
    /// <summary>
    /// 預(yù)覽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 預(yù)覽Word
    /// <summary>
    /// 預(yù)覽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 預(yù)覽Txt
    /// <summary>
    /// 預(yù)覽Txt
    /// </summary>
    public string PreviewTxt(string physicalPath, string url)
    {
      return Server.UrlDecode(url);
    }
    #endregion
    #region 預(yù)覽Pdf
    /// <summary>
    /// 預(yù)覽Pdf
    /// </summary>
    public string PreviewPdf(string physicalPath, string url)
    {
      return Server.UrlDecode(url);
    }
    #endregion
  }
}

以上就是針對直接在線預(yù)覽word、excel、text、pdf文件的全部內(nèi)容,希望大家喜歡。

相關(guān)文章

  • C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn)

    C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn)

    本文主要介紹了C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Unity3D實現(xiàn)攝像機鏡頭移動并限制角度

    Unity3D實現(xiàn)攝像機鏡頭移動并限制角度

    這篇文章主要為大家詳細(xì)介紹了Unity3D實現(xiàn)攝像機鏡頭移動并限制角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • C#設(shè)置自定義文件圖標(biāo)實現(xiàn)雙擊啟動(修改注冊表)

    C#設(shè)置自定義文件圖標(biāo)實現(xiàn)雙擊啟動(修改注冊表)

    這篇文章介紹的是利用C#設(shè)置自定義文件圖標(biāo),然后實現(xiàn)雙擊啟動的功能,文章給出了示例代碼,介紹的很詳細(xì),有需要的可以參考借鑒。
    2016-08-08
  • C#事務(wù)處理(Execute Transaction)實例解析

    C#事務(wù)處理(Execute Transaction)實例解析

    這篇文章主要介紹了C#事務(wù)處理(Execute Transaction)實例解析,對于理解和學(xué)習(xí)事務(wù)處理有一定的幫助,需要的朋友可以參考下
    2014-08-08
  • c# Parallel類的使用

    c# Parallel類的使用

    這篇文章主要介紹了c# Parallel類的使用,幫助大家實現(xiàn)數(shù)據(jù)與任務(wù)的并行,感興趣的朋友可以了解下
    2020-11-11
  • C#實現(xiàn)在底圖上動態(tài)生成文字和圖片

    C#實現(xiàn)在底圖上動態(tài)生成文字和圖片

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)在底圖上動態(tài)生成文字和圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C#?中?List?與?List?多層嵌套不改變原值的實現(xiàn)方法(深度復(fù)制)

    C#?中?List?與?List?多層嵌套不改變原值的實現(xiàn)方法(深度復(fù)制)

    這篇文章主要介紹了C#?中?List?與?List?多層嵌套不改變原值的實現(xiàn)方法,使用?BinaryFormatter?將原始?List?序列化為字節(jié)流,然后再反序列化得到新的?List,實現(xiàn)了深度復(fù)制,需要的朋友可以參考下
    2024-03-03
  • ZooKeeper的安裝及部署教程

    ZooKeeper的安裝及部署教程

    Zookeeper是一個針對大型分布式系統(tǒng)的可靠協(xié)調(diào)系統(tǒng),提供的功能包括:配置維護(hù)、名字服務(wù)、分布式同步、組服務(wù)等,這篇文章主要介紹了ZooKeeper的安裝及部署,需要的朋友可以參考下
    2019-06-06
  • C#獲取Visio模型信息的簡單方法示例

    C#獲取Visio模型信息的簡單方法示例

    這篇文章主要給大家介紹了關(guān)于C#獲取Visio模型信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • 不安裝excel使用c#創(chuàng)建excel文件

    不安裝excel使用c#創(chuàng)建excel文件

    這篇文章主要介紹了使用c#創(chuàng)建excel的示例,剛時給出了不安裝excel也可創(chuàng)建excel的方法,需要的朋友可以參考下
    2014-02-02

最新評論

常宁市| 崇仁县| 曲水县| 湟源县| 东港市| 英德市| 潞城市| 鹰潭市| 电白县| 宣武区| 晋州市| 高密市| 东乌| 九台市| 德昌县| 长岭县| 武夷山市| 罗城| 浦江县| 东安县| 区。| 叶城县| 屯门区| 陕西省| 岐山县| 绥宁县| 岑溪市| 邯郸县| 平潭县| 信阳市| 丁青县| 罗山县| 水富县| 响水县| 昆明市| 通州市| 盐边县| 高唐县| 揭东县| 金阳县| 综艺|