ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
2016-04-06 11:16
826 查看
话不多说直接上代码:
话不多说直接上代码:
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> /// <paramname="url">例:/uploads/......XXX.xls</param> public ActionResult Index(stringurl) { stringphysicalPath = Server.MapPath(Server.UrlDecode(url)); stringextension = Path.GetExtension(physicalPath); stringhtmlUrl = ""; 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; case ".jpg": case ".jpeg": case ".bmp": case ".gif": case ".png": htmlUrl = PreviewImg(physicalPath, url); break; default: htmlUrl = PreviewOther(physicalPath, url); break; } returnRedirect(Url.Content(htmlUrl)); } #endregion #region 预览Excel /// <summary> /// 预览Excel /// </summary> public string PreviewExcel(stringphysicalPath, string url) { Microsoft.Office.Interop.Excel.Application application = null; Microsoft.Office.Interop.Excel.Workbook workbook = null; application= new Microsoft.Office.Interop.Excel.Application(); objectmissing = Type.Missing; objecttrueObject = 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 Excelto Html objectformat = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; stringhtmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; StringoutputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName; workbook.SaveAs(outputFile, format, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); workbook.Close(); application.Quit(); returnPath.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName; } #endregion #region 预览Word /// <summary> /// 预览Word /// </summary> public string PreviewWord(stringphysicalPath, string url) { Microsoft.Office.Interop.Word._Application application = null; Microsoft.Office.Interop.Word._Document doc = null; application= new Microsoft.Office.Interop.Word.Application(); objectmissing = Type.Missing; objecttrueObject = 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 Excelto Html objectformat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; stringhtmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; StringoutputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName; doc.SaveAs(outputFile, format, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); doc.Close(); application.Quit(); returnPath.GetDirectoryName(Server.UrlDecode(url)) + "\\" + htmlName; } #endregion #region 预览Txt /// <summary> /// 预览Txt /// </summary> public string PreviewTxt(stringphysicalPath, string url) { returnServer.UrlDecode(url); } #endregion #region 预览Pdf /// <summary> /// 预览Pdf /// </summary> public string PreviewPdf(stringphysicalPath, string url) { returnServer.UrlDecode(url); } #endregion #region 预览图片 /// <summary> /// 预览图片 /// </summary> public string PreviewImg(stringphysicalPath, string url) { returnServer.UrlDecode(url); } #endregion #region 预览其他文件 /// <summary> /// 预览其他文件 /// </summary> public string PreviewOther(stringphysicalPath, string url) { returnServer.UrlDecode(url); } #endregion } }
相关文章推荐
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- Asp.Net WebApi Action命名中已‘Get’开头问题
- Adding the Test API in The ASP.NET Web API Help Page
- ASP.NET MVC @Helper辅助方法和@functons自定义函数的使用方法
- ireport导出中文pdf
- ASP.NET MVC使用ActionFilterAttribute实现权限限制的方法(附demo源码下载)
- Asp.Net 动态生成验证码
- ASP.NET 防盗链源码
- 微信开发(一) asp.net接入
- asp.net log4net的使用
- ASP.NET EF 使用LinqPad 快速学习Linq
- ASP.NET EF(LINQ/Lambda查询)
- ASP.NET MVC EF直接更新数据(不需查询)
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
- 监听CollaspingToolbarLayout折叠完成事件
- asp.net中的<% %>,<%= %>,><%#eval("") %><%$ %>的使用 - Hackerman
- ASP.NET Core中的依赖注入(2):依赖注入(DI)
- 【Raspberry Pi系列】4. ARM指令
- 玩转树莓派3——1新板上手
- ASP.NET中防止刷新页面造成表单重复提交