如何在excel中为单元格设置值和颜色

时间:2018-02-08 10:15:48

标签: java apache-poi

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);
}

当我执行此代码时,我的excel只更新颜色,例如红色表示失败,绿色表示通过。

现在我的问题是我要在我的单元格中打印状态(失败或通过)和颜色,我应该怎么做?

1 个答案:

答案 0 :(得分:1)

这个问题缺乏对Selenium是什么的基本理解。 Selenium或Selenium Webdriver是一个浏览器自动化库,可以与不同的编程语言一起使用。 Selenium包含用于操作Web浏览器的方法。它本身并不是一种编程语言。

要操作Excel文件,您需要查找与Selenium一起使用的编程语言的信息。从示例代码我猜可能是Java?然后,您需要查找如何使用Java写入Excel文件。

对于C#,我更喜欢ByteScout。

对于Java,这里是StackOverflow上的一个主题:writing to excel in java