直接在线预览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,在线预览
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot使用IDEA远程Debug
2021-12-13 06:40:35
![](https://img.aspxhome.com/file/2023/5/61245_0s.jpg)
eclipse springboot工程打war包方法及再Tomcat中运行的方法
2023-04-14 09:21:40
![](https://img.aspxhome.com/file/2023/9/78349_0s.png)
批处理一键安装JDK/一键安装JRE和自动配置Java环境变量
2023-11-29 05:28:30
![](https://img.aspxhome.com/file/2023/2/60312_0s.png)
详解SpringBoot 快速整合Mybatis(去XML化+注解进阶)
2022-02-19 03:54:29
![](https://img.aspxhome.com/file/2023/9/65359_0s.png)
用SpringBoot+Vue+uniapp小程序实现在线房屋装修管理系统
2023-11-12 04:10:48
![](https://img.aspxhome.com/file/2023/3/58883_0s.png)
Java SpringBoot自动装配原理详解
2022-09-08 01:15:09
![](https://img.aspxhome.com/file/2023/0/61610_0s.png)
C# SQLite执行效率的优化教程
2021-07-11 00:11:41
![](https://img.aspxhome.com/file/2023/9/68659_0s.png)
C#的内存回收代码
2023-06-20 06:38:47
C#实现winform中RichTextBox在指定光标位置插入图片的方法
2022-04-04 01:50:16
Java实现图片拼接
2023-02-28 23:01:27
简单了解java标识符的作用和命名规则
2022-06-18 17:49:09
![](https://img.aspxhome.com/file/2023/1/72601_0s.jpg)
使用Springboot自定义注解,支持SPEL表达式
2023-11-20 01:18:58
![](https://img.aspxhome.com/file/2023/4/99874_0s.png)
java多线程入门知识及示例程序
2021-11-30 03:17:58
浅谈Java编程中string的理解与运用
2021-05-31 22:15:44
Flutter 队列任务的实现
2023-07-07 17:25:14
Java8使用stream实现list中对象属性的合并(去重并求和)
2023-06-23 13:44:40
![](https://img.aspxhome.com/file/2023/9/62379_0s.png)
java实现简单单链表
2023-10-30 09:45:46
![](https://img.aspxhome.com/file/2023/2/58722_0s.jpg)
java 图片验证码的实现代码
2023-11-09 13:33:52
Android编程实现AlertDialog自定义弹出对话框的方法示例
2022-09-13 19:34:45
springboot+springmvc实现登录拦截
2023-04-26 19:23:26