简体   繁体   English

如何使用 OpenXML 仅在 Excel 电子表格中获取可见单元格?

[英]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.

相关问题 使用OpenXML如何在Excel电子表格中获取现有样式的StyleIndex - With OpenXML how do I get the StyleIndex of an existing style in an excel spreadsheet 当电子表格 window 最小化时,如何获取我的 Excel VSTO 插件以更新单元格? - How can I get my Excel VSTO Add-in to update cells when the spreadsheet window is minimized? 如何提高OpenXml Excel电子表格工具中从SharedStringTable检索值的性能? - How can I improve the performance of retrieving values from SharedStringTable in OpenXml Excel spreadsheet tools? 如何使用 OpenXML 获取 Excel 工作表的使用范围? - How can I get an Excel worksheet's used range with OpenXML? 如何在c#中使用openxml启用对excel中某些指定单元格和工作表的只读权限 - How to enable read only permision to some specified cells and sheets in excel using openxml in c# 如何使用DocumentFormat.OpenXml C#获取excel工作表中的特定列? - How can I get the specific column in excel worksheet using DocumentFormat.OpenXml C#? 使用OPENXML读取时Excel电子表格行单元格乱序 - Excel spreadsheet row cells out of order when read with OPENXML 如何使用C#隐藏OpenXML电子表格中的列? - How do I hide columns in OpenXML spreadsheet using C#? 如何使用OpenXML C#水平合并Microsoft Word表格单元格# - How i can merge Microsoft Word table cells horizontally using OpenXML C# 使用openxml合并excel中的相邻单元格 - Merge adjacent cells in excel using openxml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM