4

Im new to C# and open XML, so please be patient with my ingnorance.

I have this problem:

I need to get cell value from .xlsx file. I can do that using XlGetCellValue method. But When one cell (for example A2 from sheet1) gets it value from another cell (B2 sheet2)

XlGetCellValue("", "Sheet1", "A2") returns Sheet2!B2Joe.

Or when the cell contains computation (like =C2+D2), XlGetCellValue(...) returns C2+D2120

Is there any easy way to get just value "Joe" and "120?

glomad
  • 5,539
  • 2
  • 24
  • 38
Filip Ježek
  • 41
  • 1
  • 2
  • 1
    I would check out this SO page and see if this helps you out [SO page][1] [1]: http://stackoverflow.com/questions/5115257/open-xml-excel-read-cell-value – theboss May 24 '13 at 15:00

2 Answers2

0

Here the link to MSDN on how to get the value of a cell using the Open XML SDK 2.5. There is a code sample provided.

How to: Retrieve the values of cells in a spreadsheet document (Open XML SDK)

BrandonG
  • 876
  • 11
  • 20
  • This doesn't solve the problem. GetCellValue returns value exactly the same way as XlGetCellValue. But Thanks anyway. – Filip Ježek May 24 '13 at 18:18
0

Working with openxmnl could be a pain in the ass if you haven't yet downloaded OpenXMLSDKToolV25.msi (Productivity tool).

basically it's a reflection tool. you can open an excel document and the tool create all code you need to build the same from scratch.

CellValue it's only for value. using formula you have to deal with cell formula.

Eg. I created an excel file in A1 = 1256 in B1 = 2 in C1 "=A1*B1" Opening the file with OpenXMLSDKTool i got:

    public Row GenerateRow()
    {
        Row row1 = new Row(){ RowIndex = (UInt32Value)1U, Spans = new ListValue<StringValue>() { InnerText = "1:3" } };

        Cell cell1 = new Cell(){ CellReference = "A1" };
        CellValue cellValue1 = new CellValue();
        cellValue1.Text = "1256";

        cell1.Append(cellValue1);

        Cell cell2 = new Cell(){ CellReference = "B1" };
        CellValue cellValue2 = new CellValue();
        cellValue2.Text = "2";

        cell2.Append(cellValue2);

        Cell cell3 = new Cell(){ CellReference = "C1" };
        CellFormula cellFormula1 = new CellFormula();
        cellFormula1.Text = "A1*B1";
        CellValue cellValue3 = new CellValue();
        cellValue3.Text = "2512";

        cell3.Append(cellFormula1);
        cell3.Append(cellValue3);

        row1.Append(cell1);
        row1.Append(cell2);
        row1.Append(cell3);
        return row1;
    }

from this you can notice that CellValue and CellFormula are both childs of Cell. So, assuming you can retrieve your Row r you can have this:

Cell c = r.Elements<Cell>().ElementAt(2);
CellValue cv = c.CellValue;
CellFormula cf = c.CellFormula;
Francesco Milani
  • 415
  • 4
  • 11