首页 > 解决方案 > C# Open XML 查找 Excel 的行偏移量

问题描述

我在 C# 中使用 Open XML 进行 excel 操作。我有一个场景,我希望使用下面的单元格值合并单元格的行号是工作表,我将传递产品名称,它将返回其行偏移
Excel 模板

我正在使用下面的 linq 查询,但在合并单元格的情况下它不起作用

Row trow= worksheetPart.Worksheet.Descendants<Row>().Where(r=>r.InnerText==ProductName).FirstOrDefault();

Row = (int)trow.RowIndex.Value;

有没有办法找到合并单元格的行索引

提前致谢

标签: c#mergefindopenxmlopenxml-sdk

解决方案


在使用 OpenXML 期间非常重要的一件事是使用 Office Excel 的 XML 输出检查您正在编写的代码。
为此,我建议您安装7Zip并提取 .Xlsx 文件,然后检查提取文件夹中 Xmls 的内容。

注意:一个 office 文件包含多个 XML 文件,这些文件打包为一个 .Xlsx 文件。

然后转到 Xl->Worksheet->Sheet1.xml

那么你可以看到这样一个 XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
    <dimension ref="A1:A4"/>
    <sheetViews>
        <sheetView tabSelected="1" workbookViewId="0">
            <selection sqref="A1:A4"/>
        </sheetView>
    </sheetViews>
    <sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>
    <sheetData>
        <row r="1" spans="1:1" x14ac:dyDescent="0.25">
            <c r="A1" s="1" t="s">
                <v>0</v>
            </c>
        </row>
        <row r="2" spans="1:1" x14ac:dyDescent="0.25">
            <c r="A2" s="1"/>
        </row>
        <row r="3" spans="1:1" x14ac:dyDescent="0.25">
            <c r="A3" s="1"/>
        </row>
        <row r="4" spans="1:1" x14ac:dyDescent="0.25">
            <c r="A4" s="1"/>
        </row>
    </sheetData>
    <mergeCells count="1">
        <mergeCell ref="A1:A4"/>
    </mergeCells>
    <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>

在这个 excel 文件中,我已合并A1到,您可以在 XML 的末尾A4看到它。<mergeCells count="1">另外,我Hello在合并的单元格中写了

所以,我相信为了检查合并的单元格,你应该以某种方式使用。

SharedString.xml提取的文件夹内的文件中,您可以看到:

<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="1" uniqueCount="1"><si><t>Hello</t></si></sst>

所以你可以看到count属性的值等于<mergeCells />标签的count值。

这些介绍告诉您 OpenXml 只是一个仅解析 XML 的适当库。

如果您通过阅读 XML 文件并注意变量名称来查看我编写的以下代码,您会看到我只是尝试通过 XML 标记并找到我的正确值。

using (SpreadsheetDocument document = SpreadsheetDocument.Open(@"D:\test.xlsx", true))
{
    WorkbookPart wbPart = document.WorkbookPart;
    List<WorksheetPart> wsParts = wbPart.WorksheetParts.ToList();
    if (wsParts != null && wsParts.Any())
    {
        WorksheetPart SheetPart1 = wsParts.First();
        MergeCells mergeCells = SheetPart1.Worksheet.Elements<MergeCells>().First();
        foreach (MergeCell mergeCell in mergeCells)
        {
            string[] mergedRow = mergeCell.Reference.Value.Split(new string[]{":"},StringSplitOptions.None);
            Cell theCell = SheetPart1.Worksheet.Descendants<Cell>().
                Where(c => c.CellReference == mergedRow[0]).FirstOrDefault();
            string value = GetCellValue(document, theCell);
        }
    }
}

然后:

public string GetCellValue(SpreadsheetDocument document, Cell cell)
{
    SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
    string value = string.Empty;
    if (cell.CellValue != null)
    {
        value = cell.CellValue.InnerXml;
        if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
        {
            value = stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
        }

    }
    return value;
}

推荐阅读