private void trackMaxChars(int columnIndex, SpreadsheetCell cell, bool isTableHeaderRow = false) { if (!cell.ParticipatesInAutoWidthColumnCalculation) { return; } var previousMax = 0; if (_maxNumberOfCharsPerColumn.ContainsKey(columnIndex)) { previousMax = _maxNumberOfCharsPerColumn[columnIndex]; } var charsCount = (cell.Value?.ToString().Split('\n').Max(x => x.Length) ?? 0) + cell.Indent; if (isTableHeaderRow) { charsCount += 4; } if (previousMax < charsCount) { _maxNumberOfCharsPerColumn[columnIndex] = charsCount; } }
public void AddTable(SpreadsheetTable table, int columnIndex, int rowIndex, HeaderCellStyle headerCellStyle = null) { if (_addAdditionalItemsDisabled) { throw new InvalidOperationException("Additional elements addition is disabled"); } _tables[new SpreadsheetLocation(rowIndex, columnIndex)] = table; var headerRow = new SpreadsheetRow(); for (var i = 0; i < table.Columns.Count; i++) { var column = table.Columns[i]; var headerCell = new SpreadsheetCell(); headerCell.Value = column.Name; if (headerCellStyle != null) { headerCell.BackgroundColor = headerCellStyle.BackgroundColor; headerCell.ForegroundColor = headerCellStyle.ForegroundColor; headerCell.Font = headerCellStyle.Font; } headerRow.AddCell(headerCell); trackMaxChars(columnIndex + i, headerCell); } if (table.ShowHeaderRow) { addRow(headerRow, columnIndex, rowIndex, true); rowIndex++; } if (!table.IsInStreamingMode) { foreach (var row in table.Rows) { AddRow(row, columnIndex, rowIndex); rowIndex++; } } else { var enumerator = table.GetStreamingEnumerator(); // we have to add some rows to calculate column widths(we dont use the excel feature because it's slow when dealing with large amounts of data) // the rest of the rows will be stream directyl to the openxmlwriter // MaxRowWidthsToTrackPerTable rows is not so much that it would cause an memory issue, the rest will be written one by one var rowsToGet = MaxRowWidthsToTrackPerTable; var endOfTableIndex = rowIndex; while (rowsToGet > 0 && enumerator.MoveNext()) { AddRow(enumerator.Current, columnIndex, rowIndex); rowsToGet--; rowIndex++; endOfTableIndex++; } // if we have more items to stream, disable manually adding rows after the table _addAdditionalItemsDisabled = enumerator.Current != null; if (endOfTableIndex > _maxRowIndex) { _maxRowIndex = endOfTableIndex; } } }
public void AddCell(SpreadsheetCell cell) { RowCells.Add(cell); }