Word文档转为Html文件

时间:2021-11-29
本文章向大家介绍Word文档转为Html文件,主要包括Word文档转为Html文件使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
咪乐|直播|app|下载苹果 美国股市已经持续上涨了近10年时间,这也让投资人对于股市未来的前景愈发坐立不安,他们担心任何风吹草动都可能让这场盛宴戛然而止。
 1        public static void Run()
 2         {
 3             DirectoryInfo dir = new DirectoryInfo("E:/20190917");
 4             FileInfo[] fileList = dir.GetFiles();//获取指定文件夹下所有的文件
 5             //遍历E:/20190917目录下所有的Word文档
 6             foreach (var item in fileList)
 7             {
 8                 string ext = Path.GetExtension("E:/20190917/"+item.ToString());//获取文件的后缀名
 9                 string inputName ="E:/20190917/" + item.ToString();//获取遍历文档的具体路劲
10                 string outputName = inputName.Replace(ext, ".html"); // 同路径保存Html文档
11                 if (File.Exists(inputName))
12                 {
13                     object oMissing = System.Reflection.Missing.Value;
14                     object oTrue = true;
15                     object oFalse = false;
16                     Word._Application oWord = new Word.Application();
17                     Word._Document oWordDoc = new Word.Document();
18                     oWord.Visible = false;
19                     object openFormat = Word.WdOpenFormat.wdOpenFormatAuto;
20                     object openName = inputName;
21                     try
22                     {
23                         oWordDoc = oWord.Documents.Open(ref openName, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref openFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
24                     }
25                     catch (Exception e)
26                     {
27                         Console.WriteLine(e.Message);
28                         Console.WriteLine("读取Word文档时发生异常");
29                         oWord.Quit(ref oTrue, ref oMissing, ref oMissing);
30                         return;
31                     }
32                     object saveFileName = outputName;
33                     object saveFormat = Word.WdSaveFormat.wdFormatFilteredHTML;
34                     oWordDoc.SaveAs(ref saveFileName, ref saveFormat, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
35                     oWordDoc.Close(ref oTrue, ref oMissing, ref oMissing);
36                     oWord.Quit(ref oTrue, ref oMissing, ref oMissing);
37                     Encoding enc = Encoding.GetEncoding("GB2312");
38                     string s = File.ReadAllText(outputName, enc);
39                     s = s.Replace("position:absolute;", "");
40                     File.WriteAllText(outputName, s, enc);
41                     Console.WriteLine("Word文档已转换为html格式");
42                 }
43             }
44         }

原文地址:https://www.cnblogs.com/suflowers1700218/p/11532182.html