代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.NET
】
在线预览Word、Excel、TXT文件
作者:
/ 发布于
2016/8/25
/
509
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 } }
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3