Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: Exception while reading a cell with datatype number and cell is empty. [9373]

$
0
0
Hi,

I am getting a runtime exception 'Input string was not in a correct format' when I am trying to read a cell of type 'Number' and when its blank in the excel sheet.

Below is the code while writing the excel. Here I m explicitly inserting __blank__ when the value is __zero__ and setting the datatype as XLCellValues.Number.
```
var locationValue = GetLocValue(1);
var cellindex = "A1";
worksheetProposal.Cell(cellindex)
.SetValue(locationValue == 0 ? string.Empty : locationValue.ToString())
.SetDataType(XLCellValues.Number);
```

Below is the code while reading the generated excel. Its throwing an exception when m tring the read the same cell "A1" when its blank.
```
var LocationId = worksheet.Cell("A1").Value; // Runtime exception 'Input string was not in a correct format' when its blank.

```

Does anyone came across such scenario? Any solution for this?
Comments: The problem is in your code. You can't add 2 strings/texts. Try to do it in Excel: put '100 on cell A1 (include the apostrophe to tell Excel that it's a string), '200 on cell A2 (include the apostrophe to tell Excel that it's a string), and =SUM(A1:A2) on A3. It will give you 0. One quick fix for your code is to use cell.Value = "string with number". It will detect a number in the string and convert it accordingly. cell.SetValue doesn't convert strings into numbers. It assumes that if you pass a string you want it to be string in the cell.

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>