Open XML读取Excel表格

1、读取特定行列的单元格

private static void ReadExcel(string filePath,string sheetName,string colName,uint rowIndex)
        {
            using (SpreadsheetDocument spreadsheetDocument=SpreadsheetDocument.Open(filePath,false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                Workbook workbook = workbookPart.Workbook;

                IEnumerable sheets = workbook.GetFirstChild().Elements().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return;
                }
                string relationshipId = sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);
                Cell cell = GetSpreadsheetCell(worksheetPart.Worksheet, colName, rowIndex);
                if (cell == null)
                {
                    return;
                }
                Console.WriteLine(cell.CellValue.Text);
            }
        }

private static Cell GetSpreadsheetCell(Worksheet worksheet, string columnName, uint rowIndex)
        {
            IEnumerable rows = worksheet.GetFirstChild().Elements().Where(r => r.RowIndex == rowIndex);
            if (rows.Count() == 0)
            {
                // A cell does not exist at the specified row.
                return null;
            }

            IEnumerable cells = rows.First().Elements().Where(c => string.Compare(c.CellReference.Value, columnName + rowIndex, true) == 0);
            if (cells.Count() == 0)
            {
                // A cell does not exist at the specified column, in the specified row.
                return null;
            }

            return cells.First();
        }

2、读取对应Sheet里的所有值

private static void ReadExcel(string filePath, string sheetName)
        {
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, false))
            {
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                Workbook workbook = workbookPart.Workbook;

                IEnumerable sheets = workbook.GetFirstChild().Elements().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return;
                }
                string relationshipId = sheets.First().Id.Value;
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);
                foreach (var dic in GetAllCell(worksheetPart.Worksheet))
                {
                    Console.WriteLine($"单元格:{dic.Key}的值:{dic.Value}");
                }
            }
        }
private static Dictionary GetAllCell(Worksheet worksheet)
        {
            Dictionary dicResult = new Dictionary();
            IEnumerable rows = worksheet.GetFirstChild().Elements();
            if (rows.Count() == 0)
            {
                return null;
            }

            foreach(Row row in rows)
            {
                IEnumerable cells = row.Elements();
                if (cells.Count() == 0)
                {
                    continue;
                }
                else
                {
                    foreach (Cell cell in cells)
                    {
                        dicResult.Add(cell.CellReference, cell.CellValue.Text);
                    }
                }
            }
            return dicResult;
        }

 

你可能感兴趣的:(其他)