国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET

2024-07-10 13:29:16
字體:
供稿:網(wǎng)友

這篇文章主要用asp.net技術(shù)實(shí)現(xiàn)直接在線預(yù)覽word、excel、txt文件,有需要的朋友可以參考下

具體實(shí)現(xiàn)過程不多說了,直接貼代碼了。

 

 
  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. /// <summary> 
  12. /// 在線預(yù)覽Office文件 
  13. /// </summary> 
  14. public class OfficeViewController : Controller 
  15. #region Index頁面 
  16. /// <summary> 
  17. /// Index頁面 
  18. /// </summary> 
  19. /// <param name="url">例:/uploads/......XXX.xls</param> 
  20. public ActionResult Index(string url) 
  21. string physicalPath = Server.MapPath(Server.UrlDecode(url)); 
  22. string extension = Path.GetExtension(physicalPath); 
  23. string htmlUrl = ""
  24. switch (extension.ToLower()) 
  25. case ".xls"
  26. case ".xlsx"
  27. htmlUrl = PreviewExcel(physicalPath, url); 
  28. break
  29. case ".doc"
  30. case ".docx"
  31. htmlUrl = PreviewWord(physicalPath, url); 
  32. break
  33. case ".txt"
  34. htmlUrl = PreviewTxt(physicalPath, url); 
  35. break
  36. case ".pdf"
  37. htmlUrl = PreviewPdf(physicalPath, url); 
  38. break
  39. return Redirect(Url.Content(htmlUrl)); 
  40. #endregion 
  41. #region 預(yù)覽Excel 
  42. /// <summary> 
  43. /// 預(yù)覽Excel 
  44. /// </summary> 
  45. public string PreviewExcel(string physicalPath, string url) 
  46. Microsoft.Office.Interop.Excel.Application application = null
  47. Microsoft.Office.Interop.Excel.Workbook workbook = null
  48. application = new Microsoft.Office.Interop.Excel.Application(); 
  49. object missing = Type.Missing; 
  50. object trueObject = true
  51. application.Visible = false
  52. application.DisplayAlerts = false
  53. workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing, 
  54. missing, missing, missing, missing, missing, missing, missing, missing, missing); 
  55. //Save Excel to Html 
  56. object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; 
  57. string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"
  58. String outputFile = Path.GetDirectoryName(physicalPath) + "//" + htmlName; 
  59. workbook.SaveAs(outputFile, format, missing, missing, missing, 
  60. missing, XlSaveAsAccessMode.xlNoChange, missing, 
  61. missing, missing, missing, missing); 
  62. workbook.Close(); 
  63. application.Quit(); 
  64. return Path.GetDirectoryName(Server.UrlDecode(url)) + "//" + htmlName; 
  65. #endregion 
  66. #region 預(yù)覽Word 
  67. /// <summary> 
  68. /// 預(yù)覽Word 
  69. /// </summary> 
  70. public string PreviewWord(string physicalPath, string url) 
  71. Microsoft.Office.Interop.Word._Application application = null
  72. Microsoft.Office.Interop.Word._Document doc = null
  73. application = new Microsoft.Office.Interop.Word.Application(); 
  74. object missing = Type.Missing; 
  75. object trueObject = true
  76. application.Visible = false
  77. application.DisplayAlerts = WdAlertLevel.wdAlertsNone; 
  78. doc = application.Documents.Open(physicalPath, missing, trueObject, missing, missing, missing, 
  79. missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); 
  80. //Save Excel to Html 
  81. object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; 
  82. string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html"
  83. String outputFile = Path.GetDirectoryName(physicalPath) + "//" + htmlName; 
  84. doc.SaveAs(outputFile, format, missing, missing, missing, 
  85. missing, XlSaveAsAccessMode.xlNoChange, missing, 
  86. missing, missing, missing, missing); 
  87. doc.Close(); 
  88. application.Quit(); 
  89. return Path.GetDirectoryName(Server.UrlDecode(url)) + "//" + htmlName; 
  90. #endregion 
  91. #region 預(yù)覽Txt 
  92. /// <summary> 
  93. /// 預(yù)覽Txt 
  94. /// </summary> 
  95. public string PreviewTxt(string physicalPath, string url) 
  96. return Server.UrlDecode(url); 
  97. #endregion 
  98. #region 預(yù)覽Pdf 
  99. /// <summary> 
  100. /// 預(yù)覽Pdf 
  101. /// </summary> 
  102. public string PreviewPdf(string physicalPath, string url) 
  103. return Server.UrlDecode(url); 
  104. #endregion 

以上就是針對直接在線預(yù)覽word、excel、text、pdf文件的全部內(nèi)容,希望大家喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青冈县| 大冶市| 二连浩特市| 丹东市| 金坛市| 吉安县| 宝山区| 句容市| 枝江市| 江永县| 喀喇沁旗| 卫辉市| 方正县| 淳安县| 天水市| 甘谷县| 虹口区| 织金县| 新昌县| 随州市| 苏州市| 石首市| 定陶县| 旬邑县| 尉犁县| 宽甸| 通州区| 陵川县| 阿瓦提县| 米泉市| 新宁县| 大连市| 昭觉县| 南漳县| 乡城县| 琼结县| 于田县| 广水市| 长兴县| 通化市| 金山区|