Writing Excel file keep on corrupted in for loop

Hi i am trying to write some data into excel file but it on corrupt my file .

My Code:
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.keyword.excel.ExcelKeywords
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.lang.String
import org.openqa.selenium.interactions.Actions;

String accexcel=‘C:\user account validation\test4.xlsx’

workbook1 = ExcelKeywords.getWorkbook(accexcel)

sheet = ExcelKeywords.getExcelSheet(workbook1, “Sheet1”)

for (def rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++)
{
def a = 0
if (a==‘0’)
{
WebUI.comment(‘value is’ + rowNum)
ExcelKeywords.setValueToCellByIndex(sheet, rowNum, 3, ‘successfull’)
ExcelKeywords.saveWorkbook(accexcel , workbook1)
WebUI.comment(‘URL is :’+ accexcel)
WebUI.delay(10)
}

else
{
	WebUI.comment('value is else' + rowNum)
	ExcelKeywords.setValueToCellByIndex(sheet, rowNum, 3, 'successfull')
	ExcelKeywords.saveWorkbook(accexcel , workbook1)
	WebUI.comment('URL is else:'+ accexcel)
	WebUI.delay(10)
}

}

Error Log:

Test Cases/write FAILED.
Reason:
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
at org.apache.xmlbeans.impl.values.XmlObjectBase.newCursor(XmlObjectBase.java:286)
at org.apache.xmlbeans.impl.values.XmlComplexContentImpl.arraySetterHelper(XmlComplexContentImpl.java:1124)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontsImpl.setFontArray(Unknown Source)
at org.apache.poi.xssf.model.StylesTable.writeTo(StylesTable.java:385)
at org.apache.poi.xssf.model.StylesTable.commit(StylesTable.java:443)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:313)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:317)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:173)
at com.kms.katalon.keyword.excel.ExcelKeywords.saveWorkbook(ExcelKeywords.groovy:142)
at com.kms.katalon.keyword.excel.ExcelKeywords$saveWorkbook$2.call(Unknown Source)
at write.run(write:50)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1564133022220.run(TempTestCase1564133022220.groovy:21)

Excel Error:

Please help me to solve this issue

Getting same issue while writing excel in loop , however able to write excel in normal case

Can anybody provide solution on this issue

As the POI document clearly describes, the row number is 0-based.
https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/Sheet.html#getRow-int-

But the following line of your code indicates that you assume 1-based row number.

I put the part where you access the excel file into the loop and it seems to be working.

e.g.
put these lines into the loop part:
“String accexcel=‘C:\user account validation\test4.xlsx’
workbook1 = ExcelKeywords.getWorkbook(accexcel)
sheet = ExcelKeywords.getExcelSheet(workbook1, “Sheet1”)”