zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    AC自动机【萌新文章】
    ubuntu安装pandas
    pandas.Series函数用法
    关于scikit-learn
    关于scikit-learn
    Ubuntu安装openmpi
    Ubuntu分区小知识与分区方案
    Ubuntu16.04安装x11VNC远程桌面
    Ubuntu用户权限管理(chown, chmod)
    Ubuntu新建用户组
  • 原文地址:https://www.cnblogs.com/suflowers1700218/p/11532182.html
Copyright © 2011-2022 走看看