Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件

发布时间:2021-08-26 15:30:56 作者:小新
来源:亿速云 阅读:320

小编给大家分享一下Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1.功能说明

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

2.分类文件及代码说明

DemoFiles 存放可测试文件

Default.aspx  启动页

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),需引入的组件包括

  Microsoft Word 15.0
  Microsoft Excel 15.0

Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件

  预览效果

1、Word

Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件

2、Excel 

Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件

3、Pdf

Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件

4、Txt

Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件

未解决的问题

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

以上是“Asp.net如何实现直接在浏览器预览Word、Excel、PDF、Txt文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. html怎么实现在线预览word、excel、pdf等文件
  2. Java实现Word/Excel/TXT转PDF的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:css怎么更改上传按钮的样式

下一篇:dos命令行如何设置IE代理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》