日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Asp.net實(shí)現(xiàn)直接在瀏覽器預(yù)覽Word、Excel、PDF、Txt文件(附源碼)

 實(shí)力決定地位 2015-03-28

Asp.net實(shí)現(xiàn)直接在瀏覽器預(yù)覽Word、Excel、PDF、Txt文件(附源碼)

 
    1. 功能說明 輸入文件路徑,在瀏覽器輸出文件預(yù)覽信息,經(jīng)測(cè)試360極速(Chrome)、IE9/10、Firefox通過 分類文件及代碼說明  DemoFiles 存放可測(cè)試文件 Default.aspx  啟動(dòng)頁
       
    2. ExcelPreview.cs  Excel預(yù)覽類 
    3.  public static void Priview(System.Web.UI.Page p, string inFilePath, string outDirPath = '')word-spacing:="/" />     {word-spacing:="/" />         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預(yù)覽類  
       
      1.public static void Priview(System.Web.UI.Page p, string inFilePath)
      2.    {
      3.        p.Response.ContentType = 'Application/pdf';
      4.        string fileName = inFilePath.Substring(inFilePath.LastIndexOf('') + 1);
      5.        p.Response.AddHeader('content-disposition', 'filename=' + fileName);
      6.        p.Response.WriteFile(inFilePath);
      7.        p.Response.End();
      8.    }
      TextFilePreview.cs  文本文件預(yù)覽類    
      01.public static void Preview(System.Web.UI.Page p, string inFilePath)
      02.    {
      03.        string fileName = inFilePath.Substring(inFilePath.LastIndexOf('') + 1);
      04.        p.Response.ContentType = 'text/plain';
      05.        p.Response.ContentEncoding = System.Text.Encoding.UTF8;  //保持和文件的編碼格式一致
      06.        p.Response.AddHeader('content-disposition', 'filename=' + fileName);
      07.        p.Response.WriteFile(inFilePath);
      08.        p.Response.End();
      09.    }
      WordPreview.cs  Word預(yù)覽類 Readme.txt  說明了基本功能及引用Com組件的方法(首先需要安裝office),需引入的組件包括 Microsoft Word 15.0 Microsoft Excel 15.0
      data-cke-saved-src=http://image84.360doc.com/DownloadImg/2015/03/2810/51703751_1.png 預(yù)覽效果
      1、Word 
      data-cke-saved-src=http://image84.360doc.com/DownloadImg/2015/03/2810/51703751_2.png
      2、Excel 
      height=690
      3、Pdf 
      data-cke-saved-src=http://image84.360doc.com/DownloadImg/2015/03/2810/51703751_4.png
      4、Txt
      data-cke-saved-src=http://image84.360doc.com/DownloadImg/2015/03/2810/51703751_5.png
    5.  
    6. 未解決的問題 Pdf、txt文件只能在當(dāng)前頁顯示,并導(dǎo)致后退鍵無效,請(qǐng)各位幫忙解決此兩種文件和doc、xls一樣在新的tab中打開

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多