I try to set the background color of a cell according to a value. Please, find below the corresponding code.
File inputFile = new File(path)
FileInputStream fis = new FileInputStream(inputFile)
XSSFWorkbook inputWorkbook = new XSSFWorkbook(fis)
XSSFWorkbook outputWorkbook = new XSSFWorkbook()
XSSFSheet inputSheet = inputWorkbook.getSheetAt(1)
def inputSheetName = inputWorkbook.getSheetName(1)
XSSFSheet outputSheet = outputWorkbook.createSheet(inputSheetName)
// some code
CellStyle style = outputSheet.getRow(rowIndex).getCell(columnIndex).getCellStyle()
short color = (status.get(listIndex) == 'Passed') ? IndexedColors.GREEN.getIndex() : IndexedColors.RED.getIndex()
style.setFillBackgroundColor(color)
outputSheet.getRow(rowIndex).getCell(columnIndex).setCellStyle(style)
// some code
This code does not make an error, however, the cell background is not set ...
Someone can help me please? Thanks for help!
Update 1
CellStyle style = outputSheet.getRow(rowIndex).getCell(columnIndex).getCellStyle()
short color = (status.get(listIndex) == 'Passed') ? IndexedColors.GREEN.getIndex() : IndexedColors.RED.getIndex()
style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
style.setFillForegroundColor(color)
outputSheet.getRow(rowIndex).getCell(columnIndex).setCellStyle(style)
Update 2
The goal of the following code is to copy and paste content of an excel file to another one. And I want to set the color of the cell according to the status value.
def function(dateTimeOfTestSuite, status, executionDateTime) {
File inputFile = new File(path)
FileInputStream fis = new FileInputStream(inputFile)
XSSFWorkbook inputWorkbook = new XSSFWorkbook(fis)
def inputSheetCount = inputWorkbook.getNumberOfSheets()
def directoryName = 'Data Files\\Reports\\' + name
File directory = new File(directoryName)
if (!directory.exists())
directory.mkdirs()
def excelOutputFileName = directory.path + '\\' + dateTimeOfTestSuite + '.xlsx'
excelOutputFileName = excelOutputFileName.replace(':', '-')
File outputFile = new File(excelOutputFileName)
FileOutputStream fos = new FileOutputStream(outputFile)
XSSFWorkbook outputWorkbook = new XSSFWorkbook()
for(int i = 0; i < inputSheetCount; i++) {
XSSFSheet inputSheet = inputWorkbook.getSheetAt(i)
def inputSheetName = inputWorkbook.getSheetName(i)
XSSFSheet outputSheet = outputWorkbook.createSheet(inputSheetName)
def numberOfRows = inputSheet.getPhysicalNumberOfRows()
def numberOfColumns = inputSheet.getRow(0).getPhysicalNumberOfCells()
def listIndex = 0
def rowIndex = 0
outputSheet.createRow(0)
while (rowIndex < numberOfRows && inputSheet.getRow(rowIndex).getCell(0).getCellType() != Cell.CELL_TYPE_BLANK) {
def columnIndex = 0
while (columnIndex < numberOfColumns && inputSheet.getRow(0).getCell(columnIndex).getCellType() != Cell.CELL_TYPE_BLANK) {
def value = (inputSheet.getRow(rowIndex).getCell(columnIndex).toString() == 'null') ? '' : inputSheet.getRow(rowIndex).getCell(columnIndex).toString()
if (columnIndex == 0)
outputSheet.createRow(rowIndex).createCell(columnIndex).setCellValue(value)
else
outputSheet.getRow(rowIndex).createCell(columnIndex).setCellValue(value)
outputSheet.autoSizeColumn(columnIndex)
columnIndex++
}
if(inputSheetName == "input") {
if(rowIndex == 0) {
outputSheet.getRow(0).createCell(columnIndex).setCellValue('Status')
outputSheet.getRow(0).createCell(columnIndex + 1).setCellValue('ExecutedAt')
}
else if (rowIndex > 1) {
outputSheet.getRow(rowIndex).createCell(columnIndex).setCellValue(status.get(listIndex))
if(status.get(listIndex) != 'No run') {
outputSheet.getRow(rowIndex).createCell(columnIndex + 1).setCellValue(executionDateTime.get(listIndex))
CellStyle style = outputSheet.getRow(rowIndex).getCell(columnIndex).getCellStyle()
def color = (status.get(listIndex) == 'Passed') ? IndexedColors.GREEN.getIndex() : IndexedColors.RED.getIndex()
style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
style.setFillForegroundColor(color)
outputSheet.getRow(rowIndex).getCell(columnIndex).setCellStyle(style)
}
listIndex++
}
outputSheet.autoSizeColumn(columnIndex)
outputSheet.autoSizeColumn(columnIndex + 1)
}
rowIndex++
}
}
outputWorkbook.write(fos)
fos.close()
}
Solution
XSSFCellStyle style = outputWorkbook.createCellStyle()
def color = (status.get(listIndex) == 'Passed') ? IndexedColors.GREEN.getIndex() : IndexedColors.RED.getIndex()
style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
style.setFillForegroundColor(color)
outputSheet.getRow(rowIndex).getCell(columnIndex).setCellStyle(style)