Asp.net實現(xiàn)直接在瀏覽器預覽Word、Excel、PDF、Txt文件(附源碼)
1.功能說明
輸入文件路徑,在瀏覽器輸出文件預覽信息,經(jīng)測試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

預覽效果
1、Word

2、Excel

3、Pdf

4、Txt

未解決的問題
Pdf、txt文件只能在當前頁顯示,并導致后退鍵無效,請各位幫忙解決此兩種文件和doc、xls一樣在新的tab中打開
5.源碼下載:http://xiazai.jb51.net/201612/yuanma/FilePreviewInBrowser_jb51.rar
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
JavaScript用JQuery呼叫Server端方法實現(xiàn)代碼與參考語法
從Javascript客戶端用JQuery呼叫Server端的方法,這也是一個大膽的嘗試,本人做了演示動畫以及參考語法,感興趣的朋友可以參考下,希望本人對你有所幫助2013-01-01
解決ASP.NET中"/"應用程序中的服務(wù)器錯誤的方法
這篇文章主要介紹了解決ASP.NET中"/"應用程序中的服務(wù)器錯誤的方法,,需要的朋友可以參考下2016-03-03
ASP.NET Core中預壓縮靜態(tài)文件的方法步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何預壓縮靜態(tài)文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-03-03
.NET?Core分布式鏈路追蹤框架的基本實現(xiàn)原理
這篇文章介紹了.NET?Core分布式鏈路追蹤框架的基本實現(xiàn)原理,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03
Visual studio 2017添加引用時報錯未能正確加載ReferenceManagerPackage包的解決方法
這篇文章主要介紹了VS2017添加引用時報錯未能正確加載ReferenceManagerPackage包的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

