当前位置: 首页 > 编程笔记 >

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

危卜鹰
2023-03-14
本文向大家介绍直接在线预览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文件的全部内容,希望大家喜欢。

 类似资料:
  • 本文向大家介绍Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码),包括了Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)的使用技巧和注意事项,需要的朋友参考一下 1.功能说明 输入文件路径,在浏览器输出文件预览信息,经测试360极速(Chrome)、IE9/10、Firefox通过 2.分类文件及代码说明 DemoFiles 存

  • Seafile 专业版服务器支持在线预览 office 文件,配置方法如下。 安装 Libreoffice/UNO Office 预览依赖于 Libreoffice 4.1+ 和 Python-uno 库。 Ubuntu/Debian: sudo apt-get install libreoffice libreoffice-script-provider-python poppler-utils

  • 1.手机版 使用方法:找到需要在线预览的文件 -在线预览。 2.电脑版 使用方法:找到需要在线预览的文件 -在线预览。

  • mammoth.js插件只支持docx后缀的word文件,doc后缀应该如何解决

  • 本文向大家介绍微信小程序实现上传word、txt、Excel、PPT等文件功能,包括了微信小程序实现上传word、txt、Excel、PPT等文件功能的使用技巧和注意事项,需要的朋友参考一下 正文: 目前小程序没有能实现此功能的 API 所以我这里通过使用 web-view 实现; 实现流程: 1. 在小程序后台配置业务域名 2. 在服务器写一个html,实现表单上传文件 3.后端php接收文件并

  • 本文向大家介绍JAVA读取文件流,设置浏览器下载或直接预览操作,包括了JAVA读取文件流,设置浏览器下载或直接预览操作的使用技巧和注意事项,需要的朋友参考一下 最近项目需要在浏览器中通过URL预览图片。但发现浏览器始终默认下载,而不是预览。研究了一下,发现了问题: // 设置response的Header,注意这句,如果开启,默认浏览器会进行下载操作,如果注释掉,浏览器会默认预览。 respons