麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

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

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

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

具體實現(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. /// 在線預覽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 預覽Excel 
  42. /// <summary> 
  43. /// 預覽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 預覽Word 
  67. /// <summary> 
  68. /// 預覽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 預覽Txt 
  92. /// <summary> 
  93. /// 預覽Txt 
  94. /// </summary> 
  95. public string PreviewTxt(string physicalPath, string url) 
  96. return Server.UrlDecode(url); 
  97. #endregion 
  98. #region 預覽Pdf 
  99. /// <summary> 
  100. /// 預覽Pdf 
  101. /// </summary> 
  102. public string PreviewPdf(string physicalPath, string url) 
  103. return Server.UrlDecode(url); 
  104. #endregion 

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 牛牛a级毛片在线播放 | 福利在线小视频 | 欧美aⅴ视频 | 色妇视频 | 亚州欧美在线 | 久色乳综合思思在线视频 | 成人偷拍片视频在线观看 | 国产超碰人人做人人爱 | 日本免费一区二区三区四区 | 精品国产乱码久久久久久久久 | 欧美激情综合在线 | 久久免费视频5 | 国产精品aⅴ | www.99re14.com| 精品国产精品久久 | 三人弄娇妻高潮3p视频 | 成人免费电影在线观看 | 在线播放污 | 久久精品国产清自在天天线 | 久久艳片 | 午夜视频在线免费播放 | 福利在线免费视频 | 看一级大毛片 | 青青操国产 | 极品销魂一区二区三区 | omofun 动漫在线观看 | 日韩做爰视频免费 | 做爰xxxⅹ性护士hd在线 | 色视频91| 国产午夜精品一区二区三区免费 | 色综合久久久久久久久久 | 一区二区视频在线看 | 99麻豆久久久国产精品免费 | 免费特黄 | 国产视频在线一区 | 极品大长腿啪啪高潮露脸 | 欧美日韩色 | 最新中文字幕在线视频 | 一级美女大片 | 4p一女两男做爰在线观看 | 蜜桃一本色道久久综合亚洲精品冫 |