代码:
using System; System.Collections.Generic; System.Linq; System.Web; System.Web.Mvc; Microsoft.Office.Interop.Excel; System.Diagnostics; System.IO; 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": .xlsx: htmlUrl = PreviewExcel(physicalPath,url); break; .doc.docx PreviewWord(physicalPath,1)">.txt PreviewTxt(physicalPath,1)">.pdf PreviewPdf(physicalPath,1)">.jpg.jpeg.bmp.gif.png PreviewImg(physicalPath,1)">default PreviewOther(physicalPath,1)">; } return Redirect(Url.Content(htmlUrl)); } #endregion #region 预览Excel 预览Excel </summary> string PreviewExcel(string physicalPath, url) { Microsoft.Office.Interop.Excel.Application application = null; Microsoft.Office.Interop.Excel.Workbook workbook = ; application = new Microsoft.Office.Interop.Excel.Application(); object missing = Type.Missing; object trueObject = true; application.Visible = false; application.DisplayAlerts = ; workbook = application.Workbooks.Open(physicalPath,missing,trueObject,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,XlSaveAsAccessMode.xlNoChange,missing); workbook.Close(); application.Quit(); return Path.GetDirectoryName(Server.UrlDecode(url)) + htmlName; } #region 预览Word 预览Word string PreviewWord( url) { Microsoft.Office.Interop.Word._Application application = ; Microsoft.Office.Interop.Word._Document doc = Microsoft.Office.Interop.Word.Application(); ; application.DisplayAlerts = WdAlertLevel.wdAlertsNone; doc = application.Documents.Open(physicalPath,1)"> Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; htmlName; doc.SaveAs(outputFile,missing); doc.Close(); application.Quit(); #region 预览Txt 预览Txt string PreviewTxt( Server.UrlDecode(url); } #region 预览Pdf 预览Pdf string PreviewPdf(#region 预览图片 预览图片 string PreviewImg(#region 预览其他文件 预览其他文件 string PreviewOther(#endregion } }
原文地址:https://www.cnblogs.com/s0611163
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。