[英]Excel cell value displayed incorrectly with Excel.Interop and C#
I am dynamically adding a 13 digit value to an Excel cell via Excel.Interop and C#. 我正在通过Excel.Interop和C#向Excel单元中动态添加13位数字的值。 However, the value is displayed as something in the lines of
7,21119E+12
instead of 7211192800080
. 但是,该值显示为
7,21119E+12
而不是7211192800080
。 If you click on the cell, then the display is correct. 如果单击该单元格,则显示正确。
//_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
}
}
Any help please? 有什么帮助吗?
That's a Excel-"problem". 那是一个Excel-“问题”。 If you type the number directly in a cell an press enter, the same issue will appear.
如果直接在单元格中键入数字,请按Enter,同样会出现该问题。 If you first format the cell as number, it will be shown right.
如果您首先将单元格格式设置为数字,它将在右侧显示。
((Range)cellValueRange).NumberFormat = "0";
You should insert value without ToString()
method. 您应该插入没有
ToString()
方法的值。 Excel should apply format automatically from inserted value type. Excel应该从插入的值类型自动应用格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.