c# - 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;
有没有办法找到合并单元格的行索引
提前致谢
解决方案
在使用 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;
}
推荐阅读
- c# - Environment.GetCommandLineArgs() 可能会得到一个不受信任的字符串
- javascript - 如何将firebase身份验证与电子一起使用
- for-loop - 有没有办法组合嵌套循环,其中内部循环迭代变量迭代?
- java - Spring MVC 从本地 C: 驱动器读取文件
- dynamics-crm - Dynamic 365 - 检索 API 元数据属性列表 - DateTimeBehavior
- mysql - 在 MySQL 中以 LIFO 方式查找经过的时间
- c++ - 当我有一个具体类型时,动态调度会起作用吗?
- angular - ng serve 在 Docker 容器中不起作用,但 express.js 可以
- python - 如何使用 pandas 将基于产品保质期从现有日期列派生的新日期列添加到我的 csv 文件中?
- woocommerce - Woocommerce 休息 api:设置 id