Apache POI xls column Remove

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

I don't find how to remove a column with the Apache POI API.
I would appreciate a sample code or help on this point.

回答1:

Alan Williamson on the mailing list wrote a small helper for column removal

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  maxColumn )                 maxColumn = lastColumn;              if ( lastColumn 


回答2:

The answer of cporte is perfectly fine but imho a bit hard to read.


The Idea:

For every row, delete the cell representing the column which shall be deleted and move all cells to the right of this column one to the left.


The simplified Implementation:

//Variables for completeness Sheet sheet; int columnToDelete;  for (int rId = 0; rId 


The clone cell method copied from the other answer for completeness:
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:

I think you have to go down each HSSFRow and call HSSFRow.getCell and then HSSFRow.removeCell. The API is oriented towards rows, rather than columns, and very few operations work at the whole column level.

Sample code (untested):

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); } 


回答4:

codewing's solution worked for me like a charm with the following minor changes:

  1. When we clone the cell, the call should be cloneCell(cNew, cNext)
  2. We should set the column width only for the first row.
  3. I'm using version 3.17 of the api, so a few things changed (like CellType changed from int to an enum).

Full code is below (for clarity):

private void deleteColumn(Sheet sheet, int columnToDelete) {     for (int rId = 0; rId 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!