如何将.docx转换为c#中的html文件并将其保存到各自的目录中?

时间:2017-03-20 04:49:41

标签: c#

我的格式为.docx格式。我想将.docx转换为.html将其保存在以下路径中:

我有200多个.docx文件。手动更改为.html非常困难。

.docx格式:

<START>
<TITLE>UAE0d23376</TITLE>
<BODY>
<P>3376</P>
<P>
urged that he should be sent to saint winifreds, with some vague notion of making a man of him. he<br>might as well have thrown a piece of brussels lace into the fire with intention of changing it into<br>you want be troubled with this one long, said her son; ill go with me, and that's soon 
</P>
</BODY>
<END>

需要更改为.html并保存到“c:\ ConvertedToHTML”下

你能帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

将.docx文件转换为HTML格式

添加对OpenXmlPowerTools.dll代码的引用:

using OpenXmlPowerTools;
using DocumentFormat.OpenXml.Wordprocessing;

byte[] byteArray = File.ReadAllBytes(DocxFilePath);
using (MemoryStream memoryStream = new MemoryStream())
{
    memoryStream.Write(byteArray, 0, byteArray.Length);
    using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
 {
      HtmlConverterSettings settings = new HtmlConverterSettings()
      {
           PageTitle = "My Page Title"
      };
      XElement html = HtmlConverter.ConvertToHtml(doc, settings);

      File.WriteAllText(HTMLFilePath, html.ToStringNewLineOnAttributes());
 }
}