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