C# 将Excel,ppt和word转化为html

   有些时候可能需要将Excel,ppt和word转化为html在页面上显示。我从网上查到一些代码,记录在这里以供需要的朋友参考

1.将word转化为html显示

//========================================================================
        //  函数名: WordToHtml
        /// <summary> 
        /// Word转成Html 
        /// </summary> 
        /// <param name="wordfilename">word文件名</param> 
        /*=======================================================================
         变更记录
         序号   更新日期  开发者   变更内容
         0001   2008/07/22  张          新建
         =======================================================================*/
        public static string WordToHtml(object wordfilename)
        {
            //在此处放置用户代码以初始化页面 
            word.Application word = new word.Application();
            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[] { wordfilename, true, true });
            //转换格式,另存为 
            Type doctype = doc.GetType();
            string wordsavefilename = wordfilename.ToString();
            string strsavefilename = wordsavefilename.Substring(0, wordsavefilename.Length - 3) + "html";
            object savefilename = (object)strsavefilename;
            doctype.InvokeMember("saveas", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            doctype.InvokeMember("close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            // 退出 word 
            wordtype.InvokeMember("quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return savefilename.ToString();
        }

 

2.将PPT转化为html显示

//========================================================================
        //  函数名: PPTToHtml
        /// <summary> 
        /// PPT转成Html 
        /// </summary> 
        /// <param name="pptFilename">PPT文件名</param> 
        /*=======================================================================
         变更记录
         序号   更新日期  开发者   变更内容
         0001   2008/07/22  张          新建
         =======================================================================*/
        public string PPTToHtml(string pptFilename)
        {
            //被转换的html文档保存的位置
            string saveFileName = pptFilename + ".html";
            Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
            Microsoft.Office.Core.MsoTriState m1 = new MsoTriState();
            Microsoft.Office.Core.MsoTriState m2 = new MsoTriState();
            Microsoft.Office.Core.MsoTriState m3 = new MsoTriState();
            Microsoft.Office.Interop.PowerPoint.Presentation pp = ppt.Presentations.Open(pptFilename, m1, m2, m3);
            pp.SaveAs(saveFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTriStateMixed);
            pp.Close();
            //返回文件名
            return saveFileName;
        }

 

3.将Excel转化为html显示

//========================================================================
        //  函数名: ExcelToHtml
        /// <summary> 
        /// Excel转成Html 
        /// </summary> 
        /// <param name="excelFileName">Excel文件名</param> 
        /*=======================================================================
         变更记录
         序号   更新日期  开发者   变更内容
         0001   2008/07/22  张          新建
         =======================================================================*/
        public string ExcelToHtml(string excelFileName)
         {
             //实例化Excel
             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;
             //打开文件,n.FullPath是文件路径
             workbook = repExcel.Application.Workbooks.Open(excelFileName, 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];
             string filesavefilename = excelFileName.ToString();
             string strsavefilename = filesavefilename.Substring(0, filesavefilename.Length - 3) + "html";
             object savefilename = (object)strsavefilename;
             object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
             //进行另存为操作  
             workbook.SaveAs(savefilename, 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();
             System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
             worksheet = null;
             //垃圾回收
             GC.Collect();
             System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
             workbook = null;
             GC.Collect();
             System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
             GC.Collect();
             System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
             repExcel = null;
             GC.Collect();
            //依据时间杀灭进程
             System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
             foreach (System.Diagnostics.Process p in process)
             {
                 if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
                 {
                     p.Kill();
                 }
             }
 
            return savefilename.ToString();
         }


 

以上是转换成为html文件的方法,转换成功后会在文件夹下会生对应的图片文件夹,和样式文件,和html文件。其中如果Excel中包含多个sheet时,转化后的html会包含多个tab,如果想每个sheet转化为一个页面,则需要修改代码

 

 

你可能感兴趣的:(html,String,object,Excel,null,文档)