最近在作了一個項目,要求是對Office文檔在線預覽。下面給你們分享一下個人方法。html
1.第一種方法(不建議使用)
我是在網上搜了一個利用COM組件對office文檔進行轉換,可是此方法必需要裝Office辦公軟件,並且容易與電腦上的WPS衝突,還有一系列的版本問題。我電腦上裝的是Office2010,沒有裝WPS,因此直接可使用。具體方法以下:ide
Microsoft Office 14.0 Object Library
Microsoft Word 14.0 Object Library
Microsoft Excel 14.0 Object Library
Microsoft PowerPoint 14.0 Object Libraryui
而後添加一個Office2HtmlHelper類,用於轉換文件操做spa
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類進行轉換操做了,(控制器代碼)code
//建立文件夾 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動態連接庫orm
使用方法不變,只不過是轉換方式變了,相比使用COM組件來講更加簡便了一些:htm
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); } } }
總之使用很簡單,有遇到一樣問題的同窗,歡迎交流;blog