internal DataTable GetSchema(string fileName) { int i = 0; DataTable dataTable = new DataTable(); using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; foreach (WorksheetPart worksheetPart in workbookPart.WorksheetParts) { OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);// ( while (reader.Read()) { if (reader.ElementType == typeof(Row))///loops through row { reader.ReadFirstChild(); if (i > 1) { return dataTable; // we have recieved all column names till here } i++; //increments for the first time which means we are reading columns do { if (reader.ElementType == typeof(Cell)) { Cell c = (Cell)reader.LoadCurrentElement(); string cellValue = string.Empty; if (c.CellValue != null) { if (c.DataType != null && c.DataType == CellValues.SharedString) { SharedStringItem ssi = workbookPart.SharedStringTablePart.SharedStringTable.Elements<sharedstringitem>().ElementAt(int.Parse(c.CellValue.InnerText)); cellValue = Convert.ToString(ssi.InnerText); } else { cellValue = Convert.ToString(c.CellValue.InnerText); } } else { cellValue = c.InnerText; } if (i <= 1) { dataTable.Columns.Add(cellValue); } else { break; } } } while (reader.ReadNextSibling()); } } } return dataTable; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)