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

作者:秋荷雨翔 时间:2021-10-07 15:37:54 

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



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
 }
}
标签:asp.net,在线预览
0
投稿

猜你喜欢

  • Android之scrollview滑动使标题栏渐变背景色的实例代码

    2022-10-24 19:26:11
  • 详解Java中的反射机制和动态代理

    2023-11-27 15:41:00
  • Elasticsearch写入瓶颈导致skywalking大盘空白

    2021-07-07 00:39:42
  • C#实现将一个字符串进行翻转显示的6种方法

    2022-05-07 22:55:10
  • Java的JDBC中Statement与CallableStatement对象实例

    2022-11-14 17:22:08
  • 教你使用Java获取当前时间戳的详细代码

    2021-09-19 04:41:02
  • Spring MVC返回的json去除根节点名称的方法

    2023-07-15 17:46:55
  • Java静态代理与动态代理案例详解

    2021-12-09 19:55:44
  • Java 8 Stream操作类型及peek示例解析

    2021-07-17 20:42:08
  • springboot集成@DS注解实现数据源切换的方法示例

    2021-11-18 11:30:00
  • 简略分析Android的Retrofit应用开发框架源码

    2023-09-17 22:12:30
  • JavaCV实现读取视频信息及自动截取封面图详解

    2022-05-20 14:43:05
  • java多线程编程实例

    2022-12-08 18:51:29
  • Java接口幂等性设计原理解析

    2022-12-22 12:27:01
  • Springboot实现通用Auth认证的几种方式

    2023-08-05 20:54:58
  • Springboot整合log4j2日志全解总结

    2021-12-27 22:56:58
  • C#数据绑定(DataBinding)简单实现方法

    2021-10-09 21:13:22
  • .Net多进程通信共享内存映射文件Memory Mapped

    2022-04-30 23:33:57
  • 解决PhoneGap不支持viewport的几种方法

    2023-03-13 01:51:15
  • Java使用openssl检测网站是否支持ocsp

    2022-10-03 15:55:55
  • asp之家 软件编程 m.aspxhome.com