/** * Remove a column from the table, and shift the remaining cells to the left. * * @param columnIndex * the 0-based position of the column in the table * @throws IllegalArgumentException * if no column at the index exists or if the table has only a * single column * @since 4.0.0 */ public void removeColumn(int columnIndex) { if (columnIndex < 0 || columnIndex > getColumnCount() - 1) { throw new IllegalArgumentException("Column index out of bounds"); } if(getColumnCount() == 1) { throw new IllegalArgumentException("Table must have at least one column"); } final boolean bFixBug62759=true; if(bFixBug62759){ final CellReference from = getStartCellReference(); final CellReference to = getEndCellReference(); final int nFirstRow=from.getRow(); final int nLastRow=to.getRow(); final int nFirstCol=from.getCol(); final int nLastCol=to.getCol()-1; final int nFirstColToShift=nFirstCol+columnIndex+1; final int nLastColToShift=to.getCol(); final XSSFSheet sheet=getXSSFSheet(); CTTableColumns tableColumns = ctTable.getTableColumns(); tableColumns.removeTableColumn(columnIndex); tableColumns.setCount(tableColumns.getTableColumnList().size()); final AreaReference area = new AreaReference(new CellReference(nFirstRow,nFirstCol), new CellReference(nLastRow,nLastCol), SpreadsheetVersion.EXCEL2007); setArea(area); /* * Now shift the cells in the table to the left. This will not affect cells outside the table. * THIS DOES NOT CHANGE ANY FORMULAE!!! */ final String procName="XSSFTable.removeColumn"; System.out.println(String.format("\n%s: columnIndex=%d nFirstColToShift=%d nLastColToShift=%d", procName,columnIndex,nFirstColToShift,nLastColToShift)); if(nFirstColToShift<=nLastColToShift) { for(int nRow=nFirstRow;nRow<=nLastRow;++nRow) { final XSSFRow row=sheet.getRow(nRow); row.shiftCellsLeft(nFirstColToShift,nLastColToShift, 1); } } else { for(int nRow=nFirstRow;nRow<=nLastRow;++nRow) { final XSSFRow row=sheet.getRow(nRow); final XSSFCell cell=row.getCell(nLastColToShift); if(cell!=null) { row.removeCell(cell); } } } } else { CTTableColumns tableColumns = ctTable.getTableColumns(); tableColumns.removeTableColumn(columnIndex); tableColumns.setCount(tableColumns.getTableColumnList().size()); } updateReferences(); updateHeaders(); }