类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)
1.首先上网搜索一下有什么解决方案 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf (1) a.首先添加引用: using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word;
// <summary> /// word转成html // </summary> /// <param name="path">要转换的文档的路径</param> /// <param name="savePath">转换成的html的保淳?/param> /// <param name="wordFileName">转换后html文件的名譸aram> private static void WordToHtml(string path,string savePath,string wordFileName) { Word.ApplicationClass word = new Word.ApplicationClass(); Type wordwordType = word.GetType(); Word.Documents docs = word.Documents; Type docsdocsType = docs.GetType(); Word.Document doc = (Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,new Object[] {(object)path,true,true }); Type docdocType = doc.GetType(); string strSaveFileName = savePath + wordFileName + ".html"; object saveFileName = (object)strSaveFileName; docType.InvokeMember("SaveAs",doc,new object[] { saveFileName,Word.WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close",null); wordType.InvokeMember("Quit",word,null); } /// <summary> /// excel 转换为html /// </summary> /// <param name="path">要转换的文档的路径</param> /// <param name="savePath">转换成的html的保淳?/param> /// <param name="wordFileName">转换后html文件的名譸aram> public static void ExcelToHtml(string path,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); worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[ object htmlFile = savePath + wordFileName + ".html"; object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; workbook.SaveAs(htmlFile,ofmt,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing); object osave = false; workbook.Close(osave,Type.Missing); repExcel.Quit(); } /// <summary> /// ppt转换为html /// </summary> /// <param name="path">要转换的文档的路径</param> /// <param name="savePath">转换成的html的保淳?/param> /// <param name="wordFileName">转换后html文件的名譸aram> public static void PPTToHtml(string path,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(); }
c.接下来就是调用方法: WordToHtml(string path,string wordFileName);ExcelToHtml(string path,string wordFileName);PPTToHtml(string path,string wordFileName) 实现相应文档的转换 大家看一下这样的效果确实感觉挺丑的,特别是word,excel格式都没有了,这样的效果是难以满足要求的,那接下来我们就学习第二种方案。。。。 ? (2).在网上搜索如何和将office文档的pdf文档转换为flash,实现在线预览,你会发现有好几种解决方案 a.使用flashpaper将需要的文档通过简单的设置转换为SWF格式的Flash,扩展阅读:http://baike.baidu.com/view/917746.htm,不过由于我的电脑室win7,而flashpaper又不支持win7,所以只好 放弃此种方案。 b.在网上发现可以使用swftools(http://www.swftools.org/谢天谢地啊,它支持win7)将pdf格式的文件转换为flash,但是不能讲office文档转换为flash,那怎么办呢,难道这种方法又不行,可是仔 细想一下如果我们能将office文档先转换为pdf文件不就解决问题了吗,突然之间隐隐约约觉得使用office软件就可实现这一功能,于是打开word看一下究竟 哈哈果然有,不过要先装一个插件 一切准备工作结束以后我们就可以进行编码了 c.首先说一下swftools使用:在命令行中运行pdf2swf src.pdf des.swf一般能满足需求。然后就行主要的代码的编写(即将office文档转换为pdf文档) 编写Office2Pdf.cs类 ? ? sing System; sing System.Data; sing System.Configuration; sing System.Web; sing System.Web.Security; sing System.Web.UI; sing System.Web.UI.WebControls; sing System.Web.UI.WebControls.WebParts; sing System.Web.UI.HtmlControls; using Word = Microsoft.Office.Interop.Word; using Excel = Microsoft.Office.Interop.Excel; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Core; /// <summary> /// Office 将Office文档转化为pdf /// </summary> public class Office { public Office() { // // TODO: 驭构札逻辑 // } /// <summary> /// Word转换成pdf /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目炳路径</param> /// <returns>true=转换成功</returns> public 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 = int paramEndPage = 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 paramUseISO= false; wordDocument = wordApplication.Documents.Open( ref paramSourceDocPath,ref paramMissing,ref paramMissing); if (wordDocument != null) wordDocument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat,paramOpenAfterExport,paramExportOptimizeFor,paramExportRange,paramStartPage,paramEndPage,paramExportItem,paramIncludeDocProps,paramKeepIRM,paramCreateBookmarks,paramDocStructureTags,paramBitmapMissingFonts,paramUseISO ref paramMissing); result = true; } catch { result = false; } finally { if (wordDocument != null) { wordDocument.Close(ref paramMissing,ref paramMissing); wordDocument = null; } if (wordApplication != null) { wordApplication.Quit(ref paramMissing,ref paramMissing); wordApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } /// <summary> /// 把Excel文件转换成PDF格件 /// </summary> /// <param name="sourcePath">源文件路径</param> /// <param name="targetPath">目炳路径</param> /// <returns>true=转换成功</returns> public 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); workBook.ExportAsFixedFormat(targetType,target,Excel.XlFixedFormatQuality.xlQualityStandard,false,missing); result = true; } catch { result = false; } finally { if (workBook != null) { workBook.Close(true,missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; } ///<summary> /// 把PowerPoint文件转换成PDF格件 ///</summary> ///<param name="sourcePath">源文件路径</param> ///<param name="targetPath">目炳路径</param> ///<returns>true=转换成功</returns> public 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; } }
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Diagnostics; using System.IO; /// <summary> /// Pdf2Swf 将pdf转化为swf /// </summary> public class Pdf2Swf { public Pdf2Swf() { // // TODO: 在此处添加构造函数逻辑 // } public void PDFConvertToSWF(string sourcePath,string targetPath) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe "; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); string cmd = "pdf2swf.exe" + " " + sourcePath + " -o " + targetPath; p.StandardInput.WriteLine(cmd); p.Close(); } }
然后只需要调用这两个类就可以实现将office转换为pdf再转换为flash的功能了 (3)最后就是实现flash的显示:这时我们还要借助工具来实现功能,那就是flexpaper,首先在网上下载所需要的flash文件、javascript文件,然后编写html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- saved from url=(0014)about:internet --> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>中勤文库-Flash版</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css" media="screen"> html,body { height:100%; } body { margin:0; padding:0; overflow:auto; } #flashContent { display:none; } </style> <script type="text/javascript" src="js/swfobject/swfobject.js"></script> <script type="text/javascript" src="js/flexpaper_flash_debug.js"></script> <script type="text/javascript" src="js/jquery.js"></script> <script> //获得参数的方法 var request = { QueryString : function(val) { var uri = window.location.search; var re = new RegExp("" +val+ "=([^&?]*)","ig"); return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null); } } </script> <script type="text/javascript"> <!-- For version detection,set to min. required Flash Player version,or 0 (or 0.0.0),for no version detection. --> var swfVersionStr = "10.0.0"; <!-- To use express install,set to playerProductInstall.swf,otherwise the empty string. --> var xiSwfUrlStr = "playerProductInstall.swf"; var swfFile = emotion;//这填写文档转换成的flash文件的路径 var flashvars = { SwfFile : escape(swfFile),Scale : 0.6,ZoomTransition : "eaSEOut",ZoomTime : 0.5,ZoomInterval : 0.2,FitPageOnLoad : true,FitWidthOnLoad : false,PrintEnabled : true,FullScreenAsMaxWindow : false,ProgressiveLoading : true,PrintToolsVisible : true,ViewModeToolsVisible : true,ZoomToolsVisible : true,FullScreenVisible : true,NavToolsVisible : true,CursorToolsVisible : true,SearchToolsVisible : true,localeChain: "en_US" }; var params = { } params.quality = "high"; params.bgcolor = "#ffffff"; params.allowscriptaccess = "sameDomain"; params.allowfullscreen = "true"; var attributes = {}; attributes.id = "FlexPaperViewer"; attributes.name = "FlexPaperViewer"; swfobject.embedSWF( "FlexPaperViewer.swf","flashContent","730","580",swfVersionStr,xiSwfUrlStr,flashvars,params,attributes); swfobject.createCSS("#flashContent","display:block;text-align:left;"); </script> </head> <body> <div align="center"> <div id="flashContent"> <p> To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed. </p> <script type="text/javascript"> var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>" ); </script> </div> </div> </body> </html>
还不错吧,上面的工具栏也可以实现各种功能 至此为止,所有工作全部结束,大家只需要将这些代码进行整合,配合数据库使用,编写管理后台,就可以实现百度文库的效果,甚至可以投入实际使用。 源码下载:http://files.cnblogs.com/expectszc/Web.zip (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |