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的保存路径</param>
6 /// <param name="wordFileName">转换后html文件的名字</param>
7 private static void WordToHtml(string path, string savePath, 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, null, docs, new Object[] {(object)path, true, true });
14 Type docType = doc.GetType();
15 string strSaveFileName = savePath + wordFileName + ".html";
16 object saveFileName = (object)strSaveFileName;
17 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
18 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
19 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
20 }
21 /// <summary>
22 /// excel 转换为html
23   /// </summary>
24   /// <param name="path">要转换的文档的路径</param>
25   /// <param name="savePath">转换成的html的保存路径</param>
26   /// <param name="wordFileName">转换后html文件的名字</param>
27 public static void ExcelToHtml(string path, string savePath, string wordFileName)
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 = null;
33 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);
34 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
35 object htmlFile = savePath + wordFileName + ".html";
36 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
37 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);
38 object osave = false;
39 workbook.Close(osave, Type.Missing, Type.Missing);
40 repExcel.Quit();
41 }
42 /// <summary>
43   /// ppt转换为html
44   /// </summary>
45   /// <param name="path">要转换的文档的路径</param>
46   /// <param name="savePath">转换成的html的保存路径</param>
47   /// <param name="wordFileName">转换后html文件的名字</param>
48 public static void PPTToHtml(string path, string savePath, string wordFileName)
49 {
50 Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
51 string strSourceFile = path;
52 string strDestinationFile = savePath + wordFileName + ".html";
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 savePath, string wordFileName);PPTToHtml(string path, string savePath, 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 using Microsoft.Office.Core;
14
15 /// <summary>
16 /// Office2Pdf 将Office文档转化为pdf
17 /// </summary>
18 public class Office2Pdf
19 {
20 public Office2Pdf()
21 {
22 //
23 // TODO: 在此处添加构造函数逻辑
24 //
25 }
26 /// <summary>
27 /// Word转换成pdf
28 /// </summary>
29 /// <param name="sourcePath">源文件路径</param>
30 /// <param name="targetPath">目标文件路径</param>
31 /// <returns>true=转换成功</returns>
32 public bool DOCConvertToPDF(string sourcePath, string targetPath)
33 {
34 bool result = false;
35 Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
36 object paramMissing = Type.Missing;
37 Word.ApplicationClass wordApplication = new Word.ApplicationClass();
38 Word.Document wordDocument = null;
39 try
40 {
41 object paramSourceDocPath = sourcePath;
42 string paramExportFilePath = targetPath;
43 Word.WdExportFormat paramExportFormat = exportFormat;
44 bool paramOpenAfterExport = false;
45 Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
46 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
47 int paramStartPage = 0;
48 int paramEndPage = 0;
49 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
50 bool paramIncludeDocProps = true;
51 bool paramKeepIRM = true;
52 Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
53 bool paramDocStructureTags = true;
54 bool paramBitmapMissingFonts = true;
55 bool paramUseISO19005_1 = false;
56 wordDocument = wordApplication.Documents.Open(
57 ref paramSourceDocPath, ref paramMissing, ref paramMissing,
58 ref paramMissing, ref paramMissing, ref paramMissing,
59 ref paramMissing, ref paramMissing, ref paramMissing,
60 ref paramMissing, ref paramMissing, ref paramMissing,
61 ref paramMissing, ref paramMissing, ref paramMissing,
62 ref paramMissing);
63 if (wordDocument != null)
64 wordDocument.ExportAsFixedFormat(paramExportFilePath,
65 paramExportFormat, paramOpenAfterExport,
66 paramExportOptimizeFor, paramExportRange, paramStartPage,
67 paramEndPage, paramExportItem, paramIncludeDocProps,
68 paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
69 paramBitmapMissingFonts, paramUseISO19005_1,
70 ref paramMissing);
71 result = true;
72 }
73 catch
74 {
75 result = false;
76 }
77 finally
78 {
79 if (wordDocument != null)
80 {
81 wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
82 wordDocument = null;
83 }
84 if (wordApplication != null)
85 {
86 wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
87 wordApplication = null;
88 }
89 GC.Collect();
90 GC.WaitForPendingFinalizers();
91 GC.Collect();
92 GC.WaitForPendingFinalizers();
93 }
94 return result;
95 }
96
97 /// <summary>
98 /// 把Excel文件转换成PDF格式文件
99 /// </summary>
100 /// <param name="sourcePath">源文件路径</param>
101 /// <param name="targetPath">目标文件路径</param>
102 /// <returns>true=转换成功</returns>
103 public bool XLSConvertToPDF(string sourcePath, string targetPath)
104 {
105 bool result = false;
106 Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
107 object missing = Type.Missing;
108 Excel.ApplicationClass application = null;
109 Excel.Workbook workBook = null;
110 try
111 {
112 application = new Excel.ApplicationClass();
113 object target = targetPath;
114 object type = targetType;
115 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
116 missing, missing, missing, missing, missing, missing, missing, missing, missing);
117 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
118 result = true;
119 }
120 catch
121 {
122 result = false;
123 }
124 finally
125 {
126 if (workBook != null)
127 {
128 workBook.Close(true, missing, missing);
129 workBook = null;
130 }
131 if (application != null)
132 {
133 application.Quit();
134 application = null;
135 }
136 GC.Collect();
137 GC.WaitForPendingFinalizers();
138 GC.Collect();
139 GC.WaitForPendingFinalizers();
140 }
141 return result;
142 }
143 ///<summary>
144 /// 把PowerPoint文件转换成PDF格式文件
145 ///</summary>
146 ///<param name="sourcePath">源文件路径</param>
147 ///<param name="targetPath">目标文件路径</param>
148 ///<returns>true=转换成功</returns>
149 public bool PPTConvertToPDF(string sourcePath, string targetPath)
150 {
151 bool result;
152 PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
153 object missing = Type.Missing;
154 PowerPoint.ApplicationClass application = null;
155 PowerPoint.Presentation persentation = null;
156 try
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 = true;
161 }
162 catch
163 {
164 result = false;
165 }
166 finally
167 {
168 if (persentation != null)
169 {
170 persentation.Close();
171 persentation = null;
172 }
173 if (application != null)
174 {
175 application.Quit();
176 application = null;
177 }
178 GC.Collect();
179 GC.WaitForPendingFinalizers();
180 GC.Collect();
181 GC.WaitForPendingFinalizers();
182 }
183 return result;
184 }
185 }

    然后编写Pdf2Swf.cs类调用命令行将pdf转换为swf格式的flash

 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 System.Diagnostics;
11 using System.IO;
12
13 /// <summary>
14 /// Pdf2Swf 将pdf转化为swf
15 /// </summary>
16 public class Pdf2Swf
17 {
18 public Pdf2Swf()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24 public void PDFConvertToSWF(string sourcePath, string targetPath)
25 {
26 Process p = new Process();
27 p.StartInfo.FileName = "cmd.exe ";
28 p.StartInfo.UseShellExecute = false;
29 p.StartInfo.RedirectStandardInput = true;
30 p.StartInfo.RedirectStandardOutput = true;
31 p.StartInfo.RedirectStandardError = true;
32 p.StartInfo.CreateNoWindow = true;
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代码:

效果就这样:

还不错吧,上面的工具栏也可以实现各种功能

至此为止,所有工作全部结束,大家只需要将这些代码进行整合,配合数据库使用,编写管理后台,就可以实现百度文库的效果,甚至可以投入实际使用。

在线预览文档(支持word、excel、ppt、pdf)+在线预览文档html版(转)的更多相关文章

  1. ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)

    Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...

  2. [转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)

    转载自:https://www.cnblogs.com/Andre/p/9549874.html Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架 ...

  3. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  4. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  5. 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)

    一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...

  6. 微信小程序云开发-云存储-上传文件(word/excel/ppt/pdf)到云存储

    说明 word/excel/ppt/pdf是从客户端会话选择文件.使用chooseMessageFile中选择文件. 一.wxml文件 上传按钮,绑定chooseFile <!--上传文件(wo ...

  7. uploadify 下载组件使用技巧和在线预览 word,excel,ppt,pdf的方案

    http://www.cnblogs.com/wolf-sun/p/3565184.html uploadify 上传工具的使用技巧 http://www.cnblogs.com/wolf-sun/p ...

  8. lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

    在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...

  9. 关于在线预览word,excel,ppt,pdf的需求处理方法。

    参考文档:http://www.cnblogs.com/wolf-sun/p/3574278.html 我选用的方案:先用office com组件生成pdf,然后使用pdf.js在线预览pdf文档.在 ...

  10. 微信小程序云开发-云存储-上传、下载、打开文件文件(word/excel/ppt/pdf)一步到位

    一.wxml文件 <!-- 上传.下载.打开文件一步执行 --> <view class="handle"> <button bindtap=&quo ...

随机推荐

  1. VMWare 回收磁盘空间

    两部分内容: 1) 实际操作体验下在vmware player里回收guest vm的磁盘空间,还给host: 2)顺便把之前的笔记翻出来关于vmware unmap/reclaim, 对照总结. 1 ...

  2. 关于Cocos2d-x中让主角运动的方法

    比如要让角色跳起来 1.如果是用到物理引擎,那么在物理世界中,可以用 hero->getPhysicsBody()->setVelocity(Vec2(0, 400));  //给主角一个 ...

  3. matplotlib中的legend()——用于显示图例

    legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import matplotlib.pyplot as plt import numpy ...

  4. com.sun.awt.AWTUtilities.setWindowOpacity相关说明

    在eclipse中(jdk1.6.*)版本中出现编译不通过而报错,报错是因为这个包里面的方法不属于jdk正式版本,也就是不能保证下个版本还存在,所以编译器会拒绝,你可以在eclipse中如下设置: 选 ...

  5. org.xml.sax.SAXParseException: prolog 中不允许有内容

    org.xml.sax.SAXParseException: prolog 中不允许有内容 digester.fatalError 不下心踢了电源导致的错误应该是解析xml出问题,找了半天不知道哪个x ...

  6. LLE局部线性嵌入算法

    非线性降维 流形学习 算法思想有些类似于NLM,但是是进行的降维操作. [转载自] 局部线性嵌入(LLE)原理总结 - yukgwy60648的博客 - CSDN博客 https://blog.csd ...

  7. VC++ 实现窗口抖动

    RECT rect; int x, y, nWidth, nHeight; GetWindowRect(&rect); x = rect.left; y = rect.top; nWidth ...

  8. C#获取CPU处理器核心数量的方法_C#教程

    https://yq.aliyun.com/ziliao/89096 摘要: 本文讲的是C#获取CPU处理器核心数量的方法_C#教程, 有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核 ...

  9. VS------csc.exe已停止工作解决方法

    转载: http://blog.csdn.net/verylost/article/details/53667769 方法: 解决方法是把进程中所有的VBCSCompiler.exe进程结束掉,然后清 ...

  10. Java集合----List集合

    List List 代表一个元素有序.且可重复的集合,集合中的每个元素都有其对应的顺序索引List 允许使用重复元素,可以通过索引来访问指定位置的集合元素.List 默认按元素的添加顺序设置元素的索引 ...