Подтвердить что ты не робот

Apache POI xls column Удалить

Я не могу найти, как удалить столбец с API Apache POI.
Я был бы признателен за образец кода или помощь по этому вопросу.

4b9b3361

Ответ 1

Алан Уильямсон в списке рассылки написал небольшой помощник для удаления столбца

package org.alanwilliamson.openbd.plugin.spreadsheet;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;


/*
 * Helper functions to aid in the management of sheets
 */
public class SheetUtility extends Object {


    /**
     * Given a sheet, this method deletes a column from a sheet and moves
     * all the columns to the right of it to the left one cell.
     * 
     * Note, this method will not update any formula references.
     * 
     * @param sheet
     * @param column
     */
    public static void deleteColumn( Sheet sheet, int columnToDelete ){
        int maxColumn = 0;
        for ( int r=0; r < sheet.getLastRowNum()+1; r++ ){
            Row row = sheet.getRow( r );

            // if no row exists here; then nothing to do; next!
            if ( row == null )
                continue;

            // if the row doesn't have this many columns then we are good; next!
            int lastColumn = row.getLastCellNum();
            if ( lastColumn > maxColumn )
                maxColumn = lastColumn;

            if ( lastColumn < columnToDelete )
                continue;

            for ( int x=columnToDelete+1; x < lastColumn + 1; x++ ){
                Cell oldCell    = row.getCell(x-1);
                if ( oldCell != null )
                    row.removeCell( oldCell );

                Cell nextCell   = row.getCell( x );
                if ( nextCell != null ){
                    Cell newCell    = row.createCell( x-1, nextCell.getCellType() );
                    cloneCell(newCell, nextCell);
                }
            }
        }


        // Adjust the column widths
        for ( int c=0; c < maxColumn; c++ ){
            sheet.setColumnWidth( c, sheet.getColumnWidth(c+1) );
        }
    }


    /*
     * Takes an existing Cell and merges all the styles and forumla
     * into the new one
     */
    private static void cloneCell( Cell cNew, Cell cOld ){
        cNew.setCellComment( cOld.getCellComment() );
        cNew.setCellStyle( cOld.getCellStyle() );

        switch ( cNew.getCellType() ){
            case Cell.CELL_TYPE_BOOLEAN:{
                cNew.setCellValue( cOld.getBooleanCellValue() );
                break;
            }
            case Cell.CELL_TYPE_NUMERIC:{
                cNew.setCellValue( cOld.getNumericCellValue() );
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cNew.setCellValue( cOld.getStringCellValue() );
                break;
            }
            case Cell.CELL_TYPE_ERROR:{
                cNew.setCellValue( cOld.getErrorCellValue() );
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                cNew.setCellFormula( cOld.getCellFormula() );
                break;
            }
        }

    }
}

Ответ 2

Ответ cporte - отлично, но его немного сложно прочитать.


Идея:

Для каждой строки удалите ячейку, представляющую столбец, который должен быть удален, и переместите все ячейки справа от этого столбца на одну слева.


Упрощенная реализация:

//Variables for completeness
Sheet sheet;
int columnToDelete;

for (int rId = 0; rId <= sheet.getLastRowNum(); rId++) {
    Row row = sheet.getRow(rId);
    for (int cID = columnToDelete; cID < row.getLastCellNum(); cID++) {
        Cell cOld = row.getCell(cID);
        if (cOld != null) {
            row.removeCell(cOld);
        }
        Cell cNext = row.getCell(cID + 1);
        if (cNext != null) {
            Cell cNew = row.createCell(cID, cNext.getCellType());
            cloneCell(cNew, cNext);
            sheet.setColumnWidth(cID, sheet.getColumnWidth(cID + 1));
        }
    }
}


Метод клонированной ячейки скопирован из другого ответа для полноты:

private static void cloneCell( Cell cNew, Cell cOld ){
    cNew.setCellComment( cOld.getCellComment() );
    cNew.setCellStyle( cOld.getCellStyle() );

    switch ( cNew.getCellType() ){
        case Cell.CELL_TYPE_BOOLEAN:{
            cNew.setCellValue( cOld.getBooleanCellValue() );
            break;
        }
        case Cell.CELL_TYPE_NUMERIC:{
            cNew.setCellValue( cOld.getNumericCellValue() );
            break;
        }
        case Cell.CELL_TYPE_STRING:{
            cNew.setCellValue( cOld.getStringCellValue() );
            break;
        }
        case Cell.CELL_TYPE_ERROR:{
            cNew.setCellValue( cOld.getErrorCellValue() );
            break;
        }
        case Cell.CELL_TYPE_FORMULA:{
            cNew.setCellFormula( cOld.getCellFormula() );
            break;
        }
    }

}

Ответ 3

кодовое решение работало для меня как прелесть со следующими незначительными изменениями:

  • Когда мы клонируем ячейку, вызов должен быть cloneCell (cNew, cNext)
  • Мы должны установить ширину столбца только для первой строки.
  • Я использую версию 3.17 из api, поэтому несколько вещей изменились (например, CellType изменился с int на enum).

Полный код ниже (для ясности):

private void deleteColumn(Sheet sheet, int columnToDelete) {
    for (int rId = 0; rId < sheet.getLastRowNum(); rId++) {
        Row row = sheet.getRow(rId);
        for (int cID = columnToDelete; cID < row.getLastCellNum(); cID++) {
            Cell cOld = row.getCell(cID);
            if (cOld != null) {
                row.removeCell(cOld);
            }
            Cell cNext = row.getCell(cID + 1);
            if (cNext != null) {
                Cell cNew = row.createCell(cID, cNext.getCellTypeEnum());
                cloneCell(cNew, cNext);
                //Set the column width only on the first row.
                //Other wise the second row will overwrite the original column width set previously.
                if(rId == 0) {
                    sheet.setColumnWidth(cID, sheet.getColumnWidth(cID + 1));

                }
            }
        }
    }
}

private void cloneCell(Cell cNew, Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());

    if (CellType.BOOLEAN == cNew.getCellTypeEnum()) {
        cNew.setCellValue(cOld.getBooleanCellValue());
    } else if (CellType.NUMERIC == cNew.getCellTypeEnum()) {
        cNew.setCellValue(cOld.getNumericCellValue());
    } else if (CellType.STRING == cNew.getCellTypeEnum()) {
        cNew.setCellValue(cOld.getStringCellValue());
    } else if (CellType.ERROR == cNew.getCellTypeEnum()) {
        cNew.setCellValue(cOld.getErrorCellValue());
    } else if (CellType.FORMULA == cNew.getCellTypeEnum()) {
        cNew.setCellValue(cOld.getCellFormula());
    }
}

Ответ 4

Я думаю, вам нужно спуститься по каждому HSSFRow и вызвать HSSFRow.getCell, а затем HSSFRow.removeCell. API ориентирован на строки, а не на столбцы, и очень немногие операции работают на всем уровне столбцов.

Пример кода (непроверенный):

HSSFSheet sheet = ...
int colToRemove = 5;
Iterator rowIter = sheet.iterator();
while (rowIter.hasNext()) {
   HSSFRow row = (HSSFRow)rowIter.next();
   HSSFCell cell = row.getCell(colToRemove);
   row.removeCell(cell);
}