.NET实现Word或Excel文件转为HTML文件
Word文件转html,返回相对路径
01 | private string GetPathByDocToHTML( string strFile) |
03 | if ( string .IsNullOrEmpty(strFile)) |
08 | Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); |
09 | Type wordType = word.GetType(); |
10 | Microsoft.Office.Interop.Word.Documents docs = word.Documents; |
13 | Type docsType = docs.GetType(); |
15 | object fileName = strFile; |
17 | Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember( "Open" , |
18 | System.Reflection.BindingFlags.InvokeMethod, null , docs, new Object[] { fileName, true , true }); |
21 | Type docType = doc.GetType(); |
23 | string filename = System.DateTime.Now.Year.ToString() System.DateTime.Now.Month.ToString() System.DateTime.Now.Day.ToString() |
24 | System.DateTime.Now.Hour.ToString() System.DateTime.Now.Minute.ToString() System.DateTime.Now.Second.ToString(); |
26 | string strFileFolder = "../html/" ; |
27 | DateTime dt = DateTime.Now; |
29 | string strFileSubFolder = dt.Year.ToString(); |
30 | strFileSubFolder = (dt.Month < 10) ? ( "0" dt.Month.ToString()) : dt.Month.ToString(); |
31 | strFileSubFolder = (dt.Day < 10) ? ( "0" dt.Day.ToString()) : dt.Day.ToString(); |
32 | string strFilePath = strFileFolder strFileSubFolder "/" ; |
34 | if (!Directory.Exists(Server.MapPath(strFilePath))) |
37 | Directory.CreateDirectory(Server.MapPath(strFilePath)); |
42 | string ConfigPath = Server.MapPath(strFilePath filename ".html" ); |
43 | object saveFileName = ConfigPath; |
60 | docType.InvokeMember( "SaveAs" , System.Reflection.BindingFlags.InvokeMethod, |
61 | null , doc, new object [] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); |
67 | docType.InvokeMember( "Close" , System.Reflection.BindingFlags.InvokeMethod, |
68 | null , doc, new object [] { null , null , null }); |
71 | wordType.InvokeMember( "Quit" , System.Reflection.BindingFlags.InvokeMethod, null , word, null ); |
76 | TransHTMLEncoding(ConfigPath); |
78 | return (strFilePath filename ".html" ); |
Excel文件转HTML,返回相对路径
01 | private string GetPathByXlsToHTML( string strFile) |
03 | if ( string .IsNullOrEmpty(strFile)) |
09 | Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application(); |
10 | Microsoft.Office.Interop.Excel.Workbook workbook = null ; |
11 | Microsoft.Office.Interop.Excel.Worksheet worksheet = null ; |
14 | workbook = repExcel.Application.Workbooks.Open(strFile, 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); |
15 | worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; |
18 | string filename = System.DateTime.Now.Year.ToString() System.DateTime.Now.Month.ToString() System.DateTime.Now.Day.ToString() |
19 | System.DateTime.Now.Hour.ToString() System.DateTime.Now.Minute.ToString() System.DateTime.Now.Second.ToString(); |
21 | string strFileFolder = "../html/" ; |
22 | DateTime dt = DateTime.Now; |
24 | string strFileSubFolder = dt.Year.ToString(); |
25 | strFileSubFolder = (dt.Month < 10) ? ( "0" dt.Month.ToString()) : dt.Month.ToString(); |
26 | strFileSubFolder = (dt.Day < 10) ? ( "0" dt.Day.ToString()) : dt.Day.ToString(); |
27 | string strFilePath = strFileFolder strFileSubFolder "/" ; |
29 | if (!Directory.Exists(Server.MapPath(strFilePath))) |
32 | Directory.CreateDirectory(Server.MapPath(strFilePath)); |
34 | string ConfigPath = Server.MapPath(strFilePath filename ".html" ); |
35 | object savefilename = ( object )ConfigPath; |
37 | object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; |
39 | 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); |
42 | workbook.Close(osave, Type.Missing, Type.Missing); |
44 | System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); |
48 | System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); |
51 | System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks); |
53 | System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel); |
57 | System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName( "EXCEL" ); |
58 | foreach (System.Diagnostics.Process p in process) |
60 | if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5) |
66 | return (strFilePath filename ".html" ); |
这里可能会遇到一个问题,由于转化为HTML文件的页面编码可能使得浏览器无法正确解读,
所以需要转码,转换代码如下:
01 | private void TransHTMLEncoding( string strFilePath) |
05 | System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0)); |
06 | string html = sr.ReadToEnd(); |
08 | html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>" , "<meta http-equiv=Content-Type content='text/html; charset=gb2312'>" , System.Text.RegularExpressions.RegexOptions.IgnoreCase); |
09 | System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false , Encoding.Default); |
16 | Page.RegisterStartupScript( "alt" , "<script>alert('" ex.Message "')</script>" ); |
原文链接:.NET实现Word或Excel文件转为HTML文件