簡體   English   中英

Excel單元格值在Excel.Interop和C#中顯示不正確

[英]Excel cell value displayed incorrectly with Excel.Interop and C#

我正在通過Excel.Interop和C#向Excel單元中動態添加13位數字的值。 但是,該值顯示為7,21119E+12而不是7211192800080 如果單擊該單元格,則顯示正確。

//_ReportData is a DataTable with a single item
for (int i = 0; i < _ReportData[0].Rows.Count; i++)
{
    for (int j = 0; j < _ReportData[0].Columns.Count; j++)
    {
        excelCellRange_data = ((Worksheet)excelWorksheet_data).Cells;
        var cellValueRange = ((Range)excelCellRange_data)[i, j + 1];
        if (i == 0)
            ((Range)cellValueRange).Value2 = _ReportData[0].Columns[j].ToString(); //This is where the columns are set
        else
            ((Range)cellValueRange).Value2 = _ReportData[0].Rows[i][j].ToString(); //This is where the values are set
    }
}

有什么幫助嗎?

那是一個Excel-“問題”。 如果直接在單元格中鍵入數字,請按Enter,同樣會出現該問題。 如果您首先將單元格格式設置為數字,它將在右側顯示。

((Range)cellValueRange).NumberFormat = "0";

您應該插入沒有ToString()方法的值。 Excel應該從插入的值類型自動應用格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM