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

Source: Internet
Author: User

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

1. Function Description

Enter the file path and output the file preview information in the browser. After testing, the 360 speed (Chrome), IE9/10, and Firefox pass

2. classification file and Code Description

DemoFiles stores test files

Default. 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,                  missing, missing, missing, missing, missing, missing, missing, missing,                  missing, missing, 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, missing, missing,             missing, XlSaveAsAccessMode.xlNoChange, missing,             missing, missing, missing, missing);    excel.Quit();    //Open generated Html    Process process = new Process();    process.StartInfo.UseShellExecute = true;    process.StartInfo.FileName = outputFile;    process.Start();  }    

4. 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();  }

5. 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 ();}

6. WordPreview. cs Word preview class

7.Readme.txt describes the basic functions and how to reference the Com component (first install the office). The components to be introduced include

Microsoft Word 15.0
In Microsoft Excel 15.0

Preview Effect

1. Word

2. Excel

3. Pdf

4. Txt

Unsolved Problems

Pdf and txt files can only be displayed on the current page, leading to invalid backend keys. Please help solve these two types of files and open them in the new tab like doc and xls.

5. Download source code

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.