Arkesh Kumar
Arkesh Kumar

Reputation: 11

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

If I'm converting same value to string then in Excel file I'm getting Suggestion Like 2nd screenshot. Guys Please help me out in this.

Upvotes: 1

Views: 4418

Answers (3)

er-sho
er-sho

Reputation: 9771

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.

You just need to change your datatype to string

Upvotes: 2

sushmitgos
sushmitgos

Reputation: 153

CellValue has two constructor one is default another is parameterize CellValue(String). So you need to convert your parameter to string value.

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

Upvotes: 2

Icce
Icce

Reputation: 51

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

Upvotes: 0

Related Questions