How to insert a row between two rows in an existing excel with HSSF (Apache POI)

前端 未结 8 993
自闭症患者
自闭症患者 2020-11-27 02:38

Somehow I manage to create new rows between two rows in an existing excel file. The problem is, some of the formatting were not include along the shifting of the rows.

8条回答
  •  自闭症患者
    2020-11-27 03:23

    I've implemented this in Kotlin like this:

    fun Sheet.buildRow ( rowNum:Int ) : Row {
        val templateRow = this.getRow( rowNum )
        this.shiftRows( rowNum+1, sheet.lastRowNum, 1 )
        val newRow = this.createRow( rowNum+1 )
        templateRow.cellIterator().forEach {
            newRow.createCell( it.columnIndex ).cellStyle = it.cellStyle
        }
        return templateRow
    }
    

    It doesn't copy the cell values, just the format. Should be applicable to Java as well.

提交回复
热议问题