private void Put <T>(ISheet sheet, IEnumerable <T> objects, bool overwrite) { var sheetName = sheet.SheetName; var firstRow = sheet.GetRow(sheet.FirstRowNum); var objectArray = objects as T[] ?? objects.ToArray(); var type = MapHelper.GetConcreteType(objectArray); var columns = GetTrackedColumns(sheetName, type) ?? GetColumns(firstRow ?? PopulateFirstRow(sheet, null, type), type); if (firstRow == null) { PopulateFirstRow(sheet, columns, type); } var rowIndex = overwrite ? HasHeader ? sheet.FirstRowNum + 1 : sheet.FirstRowNum : sheet.GetRow(sheet.LastRowNum) != null ? sheet.LastRowNum + 1 : sheet.LastRowNum; MapHelper.EnsureDefaultFormats(columns, TypeFormats); foreach (var o in objectArray) { var row = sheet.GetRow(rowIndex); if (overwrite && row != null) { sheet.RemoveRow(row); row = sheet.CreateRow(rowIndex); } row = row ?? sheet.CreateRow(rowIndex); foreach (var column in columns) { var pi = column.Attribute.Property; var value = pi?.GetValue(o); var cell = row.GetCell(column.Attribute.Index, MissingCellPolicy.CREATE_NULL_AS_BLANK); column.CurrentValue = value; if (column.Attribute.TryPut == null || column.Attribute.TryPut(column, o)) { SetCell(cell, column.CurrentValue, column, setStyle: overwrite); } } rowIndex++; } // Remove not used rows if any. while (overwrite && rowIndex <= sheet.LastRowNum) { var row = sheet.GetRow(rowIndex); if (row != null) { sheet.RemoveRow(row); } rowIndex++; } }
private void Put <T>(ISheet sheet, IEnumerable <T> objects, bool overwrite, int rowOffset = 0, bool shiftDownExistingRows = false) { var sheetName = sheet.SheetName; var firstRow = sheet.GetRow(sheet.FirstRowNum); var objectArray = objects as T[] ?? objects.ToArray(); var type = MapHelper.GetConcreteType(objectArray); var columns = GetTrackedColumns(sheetName, type) ?? GetColumns(firstRow ?? PopulateFirstRow(sheet, null, type), type); firstRow = sheet.GetRow(sheet.FirstRowNum) ?? PopulateFirstRow(sheet, columns, type); var rowIndex = overwrite ? HasHeader ? sheet.FirstRowNum + 1 : sheet.FirstRowNum : sheet.GetRow(sheet.LastRowNum) != null ? sheet.LastRowNum + 1 : sheet.LastRowNum; rowIndex += rowOffset; ICellStyle baseRowCellStyle = null; if (shiftDownExistingRows) { var cell = sheet.GetRow(rowIndex)?.GetCell(0); if (cell != null) { baseRowCellStyle = Workbook.CreateCellStyle(); baseRowCellStyle.CloneStyleFrom(cell.CellStyle); } sheet.ShiftRows(rowIndex, sheet.LastRowNum, objectArray.Length, true, false); } MapHelper.EnsureDefaultFormats(columns, TypeFormats); foreach (var o in objectArray) { var row = sheet.GetRow(rowIndex); if (overwrite && !shiftDownExistingRows && row != null) { sheet.RemoveRow(row); row = sheet.CreateRow(rowIndex); } row = row ?? sheet.CreateRow(rowIndex); foreach (var column in columns) { var pi = column.Attribute.Property; var value = pi?.GetValue(o); var cell = row.GetCell(column.Attribute.Index, MissingCellPolicy.CREATE_NULL_AS_BLANK); column.CurrentValue = value; if (column.Attribute.TryPut == null || column.Attribute.TryPut(column, o)) { SetCell(cell, column.CurrentValue, column, setStyle: overwrite); if (baseRowCellStyle != null) { var cellStyle = Workbook.CreateCellStyle(); cellStyle.CloneStyleFrom(baseRowCellStyle); cellStyle.DataFormat = cell.CellStyle.DataFormat; cell.CellStyle = cellStyle; } } } rowIndex++; } // Remove not used rows if any. while (overwrite && !shiftDownExistingRows && rowIndex <= sheet.LastRowNum) { var row = sheet.GetRow(rowIndex); if (row != null) { sheet.RemoveRow(row); } rowIndex++; } // Injects custom action for headers. if (overwrite && HasHeader && _headerAction != null) { firstRow?.Cells.ForEach(c => _headerAction(c)); } }
private void Put<T>(ISheet sheet, IEnumerable<T> objects, bool overwrite, Action<Dictionary<PropertyInfo, ColumnAttribute>> order =null ) { var sheetName = sheet.SheetName; var firstRowIndex = GetFirstRowIndex(sheet); var firstRow = sheet.GetRow(firstRowIndex); var objectArray = objects as T[] ?? objects.ToArray(); var type = MapHelper.GetConcreteType(objectArray); var columns = GetTrackedColumns(sheetName, type) ?? GetColumns(firstRow ?? PopulateFirstRow(sheet, null, type,order), type); firstRow = sheet.GetRow(firstRowIndex) ?? PopulateFirstRow(sheet, columns, type,order); var rowIndex = overwrite ? HasHeader ? firstRowIndex + 1 : firstRowIndex : sheet.GetRow(sheet.LastRowNum) != null ? sheet.LastRowNum + 1 : sheet.LastRowNum; MapHelper.EnsureDefaultFormats(columns, TypeFormats); foreach (var o in objectArray) { var row = sheet.GetRow(rowIndex); if (overwrite && row != null) { //sheet.RemoveRow(row); //row = sheet.CreateRow(rowIndex); row.Cells.Clear(); } row = row ?? sheet.CreateRow(rowIndex); foreach (var column in columns) { var pi = column.Attribute.Property; var value = pi?.GetValue(o, null); var cell = row.GetCell(column.Attribute.Index, MissingCellPolicy.CREATE_NULL_AS_BLANK); column.CurrentValue = value; if (column.Attribute.TryPut == null || column.Attribute.TryPut(column, o)) { SetCell(cell, column.CurrentValue, column, setStyle: overwrite); } } rowIndex++; } // Remove not used rows if any. while (overwrite && rowIndex <= sheet.LastRowNum) { var row = sheet.GetRow(rowIndex); if (row != null) { //sheet.RemoveRow(row); row.Cells.Clear(); } rowIndex++; } // Injects custom action for headers. if (overwrite && HasHeader && _headerAction != null) { firstRow?.Cells.ForEach(c => _headerAction(c)); } }