Asp.net實現直接在瀏覽器預覽Word、Excel、PDF、Txt文件(附源碼)

  1. 功能說明
    1. 輸入文件路徑,在瀏覽器輸出文件預覽信息,經測試360極速(Chrome)、IE9/10、Firefox經過
  2. 分類文件及代碼說明 
    1. DemoFiles 存放可測試文件
    2. Default.aspx  啓動頁
    3. ExcelPreview.cs  Excel預覽類
        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預覽類
       
      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  文本文件預覽類
          
      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;  //保持和文件的編碼格式一致
              p.Response.AddHeader("content-disposition", "filename=" + fileName);
              p.Response.WriteFile(inFilePath);
              p.Response.End();
          }

       

    6. WordPreview.cs  Word預覽類
    7. Readme.txt  說明了基本功能及引用Com組件的方法(首先須要安裝office),需引入的組件包括
      1. Microsoft Word 15.0
      2. Microsoft Excel 15.0
  3. 預覽效果
    一、Word 

    二、Excel 

    三、Pdf 

    四、Txt
  4. 未解決的問題
    1. Pdf、txt文件只能在當前頁顯示,並致使後退鍵無效,請各位幫忙解決此兩種文件和doc、xls同樣在新的tab中打開
  5. 源碼下載
相關文章
相關標籤/搜索