Recently I was working on a project that required an online preview of Office documents. Here is my method to share with you.

1. The first method (not recommended) I found a COM component on the Internet to convert Office documents, but this method must install Office software, and it is easy to conflict with WPS on the computer, and there are a series of version problems. I have Office2010 installed on my computer without WPS, so I can use it directly. Specific methods are as follows:

Microsoft Office 14.0 Object Library

Microsoft Word 14.0 Object Library

Microsoft Excel 14.0 Object Library

Microsoft PowerPoint 14.0 Object Library

Then add an Office2HtmlHelper class for converting file operations

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 to Html /// </summary> /// <param Name ="path"> the path of the document to be converted </param> // <param name="savePath"> </param> // <param </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 converted into Html < summary > / / / < param name = "path" > to convert the path of the document < param > / / / < param Name ="savePath"> savePath to HTML </param> // <param name="wordFileName"> name of the file to 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 to Html < summary > / / / < param name = "path" > to convert the path of the document < param > / / / < param Name ="savePath"> savePath to HTML </param> // <param name="wordFileName"> name of the file to 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(); }}}Copy the code

So when you upload a file, you can use the Office2HtmlHelper class to convert it.

Create the folder / / string rootFolder = System. The Configuration. The ConfigurationManager. The AppSettings (" UploadFileRootPath "); string fileType = file.FileName.Split('.').Last(); string folderPath = rootFolder + "\\\\" + UPLOADS + "\\\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\\\"; // File viewing directory 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; }Copy the code

But then you have to install Office software to call COM components. Therefore, in order to avoid such unfriendly things, we finally chose to use the Aspose dynamic link library

The method of use remains the same, but the conversion method is changed, which is easier than using COM components:

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 into Html < / summary > // <param name="path"> </param> // <param name="savePath"> </param> // <param name="savePath"> </param> // <param </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); WordFileName = wordFileName + ".html"; string savePathss = Path.Combine(savePath, wordFileName); / / merge the converted HTML file path doc. Save (savePathss, Aspose. Words. SaveFormat. HTML); // convert to HTML} /// <summary> // Excel converts to HTML /// </summary> // <param name="path"> the path of the document to be converted </param> // <param Name ="savePath"> savePath to HTML </param> // <param name="wordFileName"> name of the file to 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 to Html < summary > / / / < param name = "path" > to convert the path of the document < param > / / / < param Name ="savePath"> savePath to HTML </param> // <param name="wordFileName"> name of the file to 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); }}}Copy the code

In short, it is very simple to use. If you have the same problem, welcome to exchange.