Voici une classe qui permet de supprimer une colonne en précisant sa position dans une feuille Excel :
public static class SheetUtility { public static void deleteColumn(ISheet sheet, int columnToDelete) { int maxColumn = 0; for (int r = 0; r < sheet.LastRowNum + 1; r++) { IRow row = sheet.GetRow(r); if (row == null) continue; int lastColumn = row.LastCellNum; if (lastColumn > maxColumn) maxColumn = lastColumn; if (lastColumn < columnToDelete) continue; for (int x = columnToDelete + 1; x < lastColumn + 1; x++) { ICell oldCell = row.GetCell(x - 1); if (oldCell != null) row.RemoveCell(oldCell); ICell nextCell = row.GetCell(x); if (nextCell != null) { ICell newCell = row.CreateCell(x - 1, nextCell.CellType); cloneCell(newCell, nextCell); } } } for (int c = 0; c < maxColumn; c++) { sheet.SetColumnWidth(c, sheet.GetColumnWidth(c + 1)); } } private static void cloneCell(ICell cNew, ICell cOld) { cNew.CellComment = cOld.CellComment; cNew.CellStyle = cOld.CellStyle; switch (cNew.CellType) { case CellType.Boolean: { cNew.SetCellValue(cOld.BooleanCellValue); break; } case CellType.Numeric: { cNew.SetCellValue(cOld.NumericCellValue); break; } case CellType.String: { cNew.SetCellValue(cOld.StringCellValue); break; } case CellType.Error: { cNew.SetCellValue(cOld.ErrorCellValue); break; } case CellType.Formula: { cNew.SetCellValue(cOld.CellFormula); break; } } } }
Ce code est une convertion que j'ai effectué à partir du code JAVA produit par alan williamson (http://pastebin.com/ff806298)