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. 预览效果
    1、Word 

    2、Excel 

    3、Pdf 

    4、Txt
  4. 未解决的问题
    1. Pdf、txt文件只能在当前页显示,并导致后退键无效,请各位帮忙解决此两种文件和doc、xls一样在新的tab中打开
  5. 源码下载
posted @     阅读(39959)  评论(40编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示