Click here to Skip to main content
13,051,809 members (48,119 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
Hi community of Code Project,

im having a seroius trouble with reading data of an xls with the office.interop.excel.

My big problem is, that i read a cell in an xls, where content is defentily. When u open this xls with excel directly u can see that the value of the cell is "101". But when i try to read it with

string value = worksheet.Cells[1,1].Value as string;

i get null returned. The cell beside the one with "101" as content has as content "102" and on "103, 104, 105". I can read all of them. Just the 101 cell is returning NULL.

And im totally weird. SR guys but i cant upload this xls, its not for the public available.

So i have to ask if anyone has any idea what can be with this cell???

tia Synergi
Posted 12-Nov-12 23:10pm
Richard MacCutchan 13-Nov-12 5:15am
Have you checked the spreadsheet to be sure of what is in that cell?
SynergiAios 13-Nov-12 5:48am
Sr i cant understand ur question really. If u mean if i have checked in a real Excel 2007 if the content of the cell is "101", then i can say yes. Its a combined Cell of 2 cells. the content of these two cells is the text "101".
Richard MacCutchan 13-Nov-12 5:56am
Its a combined Cell of 2 cells.

Then it is not Cells[1,1].

OK, I have just tested this and it works OK, there must be something else in this cell that is preventing you from getting the value.
SynergiAios 13-Nov-12 6:27am
Thx for this advice but im know this x) it was just as an example badly descriped of me. To have an complete example lets say we have an xls with the following

Cell[1,1] & Cell[2,1] are combined with the content "101"
Cell[3,1] & Cell[4,1] are combined with the content "102"
Cell[5,1] & Cell[6,1] are combined with the content "103"

So the Cells in Code with the content would be: (allways the first cell of the combined one)

Cell[1,1] = "101"
Cell[3,1] = "102"
Cell[5,1] = "103"

So my Problem is that in Cell[1,1] = NULL, Cell[3,1] = "102", Cell[5,1] = "103", ...

Why is Cell[1,1] = NULL.

Thats my exact problem
Richard MacCutchan 13-Nov-12 6:33am
I have just created a spreadsheet with values and combined cells as you describe, and I get the correct values in each case. There must still be something in your data or in your program which is incorrect.
SynergiAios 13-Nov-12 8:58am
Ok i have no managed to make an copy of the xls and change it so i can upload it to the public. Here ist the xls to download

Try to read the Content of the Cell with the Content "101"

tia Synergi
HGH-Developers 13-Nov-12 10:21am
I think your problem is the type of the content, the cell does not contains a string of "101" but a numerical value 101.

worksheet.Cells[1,1].Value is not null but with "as string" it becomes null because it is not a string.
you can use worksheet.Cells[1,1].Value.ToString() (if .Value is not null of course)

Richard MacCutchan 13-Nov-12 11:21am
I have created cells with numerical values and they can be extracted without problems.
Richard MacCutchan 13-Nov-12 11:22am
Sorry but that site expects me to install some software on my PC in order to do the download. I am not going to do that. Try loading it somewhere else.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should use
as string
only if you are sure of the type of Value.

To get a string from any kind of value in the cell, you can use the ToString() method.
For an Excel Cell, you may have a .Text property or something similar.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I have managed to download your spreadsheet file (no idea what happened yesterday), and it contains the sequence 101, 102, ... in cells [1, 5], [1, 7] etc. I am able to access each of these values successfully with the following short code sequence:
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
for (int i = 5; i < 20; i += 2)
    string cellValue = xlWorksheet.Cells[1, i].Value.ToString();
    Console.WriteLine("Cell[1, {0}]: {1}", i, foo);

which gives these results:
Cell[1, 5]: 101
Cell[1, 7]: 102
Cell[1, 9]: 103
Cell[1, 11]: 104
Cell[1, 13]: 105
Cell[1, 15]: 106
Cell[1, 17]: 107
Cell[1, 19]: 108

What I did notice is that cell[1, 5] contains the value 101, whereas the remaining cells contain strings. Thus you cannot get the first cell as string, but need to use the ToString() method to convert it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100