分享

Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)

 实力决定地位 2015-03-28

Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)

 
    1. 功能说明 输入文件路径,在浏览器输出文件预览信息,经测试360极速(Chrome)、IE9/10、Firefox通过 分类文件及代码说明  DemoFiles 存放可测试文件 Default.aspx  启动页
       
    2. ExcelPreview.cs  Excel预览类 
    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预览类  
       
      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  文本文件预览类    
      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预览类 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 预览效果
      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文件只能在当前页显示,并导致后退键无效,请各位帮忙解决此两种文件和doc、xls一样在新的tab中打开

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多