专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

直接在线预览Word、Excel、TXT文件之ASP.NET

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:36:44       共计:3634 浏览
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   }


}

以上就是针对直接在线预览word、excel、text、pdf文件的全部内容,希望大家喜欢。


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:WebOffice控件功能示例 | ·下一条:C#在线预览文档(word,excel,pdf,txt,png)

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有