-1
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

CellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(IndexedColors.RED.getIndex());
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);

String str = strText.substring(0, (strText.indexOf("k")-1));
//sheet1.getRow(i).createCell(8).setCellValue(str);
String strx = strText1.substring(0, (strText1.indexOf("k")-1));
//sheet1.getRow(i).createCell(9).setCellValue(strx);

double d = Double.parseDouble(str);
double d1 = Double.parseDouble(strx);

if(d>d1) {
    double no = (d1/d) + (d1 * (1/100));
    if(d>(d1+no)) {
        sheet1.getRow(i).createCell(7).setCellValue("Fail");
        sheet1.getRow(i).createCell(7).setCellStyle(style1);
    }
}
else {
    sheet1.getRow(i).createCell(7).setCellValue("Pass");
    sheet1.getRow(i).createCell(7).setCellStyle(style);
}

if(d1>d) {
    double nos = (d/d1) + (d * (1/100));
    if(d1>(d+nos)) {
        sheet1.getRow(i).createCell(7).setCellValue("Fail");
        sheet1.getRow(i).createCell(7).setCellStyle(style1);
    }
}

else {
    sheet1.getRow(i).createCell(7).setCellValue("Pass");
    sheet1.getRow(i).createCell(7).setCellStyle(style);
}

When I execute this code my excel is updating only with the colors such as fail indicated by red and pass indicated by green.

Now my problem is I want to print the status (fail or pass) and color both in my cell, how should I do it?

baudsp
  • 4,076
  • 1
  • 17
  • 35
shivam
  • 249
  • 1
  • 5
  • 18
  • From the wording, what are you thinking `sheet1.getRow(i).createCell(7)` is doing? Right, it **creates** a new cell 7 in the row. So after `sheet1.getRow(i).createCell(7).setCellValue("Fail");` there is a new cell 7 having the content "Fail". But after `sheet1.getRow(i).createCell(7).setCellStyle(style1);`there is a new new cell 7 having the style. Do `sheet1.getRow(i).createCell(7).setCellValue("Fail"); sheet1.getRow(i).getCell(7).setCellStyle(style1);`. So **creating** the cell and setting the value and the **getting** the same cell and setting the style. – Axel Richter Feb 08 '18 at 13:46

1 Answers1

1

This question lacks basic understanding about what Selenium is. Selenium, or Selenium Webdriver, is a browser automation library which can be used with different programming languages. Selenium contains methods for operating a web browser. It's not a programming language in itself.

To manipulate Excel files, you need to look up information for the programming language you're using with Selenium. From the example code I'm guessing maybe Java? Then you need to look up how to write to Excel files with Java.

For C# I prefer ByteScout.

For Java, here's a thread here on StackOverflow: writing to excel in java

Frank H.
  • 1
  • 1
  • 11
  • 25