首先找到一个引用,这个引用上的vs都有的,诺,就是这个!
using Microsoft.Office.Interop.Word;
首选准备好你的word这里做测试呢 就在项目里面创建一个文件夹,给你要转换的word放到里面,
其次copy下面这段方法到你的项目里面
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 }80 private void TransHTMLEncoding(string strFilePath)81 {82 try83 {84 System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0));85 string html = sr.ReadToEnd();86 sr.Close();87 html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content='text/html; charset=gb2312'>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);88 System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.Default);89 90 sw.Write(html);91 sw.Close();92 }93 catch (Exception ex)94 {95 Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "myscript", "<script>alert('" + ex.Message + "')</script>");96 }97 }
其实是两个方法。
为了测试呢 你需要随便弄个地方调用这个这个方法
string strWord = Server.MapPath("/wordpath/thisisword.doc");
GetPathByDocToHTML(strWord);
就是这个,需要把你的word传进去,当然你可以写在Page_Load里面,测试用运行之后打开你的项目找到你保存的位置就行了。