如何在 MS Windows XP 中检索 Unicode CSV 剪贴板数据?
我正在为一个较大的项目编写一个测试应用程序,似乎无法从 Windows 剪贴板检索 Unicode CSV 数据,我使用内置的 GetClipboardData api 调用成功检索 CF_UNICODETEXT,但是当我将 Unicode CSV 放在 MSExcel 中的剪贴板上时,尝试使用 CSV 格式检索,但得到的数据不正确。这是一些代码;
procedure TForm1.Button7Click(Sender: TObject);
var
hMem : THandle;
dwLen : DWord;
ps1, ps2 : pChar;
begin
OpenClipboard( form1.Handle );
RichEdit1.Lines.Clear;
try
if Clipboard.HasFormat( CF_UNICODETEXT ) then
begin
hMem := GetClipboardData( CF_UNICODETEXT );
ps1 := GlobalLock( hMem );
dwLen := GlobalSize( hMem );
ps2 := StrAlloc( 1 + dwLen );
StrLCopy( ps2, ps1, dwLen );
GlobalUnlock( hMem );
RichEdit1.Lines.Add( ps2 );
end
else
ShowMessage( 'No CF_UNICODETEXT on Clipboard!' );
finally
CloseClipboard;
end;
end;
现在这段代码也应该适用于 CSV,但是当我将剪贴板格式更改为我想要的格式时,应用程序将无法获取正确的数据。重要的是要知道我可以很好地获得选项卡式 Unicode,但不是我想要的 CSV。
I am writing a test app for a larger project and cant seem to retrieve Unicode CSV data from the Windows Clipboard, I am successful at retrieving CF_UNICODETEXT, using the built in GetClipboardData api call, however when I place Unicode CSV on the clipboard in MSExcel and try to retrieve with CSV format, I get bad data. Here is some code;
procedure TForm1.Button7Click(Sender: TObject);
var
hMem : THandle;
dwLen : DWord;
ps1, ps2 : pChar;
begin
OpenClipboard( form1.Handle );
RichEdit1.Lines.Clear;
try
if Clipboard.HasFormat( CF_UNICODETEXT ) then
begin
hMem := GetClipboardData( CF_UNICODETEXT );
ps1 := GlobalLock( hMem );
dwLen := GlobalSize( hMem );
ps2 := StrAlloc( 1 + dwLen );
StrLCopy( ps2, ps1, dwLen );
GlobalUnlock( hMem );
RichEdit1.Lines.Add( ps2 );
end
else
ShowMessage( 'No CF_UNICODETEXT on Clipboard!' );
finally
CloseClipboard;
end;
end;
Now this code should work for CSV as well, but when I change my Clipboard format to what I'm desiring, the app will not get proper data. It might be important to know that I can get tabbed Unicode just fine, just not he CSV I desire.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Excel 使用的 CSV 剪贴板格式 是 ANSI编码,而不是 Unicode。
通过转储 Excel 2007 剪贴板,启用 Unicode 的有:
“XML 电子表格”和“HTML 格式”都有明确定义的表/行,因此它们应该'从中提取数据并不难。
The CSV clipboard format Excel uses is ANSI encoded, not Unicode.
From dumping the Excel 2007 clipboard, the ones that are Unicode enabled are:
"XML Spreadsheet" and "HTML Format" both have well defined tables/rows, so they shouldn't be too hard to pull data from.
您需要请求 CF_CSV 格式。在获得 CF_CSV 形式的数据后,您可以将其视为 AnsiString,然后根据需要转换为 UnicodeString。
下面的屏幕截图显示了从 Excel2007 复制的 6 个单元格。我将其捕获为 CF_CSV,然后用 ClipMate 的十六进制查看器显示。您将看到这些字段以逗号(十六进制 2C)分隔,并以 CRLF (x0Dx0A) 终止。您在下面看到的是一个带注释的合成图,显示了 Excel、复制的区域以及 ClipMate 将 CF_CSV 呈现为十六进制字节的情况。
(来源:thornsoft.com)
另外,有趣的阅读相关主题:
从剪贴板获取 CSV 数据(粘贴自Excel) 包含重音字符
You need to request the CF_CSV format. AFTER you get the data as CF_CSV, then you can treat it as an AnsiString, and then convert to a UnicodeString, if you desire.
Here's a screenshot showing 6 cells copied from Excel2007. I captured into ClipMate as CF_CSV, then displayed with ClipMate's hex viewer. You'll see that the fields are separated by commas (hex 2C), terminated by CRLF (x0Dx0A). What you see below is an annotated composite, showing Excel, the region copied, and ClipMate's rendering of the CF_CSV as hex bytes.
(source: thornsoft.com)
Also, interesting reading in this related thread:
Get CSV Data from Clipboard (pasted from Excel) that contains accented characters