asp.net implementation directly in the browser preview word, Excel, PDF, TXT file (with source code) _ Practical Skills

Source: Internet
Author: User

1. Function description

Input file path, in the browser output file preview information, tested 360 speed (Chrome), IE9/10, Firefox through

2. Classification file and code description

Demofiles to store testable 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.Inte Rop.
    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, MI
    Ssing, 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 encoding format consistent with 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 basic functionality and methods for referencing COM components (you first need to install Office), and the components to be introduced include

Microsoft Word 15.0
Microsoft Excel 15.0

Preview effect

1, Word

2, Excel

3, PDF

4, TXT

Unresolved issues

PDF, TXT file can only be displayed on the current page, and cause the back key is invalid, please help solve the two files and doc, xls like in the new tab open

5. SOURCE Download

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.