using (SpreadsheetDocument xl = SpreadsheetDocument.Open(filepath, true)) { WorkbookPart wbp = xl.WorkbookPart; foreach (Sheet isheet in wbp.Workbook.Sheets) { string relationshipId = isheet.Id.Value; WorksheetPart worksheetPart = (WorksheetPart)xl.WorkbookPart.GetPartById(relationshipId); SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First(); IEnumerable<Row> rows = sheetData.Elements<Row>(); for (int irow = 1; irow <= rows.Count(); irow++) { try { IEnumerable<Cell> cells = rows.ElementAt(irow).Elements<Cell>(); for (int Ccount = 0; Ccount < cells.Count(); Ccount++) { string cellIndex = GetCellIndex(cells.ElementAt(Ccount)); WorksheetPart wsPart = (WorksheetPart)(wbp.GetPartById(isheet.Id)); Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == cellIndex).FirstOrDefault(); if (theCell != null) { value = theCell.InnerText; if (theCell.DataType != null) { switch (theCell.DataType.Value) { case CellValues.SharedString: var stringTable = wbp.GetPartsOfType<SharedStringTablePart>().FirstOrDefault(); if (stringTable != null) value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText; break; case CellValues.Date: { int i = 0; string s = value; bool result = int.TryParse(s, out i); if (result) value = JulianToDateTime(Convert.ToInt32(value)).ToShortDateString(); else value = ""; } break; case CellValues.Boolean: switch (value) { case "0": value = "FALSE"; break; default: value = "TRUE"; break; } break; } } } } } catch (ArgumentOutOfRangeException argex) { break; } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)