C#Word檔案轉換為html

來源:互聯網
上載者:User

標籤:class   lin   encode   轉換   math   c#   space   lib   microsoft   

  1 using Microsoft.Office.Interop.Word;  2 using System;  3 using System.Collections.Generic;  4 using System.IO;  5 using System.Linq;  6 using System.Text;  7 using System.Web;  8 using System.Web.UI;  9 using System.Web.UI.WebControls; 10  11 namespace Admin 12 { 13     public partial class TestDemo : System.Web.UI.Page 14     { 15         protected void Page_Load(object sender, EventArgs e) 16         { 17             string wordFileName = "~/uploads/TutorCV/111.docx"; 18             string htmlFileName = "~/uploads/TutorCV/"; 19             //GetPathByDocToHTML(Server.MapPath(wordFileName)); 20         } 21         private string GetPathByDocToHTML(string strFile) 22         { 23             if (string.IsNullOrEmpty(strFile)) 24             { 25                 return "0";//沒有檔案 26             } 27  28             //Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); 29             Microsoft.Office.Interop.Word._Application word = new Application(); 30             Type wordType = word.GetType(); 31             Microsoft.Office.Interop.Word.Documents docs = word.Documents; 32  33             // 開啟檔案   34             Type docsType = docs.GetType(); 35  36             object fileName = strFile; 37  38             Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", 39             System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true }); 40  41             // 轉換格式,另存新檔html   42             Type docType = doc.GetType(); 43             //給檔案重新起名 44             string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + 45             System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString(); 46  47             string strFileFolder = "~/uploads/TutorCV/"; 48             DateTime dt = DateTime.Now; 49             //以yyyymmdd形式產生子檔案夾名 50             string strFileSubFolder = dt.Year.ToString(); 51             strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString(); 52             strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString(); 53             string strFilePath = strFileFolder + strFileSubFolder + "/"; 54             // 判斷指定目錄下是否存在檔案夾,如果不存在,則建立  55             if (!Directory.Exists(Server.MapPath(strFilePath))) 56             { 57                 // 建立up檔案夾  58                 Directory.CreateDirectory(Server.MapPath(strFilePath)); 59             } 60  61             //被轉換的html文檔儲存的位置  62             // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html") 63             string ConfigPath = Server.MapPath(strFilePath + filename + ".html"); 64             object saveFileName = ConfigPath; 65  66             /*下面是Microsoft Word 9 Object Library的寫法,如果是10,可能寫成:  67               * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,  68               * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});  69               * 其它格式:  70               * wdFormatHTML  71               * wdFormatDocument  72               * wdFormatDOSText  73               * wdFormatDOSTextLineBreaks  74               * wdFormatEncodedText  75               * wdFormatRTF  76               * wdFormatTemplate  77               * wdFormatText  78               * wdFormatTextLineBreaks  79               * wdFormatUnicodeText  80             */ 81             docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 82             null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); 83  84             //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 85             //  null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });  86  87             //關閉文檔   88             docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, 89             null, doc, new object[] { null, null, null }); 90  91             // 退出 Word   92             wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); 93             //轉到新產生的頁面   94             //return ("/" + filename + ".html"); 95  96             //轉化HTML頁面統一編碼格式 97             TransHTMLEncoding(ConfigPath); 98  99             return (strFilePath + filename + ".html");100         }101         private void TransHTMLEncoding(string strFilePath)102         {103             try104             {105                 System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0));106                 string html = sr.ReadToEnd();107                 sr.Close();108                 html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content=‘text/html; charset=gb2312‘>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);109                 System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.Default);110 111                 sw.Write(html);112                 sw.Close();113             }114             catch (Exception ex)115             {116                 Page.RegisterStartupScript("alt", "<script>alert(‘" + ex.Message + "‘)</script>");117             }118         }119 120     }121 }

 

C#Word檔案轉換為html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.