LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

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

admin
2018年1月2日 21:46 本文热度 5415

1.功能说明

输入文件路径,在浏览器输出文件预览信息,经测试360极速(Chrome)、IE9/10、Firefox通过

2.分类文件及代码说明

DemoFiles 存放可测试文件

Default.aspx  启动页

ExcelPreview.cs  Excel预览类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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预览类

1
2
3
4
5
6
7
8
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  文本文件预览类

1
2
3
4
5
6
7
8
9
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),需引入的组件包括

  Microsoft Word 15.0
  Microsoft Excel 15.0

  预览效果

1、Word

2、Excel 

3、Pdf

4、Txt

未解决的问题

Pdf、txt文件只能在当前页显示,并导致后退键无效,请各位帮忙解决此两种文件和doc、xls一样在新的tab中打开

5.源码下载
附件:
FilePreviewInBrowser_jb51.rar

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

原文链接:http://www.cnblogs.com/gossip/p/3473024.html


该文章在 2018/1/2 21:46:39 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved