[英]How can I only get visible cells in an Excel spreadsheet using OpenXML?
I am pulling data from cells in an Excel spreadsheet using OpenXML in C#.我正在使用 C# 中的 OpenXML 从 Excel 电子表格中的单元格中提取数据。 I only want to pull data if the cell is visible on the spreadsheet.
如果单元格在电子表格上可见,我只想提取数据。 I can get all the cells with the code below:
我可以使用以下代码获取所有单元格:
var cells = part.Worksheet.Descendants<Cell>;
I can then use the "CellReference.Value" property to figure out what column the cell belongs to.然后我可以使用“CellReference.Value”属性来确定单元格属于哪一列。
The code below will give me the visible columns on the spreadsheet.下面的代码将为我提供电子表格上的可见列。
var visible_columns = part.Worksheet.Descendants<Column>().Where(a => a.Hidden == null || a.Hidden.Value == false);
I am now stuck trying to programmatically associate the cell object with its column object.我现在被困在试图以编程方式将单元格 object 与其列 object 关联起来。 From what I can tell there is no property on the column object to get it's name.
据我所知,object 列上没有属性来获取它的名称。 Ideally I would get the column name from the "CellReference.Value" property on the cell object using a regular expression.
理想情况下,我会使用正则表达式从单元格 object 上的“CellReference.Value”属性中获取列名。 Once I had that I could use it to get the associated column object, which I could then use to check the Hidden property.
一旦有了它,我就可以使用它来获取关联的列 object,然后我可以使用它来检查 Hidden 属性。
I also looked at the "Parent" property of the cell object, but this gives me the a Row object which doesn't solve my issue.我还查看了单元格 object 的“父”属性,但这给了我一个行 object 并不能解决我的问题。 Can anyone point me in the right direction?
谁能指出我正确的方向?
Thanks谢谢
Here is how you can read cells that are not inside the hidden rows or columns:以下是读取不在隐藏行或列内的单元格的方法:
static void Main()
{
using (var spreadsheetDocument = SpreadsheetDocument.Open("input.xlsx", false))
{
var workbookPart = spreadsheetDocument.WorkbookPart;
var worksheetPart = workbookPart.WorksheetParts.First();
var worksheet = worksheetPart.Worksheet;
var columns = worksheet.Elements<Columns>().First();
// Get names of the hidden columns.
var hiddenColumnNames = new HashSet<string>();
foreach (var column in columns.Elements<Column>().Where(c=> c.Hidden != null && c.Hidden.Value))
for (uint min = column.Min, max = column.Max; min <= max; min++)
hiddenColumnNames.Add(GetColumnName(min));
var sheetData = worksheet.Elements<SheetData>().First();
foreach (var row in sheetData.Elements<Row>())
{
// Skip cells that are in hidden row.
if (row.Hidden != null && row.Hidden.Value)
continue;
foreach (var cell in row.Elements<Cell>())
{
// Skip cell that is in hidden column.
var columnName = cell.CellReference.Value.Replace(row.RowIndex.ToString(), "");
if (hiddenColumnNames.Contains(columnName))
continue;
// TODO: read visible cell ...
}
}
}
}
static string GetColumnName(uint columnNumber)
{
string columnName = "";
while (columnNumber > 0)
{
uint modulo = (columnNumber - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
columnNumber = (uint)((columnNumber - modulo) / 26);
}
return columnName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.