最近在做了一个项目,要求是对Office文档在线预览。下面给大家分享一下我的方法。

1.第一种方法(不建议使用)
我是在网上搜了一个利用COM组件对office文档进行转换,但是此方法必须要装Office办公软件,而且容易与电脑上的WPS冲突,还有一系列的版本问题。我电脑上装的是Office2010,没有装WPS,所以直接可以使用。具体方法如下:

Microsoft Office 14.0 Object Library
Microsoft Word 14.0 Object Library
Microsoft Excel 14.0 Object Library
Microsoft PowerPoint 14.0 Object Library

然后添加一个Office2HtmlHelper类,用于转换文件操作

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;

namespace OfficeToHtml.Utils
{
    public class Office2HtmlHelper
    {
        /// <summary>
        /// Word转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Word2Html(string path, string savePath, string wordFileName)
        {

            Word.ApplicationClass word = new Word.ApplicationClass();
            Type wordType = word.GetType();
            Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
            Type docType = doc.GetType();
            string strSaveFileName = savePath + wordFileName + ".html";
            object saveFileName = (object)strSaveFileName;
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
        /// <summary>
        /// Excel转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Excel2Html(string path, string savePath, 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, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
            object htmlFile = savePath + wordFileName + ".html";
            object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
            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);
            object osave = false;
            workbook.Close(osave, Type.Missing, Type.Missing);
            repExcel.Quit();
        }
        /// <summary>
        /// ppt转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void PPT2Html(string path, string savePath, 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();
        }
    }
}

这样在上传文件的时候就可以使用Office2HtmlHelper类进行转换操作了,(控制器代码)

//创建文件夹
string rootFolder = System.Configuration.ConfigurationManager.AppSettings["UploadFileRootPath"];
string fileType = file.FileName.Split('.').Last();
string folderPath = rootFolder + "\\\\" + UPLOADS + "\\\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\\\";
//文件查看目录
string viewFolderPath = rootFolder + "\\\\" + HTMLUPLOADS + "\\\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\\\";
      if (!Directory.Exists(folderPath))
      {
           Directory.CreateDirectory(folderPath);
      }
      if (!Directory.Exists(viewFolderPath))
       {
           Directory.CreateDirectory(viewFolderPath);
       }
       string fileName = Guid.NewGuid().ToString();
       filePath = folderPath + fileName + "." + fileType;
       file.SaveAs(filePath);
       switch (fileType)
       {
            case "docx":
            case "doc":
                  Office2HtmlHelper.Word2Html(filePath, viewFolderPath, fileName);
                  viewPath = viewFolderPath + fileName + "." + "html";
                  break;
             case "xlsx":
             case "xls":
                   Office2HtmlHelper.Excel2Html(filePath, viewFolderPath, fileName);
                   viewPath = viewFolderPath + fileName + "." + "html";
                   break;
             case "ppt":
             case "pptx":
                   Office2HtmlHelper.PPT2Html(filePath, viewFolderPath, fileName);
                   viewPath = viewFolderPath + fileName + "." + "html";
                   break;
             default:
                   break;
         }   

但是这样的话就必须要装Office软件才可以调用COM组件了。所以为了避免这种不友好的事情发生,最终选择了使用Aspose动态链接库

使用方法不变,只不过是转换方式变了,相比使用COM组件来说更加简便了一些:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
//using Microsoft.Office.Core;
//using Word = Microsoft.Office.Interop.Word;
using Aspose.Cells;
using Aspose.Slides.Pptx;

namespace CqscSecurityApplication.Utils
{
    public class Office2HtmlHelper
    {
        /// <summary>
        /// Word转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Word2Html(string path, string savePath, string wordFileName)
        {

            //Word.ApplicationClass word = new Word.ApplicationClass();
            //Type wordType = word.GetType();
            //Word.Documents docs = word.Documents;
            //Type docsType = docs.GetType();
            //Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
            //Type docType = doc.GetType();
            //string strSaveFileName = savePath + wordFileName + ".html";
            //object saveFileName = (object)strSaveFileName;
            //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            //docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            //wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            Aspose.Words.Document doc = new Aspose.Words.Document(path);//通过path(文件原始源路径)获取文档内容
            wordFileName = wordFileName + ".html";
            string savePathss = Path.Combine(savePath, wordFileName);//合并转换后html文件路径
            doc.Save(savePathss,Aspose.Words.SaveFormat.Html);//转换为html格式
        }
        /// <summary>
        /// Excel转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Excel2Html(string path, string savePath, 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, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
            //object htmlFile = savePath + wordFileName + ".html";
            //object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
            //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);
            //object osave = false;
            //workbook.Close(osave, Type.Missing, Type.Missing);
            //repExcel.Quit();
            Workbook workbook=new Workbook(path);
            wordFileName = wordFileName + ".html";
            string savePathss = Path.Combine(savePath, wordFileName);
            workbook.Save(savePathss, SaveFormat.Html);
        }
        /// <summary>
        /// ppt转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void PPT2Html(string path, string savePath, 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();
            PresentationEx pres=new PresentationEx(path);
            wordFileName = wordFileName + ".html";
            string savePathss = Path.Combine(savePath, wordFileName);
            pres.Save(savePathss, Aspose.Slides.Export.SaveFormat.Html);
        }
    }
}

总之使用很简单,有遇到同样问题的同学,欢迎交流;

版权声明:本文为yumaster原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yumaster/p/8250143.html