微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Apache POI xls列删除

我找不到如何使用Apache POI API删除列。

我将不胜感激示例代码或帮助在这一点上。

502坏的网关和codeigniter / Nginx / apache。 代码或服务器的问题?

Apache:我如何将foo.htmlredirect到foo并将foo重写为foo.html?

www-data下的apache2多个实例

未定义的索引:在Laravel迁移时REMOTE_ADDR

public_html中的.htaccess返回404页面

邮件列表中的Alan Williamson写了一个小帮手来删除

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

cporte的答案非常好,但是imho有点难以阅读。

理念:

对于每一行,删除代表将被删除的列的单元格,并将该列右侧的所有单元格向左移一位。

简化的实施:

//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(cNext,cNew); 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; } } }

我认为你必须去每个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); }

codewing的解决方案为我工作像一个魅力与以下小的变化:

当我们克隆单元格时,调用应该是cloneCell(cNew,cNext)

我们应该只为第一行设置列宽。

我使用的版本3.17的api,所以有一些改变(像CellType从int更改为一个枚举)。

完整的代码在下面(为了清楚):

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐