c#把word⽂档转换为html页⾯
⾸先到⼀个引⽤,这个引⽤2015上的vs都有的,诺,就是这个!
using Microsoft.Office.Interop.Word;
⾸选准备好你的word这⾥做测试呢就在项⽬⾥⾯创建⼀个⽂件夹,给你要转换的word放到⾥⾯,
其次copy下⾯这段⽅法到你的项⽬⾥⾯
1private string GetPathByDocToHTML(string strFile)
2 {
3if (string.IsNullOrEmpty(strFile))
4 {
5return"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
15object 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//给⽂件重新起名
23string 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
26string strFileFolder = "/html/";
27 DateTime dt = DateTime.Now;
28//以yyyymmdd形式⽣成⼦⽂件夹名
29string 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();
32string strFilePath = strFileFolder + strFileSubFolder + "/";
33// 判断指定⽬录下是否存在⽂件夹,如果不存在,则创建
34if (!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")
42string ConfigPath = Server.MapPath(strFilePath + filename + ".html");
43object 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,
61null, 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//关闭⽂档
pdf转html67 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
68null, 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
78return (strFilePath + filename + ".html");
79 }
80private void TransHTMLEncoding(string strFilePath)
81 {
82try
83 {
84 System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0));
85string 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 }
93catch (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⾥⾯,测试⽤运⾏之后打开你的项⽬到你保存的位置就⾏了。
发布评论