ASP. net mvc Online Preview Excel, Word, TXT, PDF file, mvcpdf

Source: Internet
Author: User

ASP. net mvc Online Preview Excel, Word, TXT, PDF file, mvcpdf

Code:

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> /// preview the Office file online /// </summary> public class OfficeViewController: controller {# region Index page // <summary> // Index page // </summary> // <param name = "url"> example: /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; case ". jpg ": case ". jpeg ": case ". bmp ": case ". gif ": case ". png ": htmlUrl = PreviewImg (physicalPath, url); break; default: htmlUrl = PreviewOther (physicalPath, url); break;} return Redirect (Url. content (htmlUrl) ;}# endregion # region preview Excel /// <summary> /// preview 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 ); // 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, XlSaveAsAccessMode. xlNoChange, missing, missing); workbook. close (); application. quit (); return Path. getDirectoryName (Server. urlDecode (url) + "\" + htmlName ;} # endregion # region preview Word /// <summary> /// preview 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, and 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, XlSaveAsAccessMode. xlNoChange, missing, missing); doc. close (); application. quit (); return Path. getDirectoryName (Server. urlDecode (url) + "\" + htmlName ;} # endregion # region preview Txt // <summary> // preview Txt // </summary> public string PreviewTxt (string physicalPath, string url) {return Server. urlDecode (url) ;}# endregion # region preview Pdf /// <summary> /// preview Pdf /// </summary> public string PreviewPdf (string physicalPath, string url) {return Server. urlDecode (url) ;}# endregion # region preview image /// <summary> /// preview image /// </summary> public string PreviewImg (string physicalPath, string url) {return Server. urlDecode (url) ;}# endregion # region preview other files /// <summary> /// preview other files /// </summary> public string PreviewOther (string physicalPath, string url) {return Server. urlDecode (url) ;}# endregion }}View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.