在线预览⽂档(⽀持word、excel、ppt、pdf)+在线预览⽂档
html版(转)
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);
java数组初始化格式
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>
sidebar是什么程序
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,实现在线预览,你会发现有好⼏种解决⽅案
    放弃此种⽅案。
    细想⼀下如果我们能将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;文件格式转换app
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,
exercise翻译
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();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。