http://www.cnblogs.com/gossip/p/3473024.html
Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- 功能说明
- 输入文件路径,在浏览器输出文件预览信息,经测试360极速(Chrome)、IE9/10、Firefox通过
- 分类文件及代码说明
- 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();
} - 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(); } - 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(); } - WordPreview.cs Word预览类
- 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中打开
- 源码下载
Oracle创建触发器实现主键自增
org.hibernate.LazyInitializationException
no appropriate service handler found The Connection descriptor used by the client was: localhost:1521:myorcl
恶补java基础 位运算符
Extjs Toolbar 当做弹出菜单
解决javaWEB 下载文件中文名称乱码问题
SpringMVC HandlerMethodArgumentResolver自定义参数转换器 针对HashMap失效的问题
Jackson2.1.4 序列化对象时,过滤null的属性 empty的属性 default的属性
Jackson2.1.4 序列化格式化时间
- 最新文章
-
redis安装、测试&集群的搭建&踩过的坑
jdbc、Mybatis插入数据主键回显的实现方法
淘淘商城学习笔记 之 上传图片到远程服务器,图片的回显出现的bug
SSM简单整合教程&测试事务
Vue实例
Vue介绍
jQuery之DOM操作
JQuery选择器
jQuery介绍
CSS3新增功能
- 热门文章
-
页面布局
页面组件
JavaScript事件及BOM和DOM
JavaScript内置对象
COUNT(*)与COUNT(列名)的区别(转)
hibernateTemplate的load方法
Could not load oracle/sql/converter_xcharset/lx20354.glb.
ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
java.lang.OutOfMemoryError: PermGen space
org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.chen.vo.Dept.parentId
Copyright © 2011-2022 走看看
