ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.NET MVC在线预览Excel、Word、TXT、PDF文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3719字,纯文字阅读大概需要6分钟。
内容图文
代码:
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>publicstring 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 Htmlobject 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>publicstring 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 Htmlobject 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>publicstring PreviewTxt(string physicalPath, string url) { return Server.UrlDecode(url); } #endregion#region 预览Pdf ///<summary>/// 预览Pdf ///</summary>publicstring PreviewPdf(string physicalPath, string url) { return Server.UrlDecode(url); } #endregion } }
原文:http://www.cnblogs.com/s0611163/p/4699948.html
内容总结
以上是互联网集市为您收集整理的ASP.NET MVC在线预览Excel、Word、TXT、PDF文件全部内容,希望文章能够帮你解决ASP.NET MVC在线预览Excel、Word、TXT、PDF文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。