首页 > 编程 > .NET > 正文

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

2024-07-10 13:29:16
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要用asp.net技术实现直接在线预览word、excel、txt文件,有需要的朋友可以参考下

具体实现过程不多说了,直接贴代码了。

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.Mvc; 
  6. using Microsoft.Office.Interop.Excel; 
  7. using System.Diagnostics; 
  8. using System.IO; 
  9. using Microsoft.Office.Interop.Word; 
  10. namespace Suya.Web.Apps.Areas.PMP.Controllers 
  11. { 
  12. /// <summary> 
  13. /// 在线预览Office文件 
  14. /// </summary> 
  15. public class OfficeViewController : Controller 
  16. { 
  17. #region Index页面 
  18. /// <summary> 
  19. /// Index页面 
  20. /// </summary> 
  21. /// <param name="url">例:/uploads/......XXX.xls</param> 
  22. public ActionResult Index(string url) 
  23. { 
  24. string physicalPath = Server.MapPath(Server.UrlDecode(url)); 
  25. string extension = Path.GetExtension(physicalPath); 
  26. string htmlUrl = ""; 
  27. switch (extension.ToLower()) 
  28. { 
  29. case ".xls": 
  30. case ".xlsx": 
  31. htmlUrl = PreviewExcel(physicalPath, url); 
  32. break; 
  33. case ".doc": 
  34. case ".docx": 
  35. htmlUrl = PreviewWord(physicalPath, url); 
  36. break; 
  37. case ".txt": 
  38. htmlUrl = PreviewTxt(physicalPath, url); 
  39. break; 
  40. case ".pdf": 
  41. htmlUrl = PreviewPdf(physicalPath, url); 
  42. break; 
  43. } 
  44. return Redirect(Url.Content(htmlUrl)); 
  45. } 
  46. #endregion 
  47. #region 预览Excel 
  48. /// <summary> 
  49. /// 预览Excel 
  50. /// </summary> 
  51. public string PreviewExcel(string physicalPath, string url) 
  52. { 
  53. Microsoft.Office.Interop.Excel.Application application = null; 
  54. Microsoft.Office.Interop.Excel.Workbook workbook = null; 
  55. application = new Microsoft.Office.Interop.Excel.Application(); 
  56. object missing = Type.Missing; 
  57. object trueObject = true; 
  58. application.Visible = false; 
  59. application.DisplayAlerts = false; 
  60. workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing, 
  61. missing, missing, missing, missing, missing, missing, missing, missing, missing); 
  62. //Save Excel to Html 
  63. object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; 
  64. string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; 
  65. String outputFile = Path.GetDirectoryName(physicalPath) + "//" + htmlName; 
  66. workbook.SaveAs(outputFile, format, missing, missing, missing, 
  67. missing, XlSaveAsAccessMode.xlNoChange, missing, 
  68. missing, missing, missing, missing); 
  69. workbook.Close(); 
  70. application.Quit(); 
  71. return Path.GetDirectoryName(Server.UrlDecode(url)) + "//" + htmlName; 
  72. } 
  73. #endregion 
  74. #region 预览Word 
  75. /// <summary> 
  76. /// 预览Word 
  77. /// </summary> 
  78. public string PreviewWord(string physicalPath, string url) 
  79. { 
  80. Microsoft.Office.Interop.Word._Application application = null; 
  81. Microsoft.Office.Interop.Word._Document doc = null; 
  82. application = new Microsoft.Office.Interop.Word.Application(); 
  83. object missing = Type.Missing; 
  84. object trueObject = true; 
  85. application.Visible = false; 
  86. application.DisplayAlerts = WdAlertLevel.wdAlertsNone; 
  87. doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing, 
  88. missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); 
  89. //Save Excel to Html 
  90. object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; 
  91. string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"; 
  92. String outputFile = Path.GetDirectoryName(physicalPath) + "//" + htmlName; 
  93. doc.SaveAs(outputFile, format, missing, missing, missing, 
  94. missing, XlSaveAsAccessMode.xlNoChange, missing, 
  95. missing, missing, missing, missing); 
  96. doc.Close(); 
  97. application.Quit(); 
  98. return Path.GetDirectoryName(Server.UrlDecode(url)) + "//" + htmlName; 
  99. } 
  100. #endregion 
  101. #region 预览Txt 
  102. /// <summary> 
  103. /// 预览Txt 
  104. /// </summary> 
  105. public string PreviewTxt(string physicalPath, string url) 
  106. { 
  107. return Server.UrlDecode(url); 
  108. } 
  109. #endregion 
  110. #region 预览Pdf 
  111. /// <summary> 
  112. /// 预览Pdf 
  113. /// </summary> 
  114. public string PreviewPdf(string physicalPath, string url) 
  115. { 
  116. return Server.UrlDecode(url); 
  117. } 
  118. #endregion 
  119. } 
  120. } 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表