简体   繁体   English

剪贴板的数字格式不同?

[英]Different number format for the clipboard?

I have a program that displays numbers in a System.Windows.Forms.DataGridView . 我有一个在System.Windows.Forms.DataGridView中显示数字的程序。 I'm formatting those numbers according to the user's regional settings, which causes problems when I try to copy and paste said numbers into Excel. 我正在根据用户的区域设置格式化这些数字,这在我尝试将所述数字复制并粘贴到Excel中时会引起问题。 For example, 123 456 789,00 is the proper localized format in Finnish, but Excel interprets this as a string, not as a number. 例如,123 456 789,00是芬兰语中正确的本地化格式,但是Excel将此解释为字符串而不是数字。 Is there a way to either make Excel understand thousand separators in numbers or to use a different number format for the clipboard? 有没有一种方法可以使Excel理解数字中的千位分隔符或对剪贴板使用不同的数字格式?

You can create your own DGV derived class and override the GetClipboardContent() method. 您可以创建自己的DGV派生类,并重写GetClipboardContent()方法。 Which allows you to format the string in a way that's compatible with Excel. 这样您就可以以与Excel兼容的方式来格式化字符串。 Something like this: 像这样:

using System;
using System.Windows.Forms;

class MyDGV : DataGridView {
    public override DataObject GetClipboardContent() {
        if (this.SelectedCells.Count == 1 && this.SelectedCells[0].ColumnIndex == 1) {
            string value = string.Format("{0:N2}", this.SelectedCells[0].Value);
            return new DataObject(DataFormats.Text, value);
        }
        return base.GetClipboardContent();
    }
}

Untested. 未经测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM