简体   繁体   中英

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#. 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.

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. From what I can tell there is no property on the column object to get it's name. Ideally I would get the column name from the "CellReference.Value" property on the cell object using a regular expression. Once I had that I could use it to get the associated column object, which I could then use to check the Hidden property.

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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM