• Word文档转为Html文件


    需要添加引用:

    using System.IO;
    using Word = Microsoft.Office.Interop.Word;(添加程序集引用)


    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 }
  • 相关阅读:
    COGS2355 【HZOI2015】 有标号的DAG计数 II
    COGS2353 【HZOI2015】有标号的DAG计数 I
    COGS2259 异化多肽
    二项式定理
    Codeforces 528D Fuzzy Search
    技术文章阅读-华为WS331a产品管理页面存在CSRF漏洞
    技术文章阅读-天翼创维awifi路由器存在多处未授权访问漏洞
    技术文章阅读-红蓝对抗系列之浅谈蓝队反制红队的手法一二
    技术文章阅读-Solr ReplicationHandler漏洞浅析
    技术文章阅读-记一次edu站点从敏感信息泄露到getshell
  • 原文地址:https://www.cnblogs.com/suflowers1700218/p/11532182.html
Copyright © 2020-2023  润新知