日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net实现word、excel、ppt、pdf在线预览

發(fā)布時間:2024/3/24 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net实现word、excel、ppt、pdf在线预览 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

引言

之前項目需要,查找了office文檔在線預(yù)覽的解決方案,順便記錄一下,方便以后查詢。

方案一

直接在瀏覽器中打開Office文檔在頁面上的鏈接。會彈出如下窗口:

?

優(yōu)點:主流瀏覽器都支持。

缺點:Office文檔鏈接在瀏覽器中打開,會有如上圖的提示,需用戶自己選擇打開或者保存功能,如果客戶電腦上安裝迅雷下載軟件,會啟動迅雷下載,用戶體驗不好。

方案二

office文檔轉(zhuǎn)html,首先引入com組件中office庫,然后在程序集擴展中引入word,excel,ppt的dll。

?

然后F6生成,會報如下錯誤:

解決辦法:

?

?office文檔轉(zhuǎn)換html輔助類:

Office2HtmlHelper.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; namespace Wolfy.OfficePreview {public class Office2HtmlHelper{/// <summary>/// Word轉(zhuǎn)成Html/// </summary>/// <param name="path">要轉(zhuǎn)換的文檔的路徑</param>/// <param name="savePath">轉(zhuǎn)換成html的保存路徑</param>/// <param name="wordFileName">轉(zhuǎn)換成html的文件名字</param>public static void Word2Html(string path, string savePath, string wordFileName){Word.ApplicationClass word = new Word.ApplicationClass();Type wordType = word.GetType();Word.Documents docs = word.Documents;Type docsType = docs.GetType();Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });Type docType = doc.GetType();string strSaveFileName = savePath + wordFileName + ".html";object saveFileName = (object)strSaveFileName;docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);}/// <summary>/// Excel轉(zhuǎn)成Html/// </summary>/// <param name="path">要轉(zhuǎn)換的文檔的路徑</param>/// <param name="savePath">轉(zhuǎn)換成html的保存路徑</param>/// <param name="wordFileName">轉(zhuǎn)換成html的文件名字</param>public static void Excel2Html(string path, string savePath, string wordFileName){string str = string.Empty;Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();Microsoft.Office.Interop.Excel.Workbook workbook = null;Microsoft.Office.Interop.Excel.Worksheet worksheet = null;workbook = repExcel.Application.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];object htmlFile = savePath + wordFileName + ".html";object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;workbook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);object osave = false;workbook.Close(osave, Type.Missing, Type.Missing);repExcel.Quit();}/// <summary>/// ppt轉(zhuǎn)成Html/// </summary>/// <param name="path">要轉(zhuǎn)換的文檔的路徑</param>/// <param name="savePath">轉(zhuǎn)換成html的保存路徑</param>/// <param name="wordFileName">轉(zhuǎn)換成html的文件名字</param>public static void PPT2Html(string path, string savePath, string wordFileName){Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();string strSourceFile = path;string strDestinationFile = savePath + wordFileName + ".html";Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);prsPres.Close();ppApp.Quit();}} }

Office2Html.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Office2Html.aspx.cs" Inherits="Wolfy.OfficePreview.Office2Html" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title> </head> <body><form id="form1" runat="server"><div><asp:Button Text="Word轉(zhuǎn)Html" ID="btnWord" runat="server" CommandArgument="docx" OnClick="btnWord_Click" /><asp:Button ID="btnExcel" Text="Excel轉(zhuǎn)Html" runat="server" CommandArgument="xlsx" OnClick="btnWord_Click" /><asp:Button ID="btnPPT" Text="PPT轉(zhuǎn)Html" runat="server" CommandArgument="ppt" OnClick="btnWord_Click" /></div></form> </body> </html>

?Office2Html.aspx.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace Wolfy.OfficePreview {public partial class Office2Html : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void btnWord_Click(object sender, EventArgs e){Button btn = sender as Button;switch (btn.CommandArgument){case "docx":Office2HtmlHelper.Word2Html(MapPath("/Doc/分析某網(wǎng)站的SEO策略(外鏈篇).doc"), MapPath("/Html/"), "分析某網(wǎng)站的SEO策略(外鏈篇)");break;case "xlsx":Office2HtmlHelper.Excel2Html(MapPath("/Excel/1994-2013北京市歷年最低工資標(biāo)準(zhǔn).xlsx"), MapPath("/Html/"), "1994-2013北京市歷年最低工資標(biāo)準(zhǔn)");break;case "ppt":Office2HtmlHelper.PPT2Html(MapPath("/PPT/23種設(shè)計模式詳解.ppt"), MapPath("/Html/"), "23種設(shè)計模式詳解");break;default:break;}}} }

?

測試結(jié)果:

這里為了測試特找了含有圖片的office文檔,瀏覽正常:

?要求:機器需安裝office,并且office環(huán)境是純凈的,所謂純凈就是不能有多個版本,lz曾經(jīng)在電腦上安裝過wps,被害苦了總是報如下錯誤:

報這個錯誤,只能哭了,網(wǎng)上的關(guān)于00046的解決辦法都嘗試了,不行。然后不得不重新安裝office,然后笑了。最好安裝office完整版,因為原來裝的不是完整版,不知道有沒有這方面的原因,也沒有測試,建議完整版。

方案三

office文檔轉(zhuǎn)PDF,PDF轉(zhuǎn)swf,使用flexpaper+swftools實現(xiàn)在線瀏覽。

在操作office2007時,需安裝SaveAsPDFandXPS.exe?,安裝成功后,如圖所示:

只有安裝了SaveAsPDFandXPS.exe,程序操作office文檔,才有office文檔另存為pdf文件。office2010不需要安裝了,內(nèi)置有這個功能。

?核心代碼:

Office2PDFHelper.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Core; namespace Wolfy.OfficePreview {/// <summary>/// Office2Pdf 將Office文檔轉(zhuǎn)化為pdf/// </summary>public class Office2PDFHelper{public Office2PDFHelper(){//// TODO: 在此處添加構(gòu)造函數(shù)邏輯//}/// <summary>/// Word轉(zhuǎn)換成pdf/// </summary>/// <param name="sourcePath">源文件路徑</param>/// <param name="targetPath">目標(biāo)文件路徑</param>/// <returns>true=轉(zhuǎn)換成功</returns>public static bool DOCConvertToPDF(string sourcePath, string targetPath){bool result = false;Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;object paramMissing = Type.Missing;Word.ApplicationClass wordApplication = new Word.ApplicationClass();Word.Document wordDocument = null;try{object paramSourceDocPath = sourcePath;string paramExportFilePath = targetPath;Word.WdExportFormat paramExportFormat = exportFormat;bool paramOpenAfterExport = false;Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;int paramStartPage = 0;int paramEndPage = 0;Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;bool paramIncludeDocProps = true;bool paramKeepIRM = true;Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;bool paramDocStructureTags = true;bool paramBitmapMissingFonts = true;bool paramUseISO19005_1 = false;wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing);if (wordDocument != null)wordDocument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat, paramOpenAfterExport,paramExportOptimizeFor, paramExportRange, paramStartPage,paramEndPage, paramExportItem, paramIncludeDocProps,paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,paramBitmapMissingFonts, paramUseISO19005_1,ref paramMissing);result = true;}catch{result = false;}finally{if (wordDocument != null){wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);wordDocument = null;}if (wordApplication != null){wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);wordApplication = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}return result;}/// <summary>/// 把Excel文件轉(zhuǎn)換成PDF格式文件 /// </summary>/// <param name="sourcePath">源文件路徑</param>/// <param name="targetPath">目標(biāo)文件路徑</param>/// <returns>true=轉(zhuǎn)換成功</returns>public static bool XLSConvertToPDF(string sourcePath, string targetPath){bool result = false;Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;object missing = Type.Missing;Excel.ApplicationClass application = null;Excel.Workbook workBook = null;try{application = new Excel.ApplicationClass();object target = targetPath;object type = targetType;workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,missing, missing, missing, missing, missing, missing, missing, missing, missing);workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);result = true;}catch{result = false;}finally{if (workBook != null){workBook.Close(true, missing, missing);workBook = null;}if (application != null){application.Quit();application = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}return result;}///<summary> /// 把PowerPoint文件轉(zhuǎn)換成PDF格式文件 ///</summary> ///<param name="sourcePath">源文件路徑</param> ///<param name="targetPath">目標(biāo)文件路徑</param> ///<returns>true=轉(zhuǎn)換成功</returns> public static bool PPTConvertToPDF(string sourcePath, string targetPath){bool result;PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;object missing = Type.Missing;PowerPoint.ApplicationClass application = null;PowerPoint.Presentation persentation = null;try{application = new PowerPoint.ApplicationClass();persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);result = true;}catch{result = false;}finally{if (persentation != null){persentation.Close();persentation = null;}if (application != null){application.Quit();application = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();}return result;}} }

?Office2PDF.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Office2PDF.aspx.cs" Inherits="Wolfy.OfficePreview.Office2PDF" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title></title> </head> <body><form id="form1" runat="server"><div><asp:Button Text="Word轉(zhuǎn)PDF" ID="btnWord" runat="server" CommandArgument="docx" OnClick="btnWord_Click" /><asp:Button ID="btnExcel" Text="Excel轉(zhuǎn)PDF" runat="server" CommandArgument="xlsx" OnClick="btnWord_Click" /><asp:Button ID="btnPPT" Text="PPT轉(zhuǎn)PDF" runat="server" CommandArgument="ppt" OnClick="btnWord_Click" /></div></form> </body> </html>

?

Office2PDF.aspx.cs

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace Wolfy.OfficePreview {public partial class Office2PDF : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void btnWord_Click(object sender, EventArgs e){Button btn = sender as Button;switch (btn.CommandArgument){case "docx":Office2PDFHelper.DOCConvertToPDF(MapPath("/Doc/分析某網(wǎng)站的SEO策略(外鏈篇).doc"), MapPath("/PDF/分析某網(wǎng)站的SEO策略(外鏈篇).pdf"));break;case "xlsx":Office2PDFHelper.XLSConvertToPDF(MapPath("/Excel/1994-2013北京市歷年最低工資標(biāo)準(zhǔn).xlsx"), MapPath("/PDF/1994-2013北京市歷年最低工資標(biāo)準(zhǔn).pdf"));break;case "ppt":Office2PDFHelper.PPTConvertToPDF(MapPath("/PPT/23種設(shè)計模式詳解.ppt"), MapPath("/PDF/23種設(shè)計模式詳解.pdf"));break;default:break;}}} }

?

測試結(jié)果:

?

此方案office轉(zhuǎn)pdf文件的過程的要求與方案二要求相同。

pdf轉(zhuǎn)換完成后,就可以將pdf轉(zhuǎn)換為swf,使用flexpaper+swftools實現(xiàn)在線瀏覽了,可參考我之前的一篇文章:

FlexPaper+SWFTool+操作類=在線預(yù)覽PDF

方案四

office文檔直接轉(zhuǎn)換為swf,使用flexpaper+swftool實現(xiàn)在先瀏覽。

office直接轉(zhuǎn)換為swf,這里使用flashpaper來實現(xiàn):

FlashPaper是一個虛擬打印機,可將word文件直接轉(zhuǎn)化成swf格式文件(.doc.xls .txt .pdf等文件都可以正常生成SWF格式)。

這里只貼出核心代碼:

Office2Swf.aspx.cs

using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;namespace Wolfy.OfficePreview {public partial class Office2Swf : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void btnWord_Click(object sender, EventArgs e){Button btn = sender as Button;switch (btn.CommandArgument){case "docx":ConvertOffice2Swf(MapPath("/Doc/分析某網(wǎng)站的SEO策略(外鏈篇).doc"), MapPath("/SWF/分析某網(wǎng)站的SEO策略(外鏈篇).swf"));break;case "xlsx":Office2PDFHelper.XLSConvertToPDF(MapPath("/Excel/1994-2013北京市歷年最低工資標(biāo)準(zhǔn).xlsx"), MapPath("/SWF/1994-2013北京市歷年最低工資標(biāo)準(zhǔn).swf"));break;case "ppt":Office2PDFHelper.PPTConvertToPDF(MapPath("/PPT/23種設(shè)計模式詳解.ppt"), MapPath("/SWF/23種設(shè)計模式詳解.swf"));break;default:break;}}/// <summary>/// office 轉(zhuǎn)swf/// </summary>/// <param name="officePath">要轉(zhuǎn)換的office文檔路徑</param>/// <param name="swfPath">轉(zhuǎn)換后swf的路徑</param>private void ConvertOffice2Swf(string officePath, string swfPath){Process process = new Process(); //創(chuàng)建進程對象 ProcessStartInfo startInfo = new ProcessStartInfo();string paperroot = @"C:\Program Files\Macromedia\FlashPaper 2\FlashPrinter.exe";//這里是FlashPrinter的路徑string docFile = officePath;string swfFile = swfPath;startInfo.FileName = paperroot;startInfo.Arguments = docFile + " -o " + swfFile;startInfo.UseShellExecute = false; //不使用系統(tǒng)外殼程序啟動 startInfo.RedirectStandardInput = false; //不重定向輸入 startInfo.RedirectStandardOutput = false; //重定向輸出 startInfo.CreateNoWindow = true; //不創(chuàng)建窗口 process.StartInfo = startInfo;process.Start(); if (process != null)process.Close();}} }

鑒于測試時,flashpaper在將office文檔轉(zhuǎn)換為swf的時候,在使用flexpaper的瀏覽時,出現(xiàn)轉(zhuǎn)換的內(nèi)容為空,猜測:flexpaper能打開的swf文件與flashpaper轉(zhuǎn)的swf文件不兼容。最后使用flashpaper將office文檔轉(zhuǎn)換為pdf,然后走方案三,pdf轉(zhuǎn)swf的步驟。另外本地測試時,沒問題。將項目部署在IIS上,不能瀏覽,出現(xiàn)卡死的情況,調(diào)試發(fā)現(xiàn),文件太大,在office還沒完全轉(zhuǎn)換為pdf的情況下,swftool工具就去尋找pdf文件,出現(xiàn)錯誤。

IIS上,無法瀏覽,查詢網(wǎng)上解決方案,和權(quán)限這塊有關(guān),按照步驟設(shè)置了,未果,有點遺憾。

方案五

使用點聚公司的weboffice控件,測試后發(fā)現(xiàn)兼容性較差,放棄。有興趣的可以研究一下。

方案六

office轉(zhuǎn)pdf后,直接瀏覽器打開,此方案鑒于目前主流瀏覽器都集成adobe reader功能,可實現(xiàn)直接打開PDF文件。將pdf文件鏈接可直接打開。

必要條件:本地需安裝adobe reader類似軟件。

方案七

http://blogs.office.com/2013/04/10/office-web-viewer-view-office-documents-in-a-browser/

方案八

web在線打印,打印閱覽,打印維護,打印設(shè)計

總結(jié)

鑒于項目情況選擇一個適合的方案,其中有方案只是曲線救國,但是同樣能達(dá)到要求。如果您覺得對你有所幫助,不妨推薦一下,讓更多的人都能看到,謝謝你能看到文章最后。

demo:

鏈接:https://pan.baidu.com/s/1hqEpx5a?密碼:gupg

參考文章:

http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html

http://www.cnblogs.com/lexlin/articles/2478027.html

http://www.cnblogs.com/gossip/p/3473024.html

http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html

  • 博客地址:http://www.cnblogs.com/wolf-sun/?
    博客版權(quán):如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起討論,共同進步! 再次感謝您耐心的讀完本篇文章。

閱讀原文

總結(jié)

以上是生活随笔為你收集整理的asp.net实现word、excel、ppt、pdf在线预览的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。