欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

批量把带有批注的单元格设置黑色边框的方法

程序员文章站 2022-05-24 15:31:56
...

批量把批注的单元格设置带边框的方法
实例上图
批量把带有批注的单元格设置黑色边框的方法

  public String filePath = "C:\\Users\\Administrator\\Desktop\\abc.xlsx";
    public String filePath2 = "C:\\Users\\Administrator\\Desktop\\abc1.xlsx";
    @Test
    public void fun() throws IOException {
        FileInputStream input = new FileInputStream(filePath);
        FileOutputStream out = new FileOutputStream(filePath2);
        if (determineFileToXlsOrXlsx(filePath)){
            HSSFWorkbook workbook = new HSSFWorkbook(input);
            executer(workbook);
            // 关闭流对象
            workbook.write(out);
            workbook.close();
        }else{
            XSSFWorkbook workbook = new XSSFWorkbook(input);
            executer(workbook);
            workbook.write(out);
            workbook.close();
        }
        out.close();
        input.close();
    } public void executer(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle(); //创建一个样式
        cellStyle.setBorderBottom(BorderStyle.MEDIUM); //底部边框
        cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);//底部边框颜色

        cellStyle.setBorderLeft(BorderStyle.MEDIUM); //左边框
        cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);//左边框颜色

        cellStyle.setBorderRight(BorderStyle.MEDIUM);
        cellStyle.setRightBorderColor(HSSFColor.BLACK.index);

        cellStyle.setBorderTop(BorderStyle.MEDIUM);
        cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
            Sheet sheetAt = workbook.getSheetAt(i);
            for (int j = 0; j < sheetAt.getLastRowNum(); j++) {
                System.out.println("进入下一行");
                Row row=sheetAt.getRow(j+1)==null?sheetAt.createRow(j+1):sheetAt.getRow(j+1);
                System.out.println(row.getLastCellNum());
                for (int k = 0; k < ((row.getLastCellNum() <= 0) ? 1 :row.getLastCellNum()); k++) {//获取每个单元格
                    Cell cell = row.getCell(k);
                    if (cell!=null){
                        Comment cellComment = row.getCell(k).getCellComment();
                        if (null !=cellComment){
                            cell.setCellStyle(cellStyle);
                        }
                    }
                }
            }
            System.out.println("---Sheet表"+i+"处理完毕---");
        }
    }

运行完之后
如下图:
批量把带有批注的单元格设置黑色边框的方法

相关标签: 休闲时间 poi