poi 新增一行并拷贝上一行的样式

    /**
     * 新增一行数据并拷贝上一行的样式
     *
     * @param sheet
     * @param startRow 开始新增行数
     * @param cellNum 增加的列数
     * @return
     */
    private void insertRow(HSSFSheet sheet, int startRow, int cellNum) {
        sheet.shiftRows(startRow, sheet.getLastRowNum(), 1, true, false);
        //获取当前行
        HSSFRow rowSource = sheet.getRow(startRow - 1);
        //获取当前行样式
        HSSFCellStyle rowStyle = rowSource.getRowStyle();
        //新增行
        HSSFRow rowInsert = sheet.createRow(startRow);
        if (rowStyle != null) {
            rowInsert.setRowStyle(rowStyle);
            rowInsert.setHeight(rowSource.getHeight());
        }
        for (int col = 0; col < cellNum; col++) {
            HSSFCell cellSource = rowSource.getCell(col);
            HSSFCell cellInsert = rowInsert.createCell(col);
            HSSFCellStyle cellStyle = cellSource.getCellStyle();
            //设置单元格样式    
            if (cellStyle != null) {
                cellInsert.setCellStyle(cellStyle);
            }
        }
    }

 

posted @ 2021-03-02 10:18  卡农的忧伤ろ◆  阅读(2923)  评论(0)    收藏  举报