直接在线预览Word、Excel、TXT文件之ASP.NET
具体实现过程不多说了,直接贴代码了。
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.Mvc; usingMicrosoft.Office.Interop.Excel; usingSystem.Diagnostics; usingSystem.IO; usingMicrosoft.Office.Interop.Word; namespaceSuya.Web.Apps.Areas.PMP.Controllers { ///<summary> ///在线预览Office文件 ///</summary> publicclassOfficeViewController:Controller { #regionIndex页面 ///<summary> ///Index页面 ///</summary> ///<paramname="url">例:/uploads/......XXX.xls</param> publicActionResultIndex(stringurl) { stringphysicalPath=Server.MapPath(Server.UrlDecode(url)); stringextension=Path.GetExtension(physicalPath); stringhtmlUrl=""; switch(extension.ToLower()) { case".xls": case".xlsx": htmlUrl=PreviewExcel(physicalPath,url); break; case".doc": case".docx": htmlUrl=PreviewWord(physicalPath,url); break; case".txt": htmlUrl=PreviewTxt(physicalPath,url); break; case".pdf": htmlUrl=PreviewPdf(physicalPath,url); break; } returnRedirect(Url.Content(htmlUrl)); } #endregion #region预览Excel ///<summary> ///预览Excel ///</summary> publicstringPreviewExcel(stringphysicalPath,stringurl) { Microsoft.Office.Interop.Excel.Applicationapplication=null; Microsoft.Office.Interop.Excel.Workbookworkbook=null; application=newMicrosoft.Office.Interop.Excel.Application(); objectmissing=Type.Missing; objecttrueObject=true; application.Visible=false; application.DisplayAlerts=false; workbook=application.Workbooks.Open(physicalPath,missing,trueObject,missing,missing,missing, missing,missing,missing,missing,missing,missing,missing,missing,missing); //SaveExceltoHtml objectformat=Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; stringhtmlName=Path.GetFileNameWithoutExtension(physicalPath)+".html"; StringoutputFile=Path.GetDirectoryName(physicalPath)+"\\"+htmlName; workbook.SaveAs(outputFile,format,missing,missing,missing, missing,XlSaveAsAccessMode.xlNoChange,missing, missing,missing,missing,missing); workbook.Close(); application.Quit(); returnPath.GetDirectoryName(Server.UrlDecode(url))+"\\"+htmlName; } #endregion #region预览Word ///<summary> ///预览Word ///</summary> publicstringPreviewWord(stringphysicalPath,stringurl) { Microsoft.Office.Interop.Word._Applicationapplication=null; Microsoft.Office.Interop.Word._Documentdoc=null; application=newMicrosoft.Office.Interop.Word.Application(); objectmissing=Type.Missing; objecttrueObject=true; application.Visible=false; application.DisplayAlerts=WdAlertLevel.wdAlertsNone; doc=application.Documents.Open(physicalPath,missing,trueObject,missing,missing,missing, missing,missing,missing,missing,missing,missing,missing,missing,missing,missing); //SaveExceltoHtml objectformat=Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; stringhtmlName=Path.GetFileNameWithoutExtension(physicalPath)+".html"; StringoutputFile=Path.GetDirectoryName(physicalPath)+"\\"+htmlName; doc.SaveAs(outputFile,format,missing,missing,missing, missing,XlSaveAsAccessMode.xlNoChange,missing, missing,missing,missing,missing); doc.Close(); application.Quit(); returnPath.GetDirectoryName(Server.UrlDecode(url))+"\\"+htmlName; } #endregion #region预览Txt ///<summary> ///预览Txt ///</summary> publicstringPreviewTxt(stringphysicalPath,stringurl) { returnServer.UrlDecode(url); } #endregion #region预览Pdf ///<summary> ///预览Pdf ///</summary> publicstringPreviewPdf(stringphysicalPath,stringurl) { returnServer.UrlDecode(url); } #endregion } }
以上就是针对直接在线预览word、excel、text、pdf文件的全部内容,希望大家喜欢。