簡體   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