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中打开
- 源码下载
递归快还是循环(迭代)快?
VS2010下测试程序性能瓶颈
Qt编程之实现在QFileDialog上添加自定义的widget
This application failed to start because it could not find or load the Qt platform plugin "windows"
网络设备Web登录检测工具device-phamer
Outlook数据提取工具readpst
Xamarin无法调试Android项目
Web应用扫描工具Wapiti
Xamarin 2017.11.1更新
- 最新文章
-
010 innerHtml的使用
009 案例:在表格中进行新增一行与删除一行
008 删除节点
007 替换节点
006 创建并新增元素节点和文本节点
005 节点的属性
编译android源码m、mm、mmm命令的使用
自己动手编译Android源码(超详细)
Android摄像头测量尺(Advanced Ruler Pro)使用方法
Android 上Camera分析
- 热门文章
-
android camera jni调用
Android Opencv NativeCameraView error in 5.0 lollipop versions (Bug #4185)
移植opencv2.4.9到android过程记录
opencv manager package was not found 解决办法
OpenCV-2.4.6-android-sdk 人脸识别demo搭建
cmake编译Debug和Release
VS2010使用静态编译的qt库(Qt 5)
cmake编译(编译目标)x86或x64
链接一个外部lib库的时候注意事项
C/C++ 指针的非空判断
Copyright © 2011-2022 走看看