IEnumerable <Row> getRows(SheetData sheet) { Row tmp = sheet.GetFirstChild <Row>(); yield return(tmp); while (tmp != null) { tmp = tmp.NextSibling <Row>(); yield return(tmp); } }
public Tournament ImportFromExcel(string fileName, bool unicode = true) { Tournament result = null; var table = new DataTable(); using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(fileName, false)) { WorkbookPart workbook = spreadsheet.WorkbookPart; //create a reference to Sheet1 WorksheetPart worksheet = workbook.WorksheetParts.Last(); SheetData data = worksheet.Worksheet.GetFirstChild <SheetData>(); //add column names to the first row Row header = data.GetFirstChild <Row>(); var rowEnum = header.GetEnumerator(); while (rowEnum.MoveNext()) { table.Columns.Add(new DataColumn(rowEnum.Current.InnerText)); } Row row = header.NextSibling() as Row; do { if (row != null) { var dataRow = table.Rows.Add(); rowEnum = row.GetEnumerator(); int i = 0; while (rowEnum.MoveNext()) { dataRow[i] = rowEnum.Current.InnerText; i++; } row = row.NextSibling() as Row; } }while (row != null); } StringBuilder sb = new StringBuilder(); foreach (DataColumn item in table.Columns) { if (item != table.Columns[0]) { sb.Append(' '); } sb.Append(item.Caption); } sb.Append("\n"); foreach (DataRow item in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { if (i != 0) { sb.Append(' '); } sb.Append(item.ItemArray[i]); } sb.Append('\n'); } bool unicodeError; result = ImportFromTextLines(sb.ToString().Split('\n'), out unicodeError); return(result); }