From 74c7e4ec1958f70737dc6f795fd8f966b293f250 Mon Sep 17 00:00:00 2001 From: hmdrzsharifi Date: Mon, 15 Feb 2021 23:56:47 +0330 Subject: [PATCH] add main class --- .../poi/excel/insert/InsertRowHelper.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 apache-poi/src/main/java/com/baeldung/poi/excel/insert/InsertRowHelper.java diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/insert/InsertRowHelper.java b/apache-poi/src/main/java/com/baeldung/poi/excel/insert/InsertRowHelper.java new file mode 100644 index 0000000000..5cc4291847 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/insert/InsertRowHelper.java @@ -0,0 +1,21 @@ +package com.baeldung.poi.excel.insert; + +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.IOException; + +public class InsertRowHelper { + public Workbook insertRowBetweenRows(String fileLocation, int startRow, int rowNumber) throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + int lastRow = sheet.getLastRowNum(); + if (lastRow < startRow) { + sheet.createRow(startRow); + } + sheet.shiftRows(startRow, lastRow, rowNumber, true, true); + sheet.createRow(startRow); + return workbook; + } +}