I have a file that has been generated by a program (through Excel Com automation). I manually altered the original file to only create one named ranged pointing to only one cell (A1). The value of the cell is 1 (see attached file). When I use the below code, the value that is retrieved is: 4
Note: the original generated file contained around 400 named ranged that I manually removed to only create the range "Foo".
Excel ef = new Excel();
ef.Open("CheckCell.xls");
Cell cell;
Names names = ef.Worksheets.Names;
Name name;
name = names["Foo"];
if (name != null && name.Text != null)
{
Range range = ef.Worksheets.GetRangeByName(name.Text);
cell = ws.Cells[range.FirstRow, range.FirstColumn];
if (cell.IntValue != 0)
{
Console.WriteLine(cell.IntValue.ToString());
}
}
This problem is caused by your Excel file. Worksheet 0 is not "P&L", but another worksheet hidden by vba code. Please:
Click Tool->Macro->Visual Basic Editor, you can see 2 worksheets.
To retrieve the correct data, please change your code to:
1.
Aspose.Cells.License license = new Aspose.Cells.License();
//set License
license.SetLicense("d:\\lic\\Aspose.Custom.5744.lic");
Excel ef = new Excel();
ef.Open("d:\\test\\CheckCell.xls");
Worksheet ws = ef.Worksheets[1];
Cell cell;
Names names = ef.Worksheets.Names;
Name name;
name = names["Foo"];
if (name != null && name.Text != null)
{
Range range = ef.Worksheets.GetRangeByName(name.Text);
cell = ws.Cells[range.FirstRow, range.FirstColumn];
if (cell.IntValue != 0)
{
Console.WriteLine(cell.IntValue.ToString());
}
}
2.
Aspose.Cells.License license = new Aspose.Cells.License();
//set License
license.SetLicense("d:\\lic\\Aspose.Custom.5744.lic");
Excel ef = new Excel();
ef.Open("d:\\test\\CheckCell.xls");
Cell cell;
Names names = ef.Worksheets.Names;
Name name;
name = names["Foo"];
if (name != null && name.Text != null)
{
Range range = ef.Worksheets.GetRangeByName(name.Text);
cell = range[0, 0];
if (cell.IntValue != 0)
{
Console.WriteLine(cell.IntValue.ToString());
}
}
Thanks a lot,
Indeed, there is a hidden worksheet I was not aware of.
Omid.