如何使用openxml读取excel文件的单元格数据

我正在使用DocumentFormat.openxml nuget pkg读取.xlsx文件。 我试图读取date单元格[G2]和数据从[b5]到[m5]

示例Excel截图附加

static void Main(string[] args) { string sFileTypeError = string.Empty; string myFilePath = @"C:\Landing\file.xlsx"; //string ext = Path.GetExtension(myFilePath); var fileName = Path.GetFileName(myFilePath); var fileExtension = Path.GetExtension(fileName); if ((fileExtension != ".xlsx") && (fileExtension != ".xls")) sFileTypeError = "Invalid file. \n\nPlease browse a correct Excel file to upload."; else { using (SpreadsheetDocument doc = SpreadsheetDocument.Open(myFilePath, false)) { IEnumerable<Sheet> sheets = doc.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().Elements<Sheet>(); WorksheetPart worksheetPart = (WorksheetPart)doc.WorkbookPart.GetPartById(sheets.First().Id.Value); IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Descendants<Row>(); } } } Please let me know how can read this. Thanks 

我想你一行一行,然后逐个单元格,你可以做到这一点:

 foreach (Row actualRow in rows) { IEnumerable<Cell> cells = actualRow.Descendants<Cell>(); foreach (Cell actualCell in cells) { // It gets the good value if it's not in the shared string table string value = actualCell.CellValue.Text; } }