#Excel,ppt,word를 html로 전환

Excel, ppt, word를 html로 바꾸어 페이지에 표시해야 할 때가 있다.나는 인터넷에서 몇 가지 코드를 찾아서 여기에 기록하여 필요한 친구들이 참고하도록 제공하였다

1. 워드를 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이 한 페이지로 바뀌려면 코드를 수정해야 한다
 

좋은 웹페이지 즐겨찾기