Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
2013-12-13 14:34
941 查看
功能说明
输入文件路径,在浏览器输出文件预览信息,经测试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预览类
TextFilePreview.cs 文本文件预览类
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中打开
源码下载
输入文件路径,在浏览器输出文件预览信息,经测试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中打开
源码下载
相关文章推荐
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- 直接在线预览Word、Excel、TXT文件之ASP.NET
- asp.net 文件下载(txt,rar,pdf,word,excel,ppt)
- asp.net实现word、excel、ppt、pdf在线预览
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!
- [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!(续)
- 转载: Asp.net常见word,excel,ppt,pdf在线预览方案
- ASP.NET 在线下载Word,excel,txt文件,在线浏览html文件
- asp.net从数据库导出数据到word、excel、txt文本文件
- [Asp.net]常见word,excel,ppt,pdf在线预览方案(转)
- 通过Aspose对Word,Excel文件进行Pdf转换,实现在线预览
- ASP.NET保存PDF、Word和Excel文件到数据库