Asp.net allows you to preview Word, Excel, PDF, and Txt files in a browser (with source code)

Source: Internet
Author: User

Function Description: Enter the file path and output the file preview information in the browser. It is tested to be (Chrome), IE9/10, and Firefox stores the file Default by using the classification file and code description DemoFiles. aspx startup page ExcelPreview. cs Excel preview class public static void Priview (System. web. UI. page p, string inFilePath, string outDirPath = "") {Microsoft. office. interop. excel. application excel = null; Microsoft. office. interop. excel. workbook xls = null; excel = new Microsoft. office. interop. excel. application (); object missing = Type. missing; object trueObject = true; excel. visible = false; excel. displayAlerts = false; string randomName = DateTime. now. ticks. toString (); // output fileName xls = excel. workbooks. open (inFilePath, missing, trueObject, missing, and missing ); // Save Excel to Html object format = Microsoft. office. interop. excel. xlFileFormat. xlHtml; Workbook wsCurrent = xls; // (Workbook) wsEnumerator. current; String outputFile = outDirPath + randomName + ". html "; wsCurrent. saveAs (outputFile, format, missing, XlSaveAsAccessMode. xlNoChange, missing, missing); excel. quit (); // Open generated Html Process process = new Process (); process. startInfo. useShellExecute = true; process. startInfo. fileName = outputFile; process. start ();} PDfPreview. cs Pdf preview class public static void Priview (System. web. UI. page p, string inFilePath) {p. response. contentType = "Application/pdf"; string fileName = inFilePath. substring (inFilePath. lastIndexOf ('\') + 1); p. response. addHeader ("content-disposition", "filename =" + fileName); p. response. writeFile (inFilePath); p. response. end ();} TextFilePreview. cs text file Preview class public static void Preview (System. web. UI. page p, string inFilePath) {string fileName = inFilePath. substring (inFilePath. lastIndexOf ('\') + 1); p. response. contentType = "text/plain"; p. response. contentEncoding = System. text. encoding. UTF8; // keep the same encoding format as the file p. response. addHeader ("content-disposition", "filename =" + fileName); p. response. writeFile (inFilePath); p. response. end ();} WordPreview. cs Word Preview

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.