zoukankan      html  css  js  c++  java
  • 类似QQ邮箱中‘HTML方式查看’功能查看Office文件

    完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件

    完成类似QQ邮箱中‘HTML方式查看’功能查看Office文件。

      我们在做一些系统的 下载模块 或者 内部邮箱 的时候,有些使用系统的人可能并没有安装office,但是又急需查看附件的内容甚至图片等等,下载安装一个Office又不大现实(时间要求紧?在某个网吧?在朋友家里?),那么如何查看一些常用的附件呢?

      其实QQ邮箱有个功能很好,它可以以 HTML方式查看 Office文件,例如Word文档、Excel表格已经PowerPoint幻灯片等,这个功能有很多实现的办法,例如使用SPS就是办法之一,但是其实大部分时候,我们用不到SPS,或者由于正版软件太贵了,那如何使用ASP.NET 2.0(C#)来实现这个功能?

      注意:以下代码在vs2008,office2007,windowsxp下测试通过,项目需要添加Excel 12、Word12、PowerPoint12、Office12 四个com引用

            /// <summary>
            /// 将PPT文件转换成HTML格式
            /// </summary>
            /// <param >PPT文件路径</param>
            public static void PptToHtmlFile(string PptFilePath)
            {
                Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
                Microsoft.Office.Interop.PowerPoint.Presentation pptFile = null;
                try
                {
                    //获得html文件名
                    string htmlFileName = PptFilePath.Substring(0, PptFilePath.LastIndexOf(".")) + ".html";
                    //打开一个ppt文件
                    pptFile = ppt.Presentations.Open(PptFilePath, Microsoft.Office.Core.MsoTriState.msoTrue,
                        Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
                    //转换成html格式
                    pptFile.SaveAs(htmlFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML,
                        Microsoft.Office.Core.MsoTriState.msoCTrue);
                }
                finally
                {
                    if (pptFile != null)
                    {
                        pptFile.Close();
                    }
                    ppt.Quit();
                    GC.Collect();
                }
            }

            /// <summary>
            /// 将Excel文件转换成HTML格式
            /// </summary>
            /// <param >Excel文件路径</param>
            public static void ExcelToHtmlFile(string ExcelFilePath)
            {
                Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook oBook = null;
                // 缺省参数
                object Unknown = Type.Missing;
                try
                {
                    //目标html文件路径
                    object Target = ExcelFilePath.Substring(0, ExcelFilePath.LastIndexOf(".")) + ".html";
                    //为了保险,只读方式打开
                    object readOnly = true;
                    // 指定另存为格式(html)
                    object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

                    //打开Excel文件
                    oBook = excelApp.Workbooks.Open(ExcelFilePath, Unknown, readOnly,
                        Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,
                        Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);

                    // 转换格式
                    oBook.SaveAs(Target, format, Unknown, Unknown, Unknown, Unknown,
                         Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                         Unknown, Unknown, Unknown, Unknown, Unknown);
                }
                finally
                {
                    if (oBook != null)
                    {
                        oBook.Close(false, Unknown, Unknown);
                    }
                    excelApp.Quit();
                    GC.Collect();
                }
            }

            /// <summary>
            /// 将Word文档转换成HTML格式
            /// </summary>
            /// <param >Word文档格式</param>
            public static void WordToHtmlFile(string WordFilePath)
            {
                Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document doc = null;
                // 缺省参数
                object Unknown = Type.Missing;
                try
                {
                    // 指定原文件和目标文件
                    object Source = WordFilePath;
                    object Target = WordFilePath.Substring(0, WordFilePath.LastIndexOf(".")) + ".html";
                    //为了保险,只读方式打开
                    object readOnly = true;
                    // 指定另存为格式(html)
                    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;

                    // 打开doc文件
                    doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
                        ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

                    // 转换格式
                    doc.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                }
                finally
                {
                    if (doc != null)
                    {
                        // 关闭文档和Word程序
                        doc.Close(ref Unknown, ref Unknown, ref Unknown);
                    }
                    newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
                    GC.Collect();
                }
            }


  • 相关阅读:
    (转)【web前端培训之前后端的配合(中)】继续昨日的故事
    ural(Timus) 1136. Parliament
    scau Josephus Problem
    ACMICPC Live Archive 6204 Poker End Games
    uva 10391 Compound Words
    ACMICPC Live Archive 3222 Joke with Turtles
    uva 10132 File Fragmentation
    uva 270 Lining Up
    【转】各种字符串哈希函数比较
    uva 10905 Children's Game
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/2381188.html
Copyright © 2011-2022 走看看