如何在java xssf poi中移动列

如何将现有列数据和格式复制到Apache POI中的下一列,并将下一列移到右侧。

我试过这个。 说我的代码是这个…

XSSFCell oldCell = worksheet.getRow(0).getCell(1); XSSFCell newCell = worksheet.getRow(0).getCell(2); if(styleMap != null) { if(oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()){ newCell.setCellStyle(oldCell.getCellStyle()); } else{ int stHashCode = oldCell.getCellStyle().hashCode(); XSSFCellStyle newCellStyle = styleMap.get(stHashCode); if(newCellStyle == null){ newCellStyle = newCell.getSheet().getWorkbook().createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); styleMap.put(stHashCode, newCellStyle); } newCell.setCellStyle(newCellStyle); } } 

我能够将旧的单元格的值复制到新的单元格,但不会将现有的列移到右侧。

在此先感谢您的帮助。

我好几年没有使用POI,但是如果我没有记错的话,你必须遍历一行中的所有单元格,并更新每个Cell的列号,使其成为你想要的。 没有神奇的“插入列”方法。 记得从右到左这样做,以避免完全废除工作表:-)

我不能相信它不在API中。

你可以使用这两个方便的function。 我会尽力让公关到后来的Apache POI。

 void shiftColumns(Row row, int startingIndex, int shiftCount) { for (int i = row.getPhysicalNumberOfCells()-1;i>=startingIndex;i--){ Cell oldCell = row.getCell(i); Cell newCell = row.createCell(i + shiftCount, oldCell.getCellTypeEnum()); cloneCellValue(oldCell,newCell); } } void cloneCellValue(Cell oldCell, Cell newCell) { //TODO test it switch (oldCell.getCellTypeEnum()) { case STRING: newCell.setCellValue(oldCell.getStringCellValue()); break; case NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; case ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); case BLANK: case _NONE: break; } }