private ExpandoObject ParseFromRow(Row row, ISheetDefinition sheetDefinition) { IDictionary<string, object> expando = new ExpandoObject(); var cells = row.Elements<Cell>().ToList(); var colDefs = sheetDefinition.ColumnDefinitions.ToList(); var count = Math.Max(cells.Count, colDefs.Count); for (var index = 0; index < count; index++) { var colDef = index < colDefs.Count ? colDefs[index] : null; var cell = index < cells.Count ? cells[index] : null; string propName; object propValue; if (cell != null) { propName = cell.GetMdsolAttribute("propertyName"); propValue = _converterManager.GetCSharpValue(cell); } else if (colDef != null) { propName = colDef.PropertyName; propValue = null; } else { throw new NotSupportedException("Cell and CellDefinition are both null"); } expando.Add(propName, propValue); } return (ExpandoObject) expando; }
public IEnumerable<ExpandoObject> GetObjects(Worksheet worksheet, ISheetDefinition sheetDefinition) { if (worksheet == null) throw new ArgumentNullException("worksheet"); if (sheetDefinition == null) throw new ArgumentNullException("sheetDefinition"); var rows = worksheet.Descendants<Row>().Skip(1); return rows.Select(x => ParseFromRow(x, sheetDefinition)); }
private Worksheet CreateWorksheet(IEnumerable<SheetModel> models, ISheetDefinition sheetDefinition) { var sheetData = CreateSheetData(models, sheetDefinition); var worksheet = new Worksheet(sheetData); worksheet.AddMdsolNamespaceDeclaration(); return worksheet; }
private void BuildSheetFunc(IEnumerable<SheetModel> models, ISheetDefinition sheetDefinition, SpreadsheetDocument doc) { if (doc == null) throw new ArgumentNullException("doc"); var sheets = doc.WorkbookPart.Workbook.Sheets ?? doc.WorkbookPart.Workbook.AppendChild(new Sheets()); var worksheetPart = doc.WorkbookPart.AddNewPart<WorksheetPart>(); worksheetPart.Worksheet = CreateWorksheet(models, sheetDefinition); var sheetId = 1 + sheets.Count(); var sheetName = sheetDefinition.Name; var sheet = new Sheet { Id = doc.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = (uint) sheetId, Name = sheetName }; sheets.Append(sheet); }
private Row BuildRowFromExpandoObject(SheetModel model, ISheetDefinition sheetDefinition) { var row = new Row(); foreach (var columnDefinition in sheetDefinition.ColumnDefinitions) { var propValue = GetPropertyValue(model, columnDefinition.PropertyName); CellValues cellType; string cellValue; _converterManager.GetCellTypeAndValue(propValue, out cellType, out cellValue); var cell = new Cell { DataType = cellType, CellValue = new CellValue(cellValue) }; cell.AddMdsolAttribute("type", propValue == null ? typeof(object).FullName : propValue.GetType().FullName); cell.AddMdsolAttribute("propertyName", columnDefinition.PropertyName); row.AppendChild(cell); } return row; }
private SheetData CreateSheetData(IEnumerable<SheetModel> models, ISheetDefinition sheetDefinition) { var sheetData = new SheetData(); var rows = models.Select(x => BuildRow(x, sheetDefinition)); sheetData.Append(rows); return sheetData; }