C# 从包含重音字符的剪贴板(从 Excel 粘贴)获取 CSV 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/967878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get CSV Data from Clipboard (pasted from Excel) that contains accented characters
提问by namenlos
SCENARIO
设想
- My users will copy cells from Excel (thus placing it into the clipboard)
- And my application will retrieve those cells from the clipboard
- 我的用户将从 Excel 复制单元格(从而将其放入剪贴板)
- 我的应用程序将从剪贴板中检索这些单元格
THE PROBLEM
问题
- My code retrieves the CSV format from the clipboard
- However, the if the original Excel content contains characters like ? (a with umlaut) then retrieved CSV string doesn't have the correct characters (? ends up showing as a "square" for me)
- In comparison, if my code retrieves the Unicode text format from the clipboard everything works fine: the ? is preserved in the string retrieved from the clipboard
- 我的代码从剪贴板中检索 CSV 格式
- 但是,如果原始 Excel 内容包含像 ? (带有变音符号)然后检索到的 CSV 字符串没有正确的字符(?对我来说最终显示为“正方形”)
- 相比之下,如果我的代码从剪贴板检索 Unicode 文本格式,一切正常: ? 保存在从剪贴板检索的字符串中
SOURCE CODE - ORIGINAL - WITH THE PROBLEM
源代码 - 原始 - 有问题
[STAThread]
static void Main(string[] args)
{
var fmt_csv = System.Windows.Forms.DataFormats.CommaSeparatedValue;
// read the CSV
var dataobject = System.Windows.Forms.Clipboard.GetDataObject();
var stream = (System.IO.Stream)dataobject.GetData(fmt_csv);
var enc = new System.Text.UTF8Encoding();
var reader = new System.IO.StreamReader(stream,enc);
string data_csv = reader.ReadToEnd();
// read the unicode string
string data_string = System.Windows.Forms.Clipboard.GetText();
}
THE RESULTS WHEN RUNNING THE SAMPLE CODE
运行示例代码时的结果
- Repro steps: Enter some text in Excel (I used the word "doppelg?nger" plus some numbers) and simply hit Ctrl-C to copy it to the clipboard and then run the code above.
- data_csv is set to "doppelg?nger,1\r\n2,3\r\n\0"
- data_string is set to "doppelg?nger\t1\r\n2\t3\r\n"
- 重现步骤:在 Excel 中输入一些文本(我使用了单词“doppelg?nger”加上一些数字),然后只需按 Ctrl-C 将其复制到剪贴板,然后运行上面的代码。
- data_csv 设置为“doppelg?nger,1\r\n2,3\r\n\0”
- data_string 设置为“doppelg?nger\t1\r\n2\t3\r\n”
QUESTION
题
- What can I do to get the correct characters?
- 我该怎么做才能获得正确的字符?
COMMENTS
注释
- Yes, I know I could workaround this problem by using the Unicode text. But I actually want to understand what is going on with the CSV
- using or not using the UTF-8 encoding when retrieving the stream makes no difference in the results
- 是的,我知道我可以通过使用 Unicode 文本来解决这个问题。但我实际上想了解 CSV 发生了什么
- 检索流时使用或不使用 UTF-8 编码对结果没有影响
THE ANSWER
答案
After looking at the comments, and paying close attention to what Excel was putting on the clipboard for CSV, it seemed reasonable that Excel might be placing the contents using an "legacy" encoding instead of UTF-8. So I tried the using the Windows 1252 codepage as the encoding and it worked. See the code below
在查看评论并密切关注 Excel 为 CSV 放在剪贴板上的内容后,Excel 可能使用“传统”编码而不是 UTF-8 来放置内容似乎是合理的。所以我尝试使用 Windows 1252 代码页作为编码并且它起作用了。看下面的代码
SOURCE CODE - WITH THE ANSWER
源代码 - 答案
[STAThread]
static void Main(string[] args)
{
var fmt_csv = System.Windows.Forms.DataFormats.CommaSeparatedValue;
//read the CSV
var dataobject = System.Windows.Forms.Clipboard.GetDataObject();
var stream = (System.IO.Stream)dataobject.GetData(fmt_csv);
var enc = System.Text.Encoding.GetEncoding(1252);
var reader = new System.IO.StreamReader(stream,enc);
string data_csv= reader.ReadToEnd();
//read the Unicode String
string data_string = System.Windows.Forms.Clipboard.GetText();
}
回答by Larry K
Your encoding of the stream as UTF8 is not working. The bytes for the umlaut are being converted into the "replacement character" unicode character.
您将流编码为 UTF8 不起作用。元音变音的字节被转换成“替换字符”unicode 字符。
Instead, just look at the stream's data without any extra encoding instructions. The data will be in some set format used by Excel. You should be able to tell by looking at the byte(s) where the unlaut is. You should then be able to convert it to UTF-8.
相反,只需查看流的数据,无需任何额外的编码指令。数据将采用 Excel 使用的某种设置格式。您应该能够通过查看 unlaut 所在的字节来判断。然后,您应该能够将其转换为 UTF-8。
Worst case is if the CSV Formatter throws out everything that is not Ascii. In that case, you might be able to write your own Data formatter.
最坏的情况是 CSV Formatter 会丢弃所有不是 Ascii 的内容。在这种情况下,您也许可以编写自己的数据格式化程序。
In some cases, the Excel folks have figured that CSV means Ascii only. See http://www.tech-archive.net/Archive/Excel/microsoft.public.excel.misc/2008-07/msg02270.html
在某些情况下,Excel 人员认为 CSV 仅表示 Ascii。见http://www.tech-archive.net/Archive/Excel/microsoft.public.excel.misc/2008-07/msg02270.html
回答by Peter Ruderman
Excel stores the string on the clipboard using the Unicode character encoding. The reason you get a square when you try to read the string in ANSI is that there is no representation for that character in your system's ANSI codepage. You should just use Unicode. If you're going to be dealing with localization issues, then ANSI is just more trouble than it's worth.
Excel 使用 Unicode 字符编码将字符串存储在剪贴板上。当您尝试读取 ANSI 中的字符串时,您得到正方形的原因是系统的 ANSI 代码页中没有该字符的表示形式。您应该只使用 Unicode。如果您要处理本地化问题,那么 ANSI 只会比它的价值更麻烦。
Edit:Joel Spolsky wrote an excellent introduction to character encodings, which is definitely worth checking out: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
编辑:Joel Spolsky 写了一篇关于字符编码的精彩介绍,绝对值得一看:每个软件开发人员绝对、肯定必须知道的绝对最小值(没有借口!)