从 Word 文档转换为 HTML [英] Convert from Word document to HTML
本文介绍了从 Word 文档转换为 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 Word Viewer 将 Word 文档保存为 HTML 格式,而无需在我的机器上安装 Word.有什么办法可以在 C# 中实现这一点吗?
I want to save the Word document in HTML using Word Viewer without having Word installed in my machine. Is there any way to accomplish this in C#?
推荐答案
要将 .docx 文件转换为 HTML 格式,您可以使用 OpenXmlPowerTools.确保添加对 OpenXmlPowerTools.dll 的引用.
For converting .docx file to HTML format, you can use OpenXmlPowerTools. Make sure to add a reference to 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());
}
}
这篇关于从 Word 文档转换为 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文