OpenXML 如何获取范围内的单元格

2024-05-27

请帮助我获取范围内的单元格(例如从 A:1 到 E:11 都是矩形单元格)。 目前我的理想是

 Worksheet worksheet = GetWorksheet(document, sheetName);
        SheetData sheetData = worksheet.GetFirstChild<SheetData>();
        IEnumerable<Cell> cells = sheetData.Descendants<Cell>().Where(c =>
            c.CellReference >= A:1 &&
            c.CellReference <= E:11 &&
            );
        int t = cells.Count();

但这段代码不起作用。 谢谢


将单元格的 CellReference 与字符串进行比较并不那么容易。是的,你现在所做的事情是错误的。你根本无法比较strings以这种方式更高或更低。

你有两个选择。

选项1 :

您可以获取单元格引用并将其分解。这意味着将字符和数字分开,然后分别给它们赋值并进行比较

A1 - > A and 1 -> Give A =1 so you have 1 and 1

E11 -> E and 11 -> Give E = 5 so you have 5 and 11

所以你需要分解CellReference并检查您的要求的有效性。

选项 2:

如果您注意到上面的内容,我们只是采用二维矩阵索引(ex : 1,1 and 5,11 which are COLUMN,ROW format)。您可以简单地使用此功能进行比较。但问题是你不能使用LINQ为此,您需要迭代行和列。我尝试给出以下示例代码,尝试一下

 using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open("PATH", true))
    {
        //Get workbookpart
        WorkbookPart workbookPart = myDoc.WorkbookPart;

        // Extract the workbook part
        var stringtable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();

        //then access to the worksheet part
        IEnumerable<WorksheetPart> worksheetPart = workbookPart.WorksheetParts;

        foreach (WorksheetPart WSP in worksheetPart)
        {
            //find sheet data
            IEnumerable<SheetData> sheetData = WSP.Worksheet.Elements<SheetData>();

            int RowCount = 0;
            int CellCount = 0;

            // This is A1
            int RowMin = 1;
            int ColMin = 1;

            //This is E11              
            int RowMax = 11;
            int ColMax = 5;

            foreach (SheetData SD in sheetData)
            {
                foreach (Row row in SD.Elements<Row>())
                {
                    RowCount++; // We are in a new row

                    // For each cell we need to identify type
                    foreach (Cell cell in row.Elements<Cell>())
                    {
                        // We are in a new Cell
                        CellCount++;

                        if ((RowCount >= RowMin && CellCount >= ColMin) && (RowCount <= RowMax && CellCount <= ColMax))
                        {

                            if (cell.DataType == null && cell.CellValue != null)
                            {
                                // Check for pure numbers
                                Console.WriteLine(cell.CellValue.Text);
                            }
                            else if (cell.DataType.Value == CellValues.Boolean)
                            {
                                // Booleans
                                Console.WriteLine(cell.CellValue.Text);
                            }
                            else if (cell.CellValue != null)
                            {
                                // A shared string
                                if (stringtable != null)
                                {
                                    // Cell value holds the shared string location
                                    Console.WriteLine(stringtable.SharedStringTable.ElementAt(int.Parse(cell.CellValue.Text)).InnerText);
                                }
                            }
                            else
                            {
                                Console.WriteLine("A broken book");
                            }

                        }
                    }
                    // Reset Cell count
                    CellCount = 0;
                }
            }
        }
    }

这确实有效。我测试过。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OpenXML 如何获取范围内的单元格 的相关文章

随机推荐