直接在线预览Word、Excel、TXT文件之ASP.NET

 更新时间:2015年08月04日 10:46:57   作者:秋荷雨翔  
这篇文章主要用asp.net技术实现直接在线预览word、excel、txt文件,有需要的朋友可以参考下

具体实现过程不多说了,直接贴代码了。



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文件的全部内容,希望大家喜欢。

相关文章

  • C#创建及访问网络硬盘的实现

    C#创建及访问网络硬盘的实现

    本文主要介绍了C#创建及访问网络硬盘的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Unity3D实现摄像机镜头移动并限制角度

    Unity3D实现摄像机镜头移动并限制角度

    这篇文章主要为大家详细介绍了Unity3D实现摄像机镜头移动并限制角度,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05
  • C#设置自定义文件图标实现双击启动(修改注册表)

    C#设置自定义文件图标实现双击启动(修改注册表)

    这篇文章介绍的是利用C#设置自定义文件图标,然后实现双击启动的功能,文章给出了示例代码,介绍的很详细,有需要的可以参考借鉴。
    2016-08-08
  • C#事务处理(Execute Transaction)实例解析

    C#事务处理(Execute Transaction)实例解析

    这篇文章主要介绍了C#事务处理(Execute Transaction)实例解析,对于理解和学习事务处理有一定的帮助,需要的朋友可以参考下
    2014-08-08
  • c# Parallel类的使用

    c# Parallel类的使用

    这篇文章主要介绍了c# Parallel类的使用,帮助大家实现数据与任务的并行,感兴趣的朋友可以了解下
    2020-11-11
  • C#实现在底图上动态生成文字和图片

    C#实现在底图上动态生成文字和图片

    这篇文章主要为大家详细介绍了C#实现在底图上动态生成文字和图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • C# 中 List 与 List 多层嵌套不改变原值的实现方法(深度复制)

    C# 中 List 与 List 多层嵌套不改变原值的实现方法(深度复制)

    这篇文章主要介绍了C# 中 List 与 List 多层嵌套不改变原值的实现方法,使用 BinaryFormatter 将原始 List 序列化为字节流,然后再反序列化得到新的 List,实现了深度复制,需要的朋友可以参考下
    2024-03-03
  • ZooKeeper的安装及部署教程

    ZooKeeper的安装及部署教程

    Zookeeper是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护、名字服务、分布式同步、组服务等,这篇文章主要介绍了ZooKeeper的安装及部署,需要的朋友可以参考下
    2019-06-06
  • C#获取Visio模型信息的简单方法示例

    C#获取Visio模型信息的简单方法示例

    这篇文章主要给大家介绍了关于C#获取Visio模型信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • 不安装excel使用c#创建excel文件

    不安装excel使用c#创建excel文件

    这篇文章主要介绍了使用c#创建excel的示例,刚时给出了不安装excel也可创建excel的方法,需要的朋友可以参考下
    2014-02-02

最新评论