MS Word Ole自动化、ADO和外国人物
-
06-09-2019 - |
题
我试图出口WideString文字从数据库(ADO/MS Access)以MS Word文件(Delphi7),但外国文字是不正确转移(即"è"而不是"č"):
while not ADOQuery1.Eof do
begin
WordApplication1.Selection.TypeText(ADOQuery1Text.AsVariant); // TWideStringField
WordApplication1.Selection.TypeParagraph;
ADOQuery1.Next;
end;
我还试着用 CreateOleObject()
直接的,但是没有差别。
我是什么丢失?
谢谢!
解决方案
我认为这不是一个问题与文字,而是与的方式串被存储在数据库。他们可能是保存为Ansi串,不如Unicode/WideString串。如果这是真的,那么,他们都保存在一些编码,你必须知道,如果你希望他们是正确解码。
这里是一个样本应用程序演示如何转换Ansi串入WideString,并将其保存在字:
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,
ComObj,
ActiveX,
CodecUtilsWin32;
procedure Test();
var
wordApp, wordDoc: Variant;
ansiStr: string;
codec: TUnicodeCodec;
function str2WideStr(const s: string): WideString;
var
i: Integer;
begin
codec.DecodeStr(@s[1], Length(s), Result);
end;
begin
codec := TEncodingRepository.CreateCodecByAlias('ISO-8859-2');
ansiStr := #$BF#$F3#$B3#$E6; //"zólc"
wordApp := CreateOleObject('Word.Application');
wordDoc := wordApp.Documents.Add;
wordApp.Selection.TypeText(str2WideStr(ansiStr));
wordDoc.SaveAs('C:\sample.doc');
wordDoc.Close();
wordApp.Quit(False);
end;
begin
CoInitialize(nil);
Test();
end.
上述代码使用的免费软件单元CodecUtilsWin32.考绩制度从 实用的图书馆五.2.0.18
所以我建议使用TStringField而不是TWideStringField和转换串WideStrings如在上面的例子。
其他提示
你有没有尝试过使用
WordApplication1.Selection.TypeText(ADOQuery1Text.AsWideString);
短的,我知道,德尔福2009年更好地处理Unicode(整个VCL现在支持它直接),将最有可能的纠正你的问题。
不隶属于 StackOverflow