类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)
转自http://scdzlee.blog.163.com/blog/static/2893916420124902816526/ 1.首先上网搜索一下有什么解决方案 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf (1) a.首先添加引用: using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; b.其次编写文档转换的方法: 1 /// <summary> 2 /// word转成html 3 </summary> 4 <param name="path">要转换的文档的路径</param> 5 <param name="savePath">转换成的html的保存路径 6 <param name="wordFileName">转换后html文件的名字</param> 7 private static void WordToHtml(string path,string savePath,255)">string wordFileName) 8 { 9 Word.ApplicationClass word = new Word.ApplicationClass(); 10 Type wordType = word.GetType(); 11 Word.Documents docs = word.Documents; 12 Type docsType = docs.GetType(); 13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,255)">null,docs,255)">new Object[] {(object)path,255)">true,255)">true }); 14 Type docType = doc.GetType(); 15 string strSaveFileName = savePath + wordFileName + .html"; 16 object saveFileName = (object)strSaveFileName; 17 docType.InvokeMember(SaveAsnew object[] { saveFileName,Word.WdSaveFormat.wdFormatFilteredHTML }); 18 docType.InvokeMember(Closenull); 19 wordType.InvokeMember(Quit20 } 21 22 excel 转换为html 23 24 25 26 27 public void ExcelToHtml(28 { 29 string str = string.Empty; 30 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application(); 31 Microsoft.Office.Interop.Excel.Workbook workbook = null; 32 Microsoft.Office.Interop.Excel.Worksheet worksheet = 33 workbook = repExcel.Application.Workbooks.Open(path,Type.Missing,Type.Missing); 34 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; 35 object htmlFile = savePath + wordFileName + 36 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; 37 workbook.SaveAs(htmlFile,ofmt,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,128)">38 object osave = false; 39 workbook.Close(osave,128)">40 repExcel.Quit(); 41 } 42 43 ppt转换为html 44 45 46 47 48 void PPTToHtml(49 { 50 Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application(); 51 string strSourceFile = path; 52 string strDestinationFile = savePath + wordFileName + 53 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); 54 prsPres.SaveAs(strDestinationFile,Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML,MsoTriState.msoTrue); 55 prsPres.Close(); 56 ppApp.Quit(); 57 } c.接下来就是调用方法:WordToHtml(string path,string savePath,string wordFileName);ExcelToHtml(string path,string wordFileName);PPTToHtml(string path,string wordFileName)实现相应文档的转换 然后就可以看见效果了 WORD PPT ? 大家看一下这样的效果确实感觉挺丑的,特别是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类 1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 using Word = Microsoft.Office.Interop.Word; 11 using Excel = Microsoft.Office.Interop.Excel; 12 using PowerPoint = Microsoft.Office.Interop.PowerPoint; 13 14 15 16 Office2Pdf 将Office文档转化为pdf 17 </summary> 18 class Office2Pdf 19 { 20 public Office2Pdf() 21 { 22 // 23 TODO: 在此处添加构造函数逻辑 24 // 25 } 26 27 Word转换成pdf 28 29 <param name="sourcePath">源文件路径 30 <param name="targetPath">目标文件路径 31 <returns>true=转换成功</returns> 32 bool DOCConvertToPDF(string sourcePath,255)">string targetPath) 33 { 34 bool result = 35 Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF; 36 object paramMissing = Type.Missing; 37 Word.ApplicationClass wordApplication = 38 Word.Document wordDocument = 39 try 40 { 41 object paramSourceDocPath = sourcePath; 42 string paramExportFilePath = targetPath; 43 Word.WdExportFormat paramExportFormat = exportFormat; 44 bool paramOpenAfterExport = 45 Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint; 46 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument; 47 int paramStartPage = 0; 48 int paramEndPage = 49 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent; 50 bool paramIncludeDocProps = true; 51 bool paramKeepIRM = 52 Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; 53 bool paramDocStructureTags = 54 bool paramBitmapMissingFonts = 55 bool paramUseISO19005_1 = 56 wordDocument = wordApplication.Documents.Open( 57 ref paramSourceDocPath,255)">ref paramMissing,128)"> 58 59 60 61 62 ref paramMissing); 63 if (wordDocument != null) 64 wordDocument.ExportAsFixedFormat(paramExportFilePath,128)"> 65 paramExportFormat,paramOpenAfterExport,128)"> 66 paramExportOptimizeFor,paramExportRange,paramStartPage,128)"> 67 paramEndPage,paramExportItem,paramIncludeDocProps,128)"> 68 paramKeepIRM,paramCreateBookmarks,paramDocStructureTags,128)"> 69 paramBitmapMissingFonts,paramUseISO19005_1,128)"> 70 71 result = 72 } 73 catch 74 { 75 result = 76 } 77 finally 78 { 79 80 { 81 wordDocument.Close( 82 wordDocument = 83 } 84 if (wordApplication != 85 { 86 wordApplication.Quit( 87 wordApplication = 88 } 89 GC.Collect(); 90 GC.WaitForPendingFinalizers(); 91 GC.Collect(); 92 GC.WaitForPendingFinalizers(); 93 } 94 return result; 95 } 96 97 98 把Excel文件转换成PDF格式文件 99 100 101 102 103 bool XLSConvertToPDF(104 { 105 106 Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF; 107 object missing = Type.Missing; 108 Excel.ApplicationClass application = 109 Excel.Workbook workBook = 110 111 { 112 application = new Excel.ApplicationClass(); 113 object target = targetPath; 114 object type = targetType; 115 workBook = application.Workbooks.Open(sourcePath,missing,128)">116 missing,missing); 117 workBook.ExportAsFixedFormat(targetType,target,Excel.XlFixedFormatQuality.xlQualityStandard,255)">false,128)">118 result = 119 } 120 121 { 122 result = 123 } 124 125 { 126 if (workBook != 127 { 128 workBook.Close(129 workBook = 130 } 131 if (application != 132 { 133 application.Quit(); 134 application = 135 } 136 GC.Collect(); 137 GC.WaitForPendingFinalizers(); 138 GC.Collect(); 139 GC.WaitForPendingFinalizers(); 140 } 141 142 } 143 /// 144 把PowerPoint文件转换成PDF格式文件 145 146 147 148 </returns> 149 bool PPTConvertToPDF(150 { 151 bool result; 152 PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF; 153 154 PowerPoint.ApplicationClass application = 155 PowerPoint.Presentation persentation = 156 157 { 158 application = new PowerPoint.ApplicationClass(); 159 persentation = application.Presentations.Open(sourcePath,MsoTriState.msoTrue,MsoTriState.msoFalse,MsoTriState.msoFalse); persentation.SaveAs(targetPath,targetFileType,Microsoft.Office.Core.MsoTriState.msoTrue); 160 result = 161 } 162 163 { 164 result = 165 } 166 167 { 168 if (persentation != 169 { 170 persentation.Close(); 171 persentation = 172 } 173 174 { 175 application.Quit(); 176 application = 177 } 178 GC.Collect(); 179 GC.WaitForPendingFinalizers(); 180 GC.Collect(); 181 GC.WaitForPendingFinalizers(); 182 } 183 184 } 185 } ? 然后编写Pdf2Swf.cs类调用命令行将pdf转换为swf格式的flash 1 2 3 4 5 6 7 8 9 10 using System.Diagnostics; 11 using System.IO; 12 13 14 Pdf2Swf 将pdf转化为swf 15 16 class Pdf2Swf 17 { 18 public Pdf2Swf() 19 { 20 21 22 23 } 24 void PDFConvertToSWF(25 { 26 Process p = new Process(); 27 p.StartInfo.FileName = cmd.exe 28 p.StartInfo.UseShellExecute = 29 p.StartInfo.RedirectStandardInput = 30 p.StartInfo.RedirectStandardOutput = 31 p.StartInfo.RedirectStandardError = 32 p.StartInfo.CreateNoWindow = 33 p.Start(); 34 string cmd = pdf2swf.exe" + " " + sourcePath + -o " + targetPath; 35 p.StandardInput.WriteLine(cmd); 36 p.Close(); 37 } 38 } ? 然后只需要调用这两个类就可以实现将office转换为pdf再转换为flash的功能了 (3)最后就是实现flash的显示:这时我们还要借助工具来实现功能,那就是flexpaper,首先在网上下载所需要的flash文件、javascript文件,然后编写html代码: 效果就这样: 至此为止,所有工作全部结束,大家只需要将这些代码进行整合,配合数据库使用,编写管理后台,就可以实现百度文库的效果,甚至可以投入实际使用。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |