.NET MS Interop Word 不在 UTF8 网页中保存文档

标签 .net office-interop

注意:我用于测试的示例文档可以在以下位置找到:http://ftp.3gpp.org//Specs/archive/38_series/38.413/38413-100.zip

问题

我正在尝试使用以下代码将 MS Word 97-2003 文档 (.doc) 转换为 UTF-8 网页:

var wordApp = new Word.Application();
var doc = wordApp.Documents.Open("input.doc");
Console.WriteLine(doc.TextEncoding); // msoEncodingWestern
doc.SaveEncoding = MsoEncoding.msoEncodingUTF8;
doc.WebOptions.Encoding = MsoEncoding.msoEncodingUTF8;
doc.SaveAs2("output.htm", WdSaveFormat.wdFormatFilteredHTML, Encoding: MsoEncoding.msoEncodingUTF8);
doc.Close();
wordApp.Quit();

问题是文档包含某个字符,该字符在网页中呈现不正确:

在文档中

enter image description here

在网页中

enter image description here

(信息)手动方式

有关信息,如果我以如下手动方式执行上述操作,则箭头字符会在网页中正确呈现。

enter image description here

最佳答案

我认为您以太多不同的方式引用了编码。您应该只需要设置一次。

试试这个:

var wordApp = new Word.Application();
var doc = wordApp.Documents.Open("input.doc");
doc.Fields.Update(); // ** this is the new line of code.
Console.WriteLine(doc.TextEncoding); // msoEncodingWestern
doc.WebOptions.Encoding = MsoEncoding.msoEncodingUTF8;
doc.SaveAs2("output.htm", WdSaveFormat.wdFormatFilteredHTML);
doc.Close();
wordApp.Quit();

关于.NET MS Interop Word 不在 UTF8 网页中保存文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60931142/

相关文章:

c# - 使用 Word Interop 在 C# 中进行拼写检查

ms-word - WinDbg:如何从核心转储中获取在 Word 对话框中显示的消息?

c# - 创建 Excel 应用程序时出现 COM 异常 80040154

c# - 使用 XmlInclude 的多态元素类型名称..?

c# - GridView 链接列值

.net - Owin 获取查询字符串参数

.net - 为什么我们激活它时Word没有 "come to front"?

c# - 使用来自对象的参数格式化字符串

.net - 如何在.net 中并行启动多个操作?

xml - 如何自动将 Doc/Docx 转换为单个 XML 文件?