/// <summary> /// Note this list is static - once read, it does not notice later changes to the underlying column structures /// </summary> /// <returns></returns> public List <XSSFTableColumn> GetColumns() { if (tableColumns == null) { var columns = new List <XSSFTableColumn>(); CT_TableColumns ctTableColumns = ctTable.tableColumns; if (ctTableColumns != null) { foreach (CT_TableColumn column in ctTableColumns.GetTableColumnList()) { XSSFTableColumn tableColumn = new XSSFTableColumn(this, column); columns.Add(tableColumn); } } tableColumns = columns; } return(tableColumns); }
public XSSFTableColumn CreateColumn(String columnName, int columnIndex) { int columnCount = ColumnCount; if (columnIndex < 0 || columnIndex > columnCount) { throw new ArgumentException("Column index out of bounds"); } // Ensure we have Table Columns CT_TableColumns columns = ctTable.tableColumns; if (columns == null) { columns = ctTable.AddNewTableColumns(); } // check if name is unique and calculate unique column id long nextColumnId = 0; foreach (XSSFTableColumn tableColumn in this.GetColumns()) { if (columnName != null && columnName.Equals(tableColumn.Name, StringComparison.InvariantCultureIgnoreCase)) { throw new ArgumentException("Column '" + columnName + "' already exists. Column names must be unique per table."); } nextColumnId = Math.Max(nextColumnId, tableColumn.Id); } // Bug #62740, the logic was just re-using the existing max ID, not incrementing beyond it. nextColumnId++; // Add the new Column CT_TableColumn column = columns.InsertNewTableColumn(columnIndex); columns.count = columns.count; column.id = (uint)nextColumnId; if (columnName != null) { column.name = columnName; } else { column.name = "Column " + nextColumnId; } /*if (ctTable.@ref != null) * { * // calculate new area * int newColumnCount = columnCount + 1; * CellReference tableStart = StartCellReference; * CellReference tableEnd = EndCellReference; * SpreadsheetVersion version = GetXSSFSheet().GetWorkbook().SpreadsheetVersion; * CellReference newTableEnd = new CellReference(tableEnd.Row, * tableStart.Col + newColumnCount - 1); * AreaReference newTableArea = new AreaReference(tableStart, newTableEnd, version); * * SetCellRef(newTableArea); * }*/ UpdateHeaders(); return(GetColumns()[columnIndex]); }