直接在线预览Word、Excel、TXT文件之ASP.NET
作者:秋荷雨翔 发布时间:2021-10-07 15:37:54
标签: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
}
}


猜你喜欢
- 一、构造方法类的构造方法是类的成员方法的一种,它的作用是对类中的成员进行初始化操作。类的构造方法分为:
- 首先我们要明白一下几点,1.代码写出来除了让他跑起来还有个非常非常重要的作用是维护,因为没有一成不变的代码,需求变化代码就不可避免的要变化。
- springboot使用mybatis一对多的关联查询由于刚开始写java不久,对sql语句的熟悉度还是不够熟练,虽然现在使用的mybati
- 1、打开IntelliJ IDEA,新建一个Maven项目2、导入Jmeter的依赖包在idea中导入jmeter下的ApacheJMete
- 1.结构体类型C语言中的2种类型:原生类型和自定义类型,结构体类型是一种自定义类型。2.结构体使用时先定义结构体类型再用类型定义变量->
- 本文实例汇总了Java的System.getProperty()方法获取信息的用法。分享给大家供大家参考。具体如下:System.out.p
- 本文实例分析了C#遍历List并删除某个元素的方法。分享给大家供大家参考。具体如下:1、我们选择用for循环:for(int i=0;i&l
- Android RecycleView添加head配置封装的实例这个是把RecycleView的适配器给封装了,直接调用就可以了,还添加了可
- maven使用过程中无法导入依赖的一些总结作为一名java开发的新手,在学习中难免遇见各种问题,在此总结一下。在使用maven过程中总是碰见
- 详解Java对象的强、软、弱和虚引用+ReferenceQueue一、强引用(StrongReference)强引用是使用最普遍的引用。如果
- 本文实例为大家分享了C#实现订单管理程序的具体代码,供大家参考,具体内容如下订单管理的控制台程序,能够实现添加订单、删除订单、修改订单、查询
- 本文实例为大家分享了Android自定义弹框Dialog效果的具体代码,供大家参考,具体内容如下1.dialog_delete.xml<
- 要说this和super就不得不说Java的封装和继承了,首先说封装,这是一种思想,算不上一种技术,核心思想就是将对象的同一行为和状态看成是
- 这篇文章主要介绍了MyBatis Mapper接受参数的四种方式代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 本篇文章介绍SpringBoot的上传和下载功能。一、创建SpringBoot工程,添加依赖compile("org.spring
- 前言本篇文章主要讲述的是SpringBoot整合Mybatis、Druid和PageHelper 并实现多数据源和分页。其中SpringBo
- 如果没有安装过maven,是用的idea自带的maven,那就是idea的安装目录下 /plugins/maven/lib/maven3这个
- 上篇文章给大家介绍了springboot全局字符编码设置解决乱码问题 感兴趣的朋友可以点击查看,下面通过两种方式给大家介绍Spri
- 这个问题百度上一搜一大把,基本上都是说找到和SurfaceView的比例相近的camera预览尺寸,但是发现预览时候还是差了点意思,具体看下
- 如何调试Java程序?大家最开始学习Java,都会觉得IDE调试好高端有木有,其实很简单了。下文会尽量简单直观的教会你在Eclipse中调试