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

时间:2015-03-05 03:32:00

标签: c# openxml

请帮助我让细胞进入范围(从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();

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

1 个答案:

答案 0 :(得分:1)

使用字符串比较单元格的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:

如果您注意到上述情况,我们只需采用2D矩阵索引(ex : 1,1 and 5,11 which are COLUMN,ROW format)即可。您可以简单地使用此功能进行比较。但是catch是你不能使用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;
                }
            }
        }
    }

这实际上有效。我测试了。

相关问题