• 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 }
  • 相关阅读:
    聊聊简单又灵活的权限设计(RBAC)
    手把手搭建一个属于自己的在线 IDE
    聊一聊如何搭建高性能网站哪一些事
    一个老程序员的忠告:你这辈子输就输在以为靠技术就能生存下
    缓存提升性能的关键性手段
    python学习笔记1之-python简介及其环境安装
    聊一聊mycat数据库集群系列之双主双重实现
    mycat数据库集群系列之mycat读写分离安装配置
    mycat数据库集群系列之mysql主从同步设置
    mycat数据库集群系列之数据库多实例安装
  • 原文地址:https://www.cnblogs.com/suflowers1700218/p/11532182.html
Copyright © 2020-2023  润新知