• POI导出excel文件之取消合并单元格、删除列、移动列


    1. 删除列

             场景:将源文件中的某列的值、批准删除

    1. /**
    2. * 删除列
    3. *
    4. * @param sheet
    5. * @param columnToDelete 要删除的列号
    6. */
    7. public static void deleteColumn(Sheet sheet, int columnToDelete) {
    8. for (int rId = 0; rId <= sheet.getLastRowNum(); rId++) {
    9. Row row = sheet.getRow(rId);
    10. if(row==null){
    11. continue;
    12. }
    13. for (int cID = columnToDelete; cID <= row.getLastCellNum(); cID++) {
    14. Cell cOld = row.getCell(cID);
    15. if (cOld != null) {
    16. //删除批注
    17. cOld.removeCellComment();
    18. row.removeCell(cOld);
    19. }
    20. Cell cNext = row.getCell(row.getLastCellNum() + 1);
    21. if (cNext != null) {
    22. Cell cNew = row.createCell(cID, cNext.getCellType());
    23. cloneCell(cNew, cNext);
    24. //Set the column width only on the first row.
    25. //Other wise the second row will overwrite the original column width set previously.
    26. if (rId == 0) {
    27. sheet.setColumnWidth(cID, sheet.getColumnWidth(cID + 1));
    28. }
    29. }
    30. }
    31. }
    32. }

    2.取消合并单元格

     场景:取消某区域的合并单元格

    1. /**
    2. * 取消多个合并单元格
    3. *
    4. * @param sheet
    5. * @param startRow 开始行号
    6. * @param endRow 结束行号
    7. * @param startColumn 开始列号
    8. * @param endColumn 结束列号
    9. */
    10. public static void removeMerged(Sheet sheet, Integer startRow, Integer endRow, Integer startColumn, Integer endColumn) {
    11. if(startRow==null){
    12. startRow= sheet.getFirstRowNum();
    13. }
    14. if(endRow==null){
    15. endRow= sheet.getLastRowNum();
    16. }
    17. //获取所有的单元格
    18. int sheetMergeCount = sheet.getNumMergedRegions();
    19. //用于保存要移除的那个合并单元格序号
    20. List indexList = new ArrayList<>();
    21. for (int i = 0; i < sheetMergeCount; i++) {
    22. //获取第i个单元格
    23. CellRangeAddress ca = sheet.getMergedRegion(i);
    24. int firstColumn = ca.getFirstColumn();
    25. int lastColumn = ca.getLastColumn();
    26. int firstRow = ca.getFirstRow();
    27. int lastRow = ca.getLastRow();
    28. if (startRow <= firstRow && endRow >= lastRow && startColumn <= firstColumn && endColumn >= lastColumn) {
    29. indexList.add(i);
    30. }
    31. }
    32. sheet.removeMergedRegions(indexList);
    33. }

    3.移动列

     场景:将某列值移动至 空列上

    1. private static void cloneCell(Cell cNew, Cell cOld) {
    2. cNew.setCellComment(cNew.getCellComment());
    3. cNew.setCellStyle(cNew.getCellStyle());
    4. if (CellType.BOOLEAN == cNew.getCellType()) {
    5. cNew.setCellValue(cOld.getBooleanCellValue());
    6. } else if (CellType.NUMERIC == cNew.getCellType()) {
    7. cNew.setCellValue(cOld.getNumericCellValue());
    8. } else if (CellType.STRING == cNew.getCellType()) {
    9. cNew.setCellValue(cOld.getStringCellValue());
    10. } else if (CellType.ERROR == cNew.getCellType()) {
    11. cNew.setCellValue(cOld.getErrorCellValue());
    12. } else if (CellType.FORMULA == cNew.getCellType()) {
    13. cNew.setCellValue(cOld.getCellFormula());
    14. }
    15. }

  • 相关阅读:
    cesium在地形上贴地添加各种entity
    设计模式- 建造者模式(Builder Pattern)结构|原理|优缺点|场景|示例
    船用低速发动机缸压在线监测系统
    CSS 滚动驱动动画 scroll()
    以K近邻算法为例,使用网格搜索GridSearchCV优化模型最佳参数
    Kotlin委托
    惠普打印机驱动安装
    武汉新时标文化传媒有限公司企媒体营销与SaaS系统的加持
    AI新时代,新华三存储新思考
    总交易量突破 3000 亿美元,APX Finance 成本轮牛市最大的黑马?
  • 原文地址:https://blog.csdn.net/kelly921011/article/details/126767809