4

我是 C# 新手并打开 XML,所以请耐心等待我的无知。

我有这个问题:

我需要从 .xlsx 文件中获取单元格值。我可以使用 XlGetCellValue 方法做到这一点。但是当一个单元格(例如来自 sheet1 的 A2)从另一个单元格(B2 sheet2)获取它的值时

XlGetCellValue("", "Sheet1", "A2") 返回Sheet2!B2Joe

或者当单元格包含计算(如 =C2+D2)时,XlGetCellValue(...)返回C2+D2120

有没有简单的方法来获得“乔”和“120”的价值?

4

2 回答 2

0

如果您还没有下载 OpenXMLSDKToolV25.msi(生产力工具),那么使用 openxmnl 可能会很痛苦。

基本上它是一个反射工具。您可以打开一个 excel 文档,该工具会创建从头开始构建所需的所有代码。

CellValue 它只是为了价值。使用公式你必须处理单元格公式。

例如。我在 A1 = 1256 in B1 = 2 in C1 "=A1*B1" 中创建了一个 excel 文件用 OpenXMLSDKTool 打开文件我得到:

    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;
    }

从这里你可以注意到 CellValue 和 CellFormula 都是 Cell 的孩子。所以,假设你可以检索你的 Row r 你可以有这个:

Cell c = r.Elements<Cell>().ElementAt(2);
CellValue cv = c.CellValue;
CellFormula cf = c.CellFormula;
于 2014-01-26T00:28:40.317 回答
0

这里是关于如何使用 Open XML SDK 2.5 获取单元格值的 MSDN 链接。提供了一个代码示例。

如何:检索电子表格文档中单元格的值(Open XML SDK)

于 2013-05-24T15:18:04.633 回答