繁体   English   中英

剪贴板的数字格式不同?

[英]Different number format for the clipboard?

我有一个在System.Windows.Forms.DataGridView中显示数字的程序。 我正在根据用户的区域设置格式化这些数字,这在我尝试将所述数字复制并粘贴到Excel中时会引起问题。 例如,123 456 789,00是芬兰语中正确的本地化格式,但是Excel将此解释为字符串而不是数字。 有没有一种方法可以使Excel理解数字中的千位分隔符或对剪贴板使用不同的数字格式?

您可以创建自己的DGV派生类,并重写GetClipboardContent()方法。 这样您就可以以与Excel兼容的方式来格式化字符串。 像这样:

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();
    }
}

未经测试。

暂无
暂无

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

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