简体   繁体   English

如何在OpenXML单元格中插入整数或十进制数

[英]How to insert integer or decimal number into openxml cell

Cell cellName = new Cell();
cellName.DataType = CellValues.Number;
cellName.CellValue = new CellValue(10);
//error i'm getting here: "cannot convert int to string"
newRow.AppendChild(cellName);

Here I'm getting error 我在这里出错

cannot convert int to string 无法将int转换为字符串

If I'm converting same value to string then in Excel file I'm getting Suggestion Like 2nd screenshot. 如果我要将相同的值转换为string那么在Excel文件中,我将获得“像第二张屏幕一样的建议”。 Guys Please help me out in this. 伙计们请帮助我。

cellName.CellValue = new CellValue(10); //error i am getting here "cannot convert int to string"

10 is an integer, while the constructor needs a string. 10是整数,而构造函数则需要一个字符串。 Manually, you can do it like this. 您可以手动执行此操作。

cellName.CellValue = new CellValue("10");

Otherwise, if you have a variable, do this: 否则,如果您有变量,请执行以下操作:

int number = 20;
cellName.CellValue = new CellValue(number.ToString());

CellValue has two constructor one is default another is parameterize CellValue(String) . CellValue有两个构造函数,一个是默认构造函数,另一个是参数化CellValue(String) So you need to convert your parameter to string value. 因此,您需要将参数转换为string值。

int number = 90;
CellValue cl = new CellValue(Convert.ToString(number));

You can do this by using following code 您可以使用以下代码来完成此操作

 Cell cellName = new Cell();
 cellName.DataType = CellValues.Number;

 //you can get or set more porperties with this object
 CellValue cellValue = new CellValue();

 //If you want to set text as number 
 cellValue.Text = Convert.ToString(10);

 //If you want to set text as boolean 
 cellValue.Text = Convert.ToString(true);

 //If you want to set text as decimal 
 cellValue.Text = Convert.ToString(123.45M);

 cellName.CellValue = cellValue;

cellValue.Text property gives you to gets or sets the text of the current element. cellValue.Text属性使您可以获取或设置当前元素的文本。

You just need to change your datatype to string 您只需要将数据类型更改为字符串

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

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