Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用OpenXMLC从.xlsx获取单元格中显示的值#_C#_Openxml - Fatal编程技术网

C# 如何使用OpenXMLC从.xlsx获取单元格中显示的值#

C# 如何使用OpenXMLC从.xlsx获取单元格中显示的值#,c#,openxml,C#,Openxml,我不熟悉C#和开放XML,所以请耐心等待我的回答 我有一个问题: 我需要从.xlsx文件中获取单元格值。我可以使用XlGetCellValue方法来实现这一点。 但当一个单元格(例如来自sheet1的A2)从另一个单元格(B2 sheet2)获取其值时 XlGetCellValue(“,“Sheet1”,“A2”) 返回Sheet2!B2Joe 或者当单元格包含计算(如=C2+D2)时,XlGetCellValue(…)返回C2+D2120 有没有简单的方法来获取“Joe”和“120”的值?这里

我不熟悉C#和开放XML,所以请耐心等待我的回答

我有一个问题:

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

XlGetCellValue(“,“Sheet1”,“A2”)
返回
Sheet2!B2Joe

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


有没有简单的方法来获取“Joe”和“120”的值?

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


如果您还没有下载OpenXMLSDKToolV25.msi(生产力工具),那么使用openxmnl可能是一件麻烦事

基本上,它是一个反射工具。你可以打开一个excel文档,这个工具可以创建所有你需要的代码来从头开始构建相同的文档

CellValue仅用于值。使用公式必须处理单元格公式

例如,我在A1=1256的B1=2的C1中创建了一个excel文件“=A1*B1” 使用我得到的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;
    }
public Row GenerateRow()
{
row1=newrow(){RowIndex=(UInt32Value)1U,span=newlistvalue(){InnerText=“1:3”};
Cell cell1=新单元格(){CellReference=“A1”};
CellValue cellValue1=新的CellValue();
cellValue1.Text=“1256”;
cell1.Append(cellValue1);
Cell cell2=新单元格(){CellReference=“B1”};
CellValue cellValue2=新的CellValue();
cellValue2.Text=“2”;
cell2.Append(cellValue2);
Cell cell3=新单元格(){CellReference=“C1”};
CellFormula cellFormula1=新的CellFormula();
单元格公式1.Text=“A1*B1”;
CellValue cellValue3=新的CellValue();
cellValue3.Text=“2512”;
单元格3.追加(单元格公式1);
cell3.Append(cellValue3);
行1.追加(单元格1);
行1.追加(单元格2);
第1行。追加(第3单元格);
返回第1行;
}
由此您可以注意到CellValue和CellFormula都是Cell的子项。 因此,假设您可以检索行r,您可以得到:

Cell c = r.Elements<Cell>().ElementAt(2);
CellValue cv = c.CellValue;
CellFormula cf = c.CellFormula;
Cell c=r.Elements().ElementAt(2);
CellValue cv=c.CellValue;
CellFormula cf=c.CellFormula;

我会查看此SO页面,看看这是否有助于您解决[SO页面][1][1]:这并不能解决问题。GetCellValue返回值的方式与XlGetCellValue完全相同。不过还是要谢谢您。