我也尝试过,遇到了与您报告的相同的问题(我使用 MATLAB R2015a 和 Office 2013 进行了测试)...
我认为 MATLAB 和 Word 之间的 COM 层中的某些东西弄乱了文本编码。
为了确认这确实是 MATLAB 中的一个错误,我在 Python 中尝试了同样的方法,它运行良好:
#!/usr/bin/env python
import os
import win32com.client
word = win32com.client.Dispatch("Word.Application")
word.Visible = True
doc = word.Documents.Add()
str = u"Have you seen my " + unichr(9730) + u"?"
word.Selection.TypeText(str)
fname = os.path.join(os.getcwd(), "out.docx")
doc.SaveAs2(fname)
doc.Close()
word.Quit()
我为 MATLAB 提出了两种解决方法:
方法一(首选):
这个想法是创建一个使用 Office 互操作的 .NET 程序集。它将接收任何 Unicode 字符串并将其写入某个指定的 Word 文档。
然后可以将这个程序集加载到 MATLAB 中并用作 MS Office 的包装器。
C# 中的示例:
MSWord.cs
using System;
using Microsoft.Office.Interop.Word;
namespace MyOfficeInterop
{
public class MSWord
{
// this is very basic, but you can expose anything you want!
public void AppendTextToDocument(string filename, string str)
{
Application app = null;
Document doc = null;
try
{
app = new Application();
doc = app.Documents.Open(filename);
app.Selection.TypeText(str);
app.Selection.TypeParagraph();
doc.Save();
}
catch (Exception)
{
throw;
}
finally
{
doc.Close();
app.Quit();
}
}
}
}
我们先编译一下:
csc.exe /nologo /target:library /out:MyOfficeInterop.dll /reference:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Word.dll" MSWord.cs
然后我们从 MATLAB 中测试它:
%// load assembly
NET.addAssembly('C:\path\to\MyOfficeInterop.dll')
%// I am assuming the document file already exists
fname = fullfile(pwd,'test.docx');
fclose(fopen(fname,'w'));
%// some text
str = ['Have you seen my ' char(9730) '?'];
%// add text to Word document
word = MyOfficeInterop.MSWord();
word.AppendTextToDocument(fname, str);
方法二:
这更像是一个黑客!我们只需将 MATLAB 中的文本直接写入文本文件(正确编码)。然后我们使用 COM/ActiveX 接口在 MS Word 中打开它,并重新保存为合适的 .docx Word 文档。
例子:
%// params
fnameTXT = fullfile(pwd,'test.txt');
fnameDOCX = fullfile(pwd,'test.docx');
str = ['Have you seen my ' char(9730) '?'];
%// create UTF-8 encoded text file
bytes = unicode2native(str, 'UTF-8');
fid = fopen(fnameTXT, 'wb');
fwrite(fid, bytes);
fclose(fid);
%// some office interop constants (extracted using IL DASM)
msoEncodingUTF8 = int32(hex2dec('0000FDE9')); % MsoEncoding
wdOpenFormatUnicodeText = int32(hex2dec('00000005')); % WdOpenFormat
wdFormatDocumentDefault = int32(hex2dec('00000010')); % WdSaveFormat
wdDoNotSaveChanges = int32(hex2dec('00000000')); % WdSaveOptions
%// start MS Word
Word = actxserver('Word.Application');
%Word.Visible = true;
%// open text file in MS Word
doc = Word.Documents.Open(...
fnameTXT, ... % FileName
[], ... % ConfirmConversions
[], ... % ReadOnly
[], ... % AddToRecentFiles
[], ... % PasswordDocument
[], ... % PasswordTemplate
[], ... % Revert
[], ... % WritePasswordDocument
[], ... % WritePasswordTemplate
wdOpenFormatUnicodeText, ... % Format
msoEncodingUTF8, ... % Encoding
[], ... % Visible
[], ... % OpenAndRepair
[], ... % DocumentDirection
[], ... % NoEncodingDialog
[]); % XMLTransform
%// save it as docx
doc.SaveAs2(...
fnameDOCX, ... % FileName
wdFormatDocumentDefault, ... % FileFormat
[], ... % LockComments
[], ... % Password
[], ... % AddToRecentFiles
[], ... % WritePassword
[], ... % ReadOnlyRecommended
[], ... % EmbedTrueTypeFonts
[], ... % SaveNativePictureFormat
[], ... % SaveFormsData
[], ... % SaveAsAOCELetter
msoEncodingUTF8, ... % Encoding
[], ... % InsertLineBreaks
[], ... % AllowSubstitutions
[], ... % LineEnding
[], ... % AddBiDiMarks
[]), % CompatibilityMode
%// close doc, quit, and cleanup
doc.Close(wdDoNotSaveChanges, [], [])
Word.Quit()
clear doc Word