程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#.NET實現Word或Excel文件轉為HTML文件,

C#.NET實現Word或Excel文件轉為HTML文件,

編輯:C#入門知識

C#.NET實現Word或Excel文件轉為HTML文件,


Word文件轉html,返回相對路徑

 1 private string GetPathByDocToHTML(string strFile)
 2     {
 3         if (string.IsNullOrEmpty(strFile))
 4         {
 5             return "0";//沒有文件
 6         }
 7 
 8         Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
 9         Type wordType = word.GetType();
10         Microsoft.Office.Interop.Word.Documents docs = word.Documents;
11 
12         // 打開文件  
13         Type docsType = docs.GetType();
14 
15         object fileName = strFile;
16 
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 });
19 
20         // 轉換格式,另存為html  
21         Type docType = doc.GetType();
22         //給文件重新起名
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();
25 
26         string strFileFolder = "../html/";
27         DateTime dt = DateTime.Now;
28         //以yyyymmdd形式生成子文件夾名
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 + "/";
33         // 判斷指定目錄下是否存在文件夾,如果不存在,則創建 
34         if (!Directory.Exists(Server.MapPath(strFilePath)))
35         {
36             // 創建up文件夾 
37             Directory.CreateDirectory(Server.MapPath(strFilePath));
38         }
39 
40         //被轉換的html文檔保存的位置 
41         // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")
42         string ConfigPath = Server.MapPath(strFilePath + filename + ".html");
43         object saveFileName = ConfigPath;
44 
45         /*下面是Microsoft Word 9 Object Library的寫法,如果是10,可能寫成: 
46           * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 
47           * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML}); 
48           * 其它格式: 
49           * wdFormatHTML 
50           * wdFormatDocument 
51           * wdFormatDOSText 
52           * wdFormatDOSTextLineBreaks 
53           * wdFormatEncodedText 
54           * wdFormatRTF 
55           * wdFormatTemplate 
56           * wdFormatText 
57           * wdFormatTextLineBreaks 
58           * wdFormatUnicodeText 
59         */
60         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
61         null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
62 
63         //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
64         //  null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); 
65 
66         //關閉文檔  
67         docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
68         null, doc, new object[] { null, null, null });
69 
70         // 退出 Word  
71         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
72         //轉到新生成的頁面  
73         //return ("/" + filename + ".html");
74 
75         //轉化HTML頁面統一編碼格式
76         TransHTMLEncoding(ConfigPath);
77 
78         return (strFilePath + filename + ".html");
79     }

Excel文件轉HTML,返回相對路徑

 1 private string GetPathByXlsToHTML(string strFile)
 2     {
 3         if (string.IsNullOrEmpty(strFile))
 4         {
 5             return "0";//沒有文件
 6         }
 7 
 8         //實例化Excel  
 9         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;
12 
13         //打開文件,n.FullPath是文件路徑  
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];
16 
17         //給文件重新起名
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();
20 
21         string strFileFolder = "../html/";
22         DateTime dt = DateTime.Now;
23         //以yyyymmdd形式生成子文件夾名
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 + "/";
28         // 判斷指定目錄下是否存在文件夾,如果不存在,則創建 
29         if (!Directory.Exists(Server.MapPath(strFilePath)))
30         {
31             // 創建up文件夾 
32             Directory.CreateDirectory(Server.MapPath(strFilePath));
33         }
34         string ConfigPath = Server.MapPath(strFilePath + filename + ".html");
35         object savefilename = (object)ConfigPath;
36 
37         object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
38         //進行另存為操作    
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);
40         object osave = false;
41         //逐步關閉所有使用的對象  
42         workbook.Close(osave, Type.Missing, Type.Missing);
43         repExcel.Quit();
44         System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
45         worksheet = null;
46         //垃圾回收  
47         GC.Collect();
48         System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
49         workbook = null;
50         GC.Collect();
51         System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
52         GC.Collect();
53         System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
54         repExcel = null;
55         GC.Collect();
56         //依據時間殺滅進程  
57         System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");
58         foreach (System.Diagnostics.Process p in process)
59         {
60             if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
61             {
62                 p.Kill();
63             }
64         }
65 
66         return (strFilePath + filename + ".html");
67     }

這裡可能會遇到一個問題,由於轉化為HTML文件的頁面編碼可能使得浏覽器無法正確解讀,所以需要轉碼,轉換代碼如下:

 1     private void TransHTMLEncoding(string strFilePath)
 2     {
 3         try
 4         {
 5             System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0));
 6             string html = sr.ReadToEnd();
 7             sr.Close();
 8             html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content='text/html; charset=gb2312'>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
 9             System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.Default);
10 
11             sw.Write(html);
12             sw.Close();
13         }
14         catch (Exception ex)
15         {
16             Page.RegisterStartupScript("alt", "<script>alert('" + ex.Message + "')</script>");
17         }
18     }

這樣就可以正常在頁面上正常顯示了

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved