EXCEL轉html

        /*
fullname: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(fullname, 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]; if (!Directory.Exists(System.IO.Path.Combine(System.IO.Directory.GetParent(fullname).FullName,"html"))) { // 創建up文件夾 Directory.CreateDirectory(System.IO.Path.Combine(System.IO.Directory.GetParent(fullname).FullName,"html")); } string ConfigPath = System.IO.Path.Combine(System.IO.Directory.GetParent(fullname).FullName, "html","a.html"); object savefilename = (object)ConfigPath; 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(); } }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章