Java 正面粗体权重\u粗体无法解析或不是字段,实心\u前景无法解析或不是字段

Java 正面粗体权重\u粗体无法解析或不是字段,实心\u前景无法解析或不是字段,java,selenium,Java,Selenium,我面对的是BOLDWEIGHT\u BOLD无法解析或不是字段,而SOLID\u前台无法解析或不是字段 我的原始Generic.java文件: package com.expertus.libraries; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; im

我面对的是BOLDWEIGHT\u BOLD无法解析或不是字段,而SOLID\u前台无法解析或不是字段

我的原始Generic.java文件:

package com.expertus.libraries;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Set;

//import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.*;

public class Generic {
    public static int getRowCount(String xlPath, String sheetName) {
        try {
            FileInputStream fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            return wb.getSheet(sheetName).getLastRowNum();
        } catch (Exception e) {

            return -1;
        }

    }

    public static String getCellValue(String xlPath, String sheetName,
            int rowNum, int cellNum) {
        FileInputStream fis;
        try 
        {
            fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            return wb.getSheet(sheetName).getRow(rowNum).getCell(cellNum).getStringCellValue();
        } 
        catch (Exception e) 
        {
            return "";
        }
    }

    public static int getColumnCount(String xlPath, String sheetName, int rowNum) {
        FileInputStream fis;
        try 
        {
            fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            return wb.getSheet(sheetName).getRow(rowNum).getLastCellNum();
        } 
        catch (Exception e) 
        {
            return -1;
        }
    }

    public static void setCellValue(String xlPath, String sheetName,
            int rowNum, int cellNum, String cellVal) {
        FileInputStream fis;
        FileOutputStream fos;
        try {
            fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            Row r = wb.getSheet(sheetName).getRow(rowNum);
            if (r == null)
                r = wb.getSheet(sheetName).createRow(rowNum);
            Cell c = r.getCell(cellNum);
            if (c == null)
                c = r.createCell(cellNum);
            c.setCellValue(cellVal);
            fis.close();
            fos = new FileOutputStream(xlPath);
            wb.write(fos);
            fos.close();
        } 
        catch (Exception e) 
        {

        }

    }

    public static void writeRes(String fisPath, HashMap hm) 
    {
        FileInputStream fis;
        FileOutputStream fos;
        try 
        {
            fis = new FileInputStream(fisPath);
            Workbook wb = WorkbookFactory.create(fis);
            CellStyle style = wb.createCellStyle();

            Sheet mainSheet = wb.getSheet("Main");
            int rc = mainSheet.getLastRowNum();
            for (int i = 1; i <= rc; i++) {
                String module = mainSheet.getRow(i).getCell(0)
                        .getStringCellValue();
                Sheet testCaseSheet = wb.getSheet(module);
                int rowCnt = testCaseSheet.getLastRowNum();
                for (int j = 1; j <= rowCnt; j++) {
                    String testcase = testCaseSheet.getRow(j).getCell(0)
                            .getStringCellValue();
                    Set<String> key = hm.keySet();
                    for (String k : key) {
                        if (testcase.equalsIgnoreCase(k)) {
                            Cell c = testCaseSheet.getRow(j).getCell(2);
                            if (c == null)
                                c = testCaseSheet.getRow(j).createCell(2);
                            String value = hm.get(k).toString();
                            if (value.equalsIgnoreCase("PASS")) {

                                c.setCellValue(hm.get(k).toString());

                                Font headerFont = wb.createFont();

                                headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
                                CellStyle headerStyle = wb.createCellStyle();
                                headerStyle.setFont(headerFont);
                                headerStyle
                                        .setFillForegroundColor(IndexedColors.GREEN
                                                .getIndex());
                                headerFont.setColor(IndexedColors.BLACK
                                        .getIndex());

                                headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
                                c.setCellStyle(headerStyle);

                            } else if (value.equalsIgnoreCase("SKIP")) {

                                c.setCellValue(hm.get(k).toString());

                                Font headerFont2 = wb.createFont();

                                headerFont2.setBoldweight(Font.BOLDWEIGHT_BOLD);
                                CellStyle headerStyle2 = wb.createCellStyle();
                                headerStyle2.setFont(headerFont2);
                                headerStyle2
                                        .setFillForegroundColor(IndexedColors.YELLOW
                                                .getIndex());
                                headerFont2.setColor(IndexedColors.BLACK
                                        .getIndex());
                                headerStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
                                c.setCellStyle(headerStyle2);

                            } else if (value.equalsIgnoreCase("FAIL")) {

                                c.setCellValue(hm.get(k).toString());

                                Font headerFont1 = wb.createFont();
                                headerFont1.setBoldweight(Font.BOLDWEIGHT_BOLD);
                                CellStyle headerStyle1 = wb.createCellStyle();
                                headerStyle1.setFont(headerFont1);
                                headerStyle1
                                        .setFillForegroundColor(IndexedColors.RED
                                                .getIndex());
                                headerFont1.setColor(IndexedColors.BLACK
                                        .getIndex());
                                headerStyle1.setFillPattern(CellStyle.SOLID_FOREGROUND);
                                c.setCellStyle(headerStyle1);
                            }

                        }
                    }
                }
            }

            fis.close();
            fos = new FileOutputStream("./Results/Results " + curDate()
                    + ".xlsx");
            wb.write(fos);
            fos.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    // returns current date with time
    public static String curDate() {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    // returns current date
    public static String date() {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    }

    // returns past date
    public static String past_furure_Date(int i) {
        SimpleDateFormat date = new SimpleDateFormat("MM-dd-yyyy");
        GregorianCalendar gc = new GregorianCalendar();
        gc.add(Calendar.DAY_OF_MONTH, -i);
        String pastDate = date.format(gc.getTime());
        return pastDate;
    }

    public static void takeScreenShot(WebDriver driver, String name)
            throws IOException {
        File scrFile = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        //FileUtils
        //      .copyFile(scrFile, new File(".\\Screenshot\\" + name + ".png"));

    }

    // to delete .png files in screenshot folder
    public static void deleteRecursive(File path) {
        File[] c = path.listFiles();
        System.out.println("Cleaning out folder:" + path.toString());
        for (File file : c) {
            if (file.isDirectory()) {
                System.out.println("Deleting file:" + file.toString());
                deleteRecursive(file);
                file.delete();
            } else {
                file.delete();
            }
        }
    }

}
package com.expertus.libraries;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Set;

//import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.*;

public class Generic {
    public static int getRowCount(String xlPath, String sheetName) {
        try {
            FileInputStream fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            return wb.getSheet(sheetName).getLastRowNum();
        } catch (Exception e) {

            return -1;
        }

    }

    public static String getCellValue(String xlPath, String sheetName,
            int rowNum, int cellNum) {
        FileInputStream fis;
        try 
        {
            fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            return wb.getSheet(sheetName).getRow(rowNum).getCell(cellNum).getStringCellValue();
        } 
        catch (Exception e) 
        {
            return "";
        }
    }

    public static int getColumnCount(String xlPath, String sheetName, int rowNum) {
        FileInputStream fis;
        try 
        {
            fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            return wb.getSheet(sheetName).getRow(rowNum).getLastCellNum();
        } 
        catch (Exception e) 
        {
            return -1;
        }
    }

    public static void setCellValue(String xlPath, String sheetName,
            int rowNum, int cellNum, String cellVal) {
        FileInputStream fis;
        FileOutputStream fos;
        try {
            fis = new FileInputStream(xlPath);
            Workbook wb = WorkbookFactory.create(fis);
            Row r = wb.getSheet(sheetName).getRow(rowNum);
            if (r == null)
                r = wb.getSheet(sheetName).createRow(rowNum);
            Cell c = r.getCell(cellNum);
            if (c == null)
                c = r.createCell(cellNum);
            c.setCellValue(cellVal);
            fis.close();
            fos = new FileOutputStream(xlPath);
            wb.write(fos);
            fos.close();
        } 
        catch (Exception e) 
        {

        }

    }

    public static void writeRes(String fisPath, HashMap hm) 
    {
        FileInputStream fis;
        FileOutputStream fos;
        try 
        {
            fis = new FileInputStream(fisPath);
            Workbook wb = WorkbookFactory.create(fis);
            CellStyle style = wb.createCellStyle();

            Sheet mainSheet = wb.getSheet("Main");
            int rc = mainSheet.getLastRowNum();
            for (int i = 1; i <= rc; i++) {
                String module = mainSheet.getRow(i).getCell(0)
                        .getStringCellValue();
                Sheet testCaseSheet = wb.getSheet(module);
                int rowCnt = testCaseSheet.getLastRowNum();
                for (int j = 1; j <= rowCnt; j++) {
                    String testcase = testCaseSheet.getRow(j).getCell(0)
                            .getStringCellValue();
                    Set<String> key = hm.keySet();
                    for (String k : key) {
                        if (testcase.equalsIgnoreCase(k)) {
                            Cell c = testCaseSheet.getRow(j).getCell(2);
                            if (c == null)
                                c = testCaseSheet.getRow(j).createCell(2);
                            String value = hm.get(k).toString();
                            if (value.equalsIgnoreCase("PASS")) {

                                c.setCellValue(hm.get(k).toString());

                                Font headerFont = wb.createFont();

                                headerFont.setBold(true);

                                CellStyle headerStyle = wb.createCellStyle();
                                headerStyle.setFont(headerFont);
                                headerStyle
                                        .setFillForegroundColor(IndexedColors.GREEN
                                                .getIndex());
                                headerFont.setColor(IndexedColors.BLACK
                                        .getIndex());
                                headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);


                                c.setCellStyle(headerStyle);

                            } else if (value.equalsIgnoreCase("SKIP")) {

                                c.setCellValue(hm.get(k).toString());

                                Font headerFont2 = wb.createFont();
                                headerFont2.setBold(true);

                                CellStyle headerStyle2 = wb.createCellStyle();
                                headerStyle2.setFont(headerFont2);
                                headerStyle2
                                        .setFillForegroundColor(IndexedColors.YELLOW
                                                .getIndex());
                                headerFont2.setColor(IndexedColors.BLACK
                                        .getIndex());

                                headerStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                                c.setCellStyle(headerStyle2);

                            } else if (value.equalsIgnoreCase("FAIL")) {

                                c.setCellValue(hm.get(k).toString());

                                Font headerFont1 = wb.createFont();
                                headerFont1.setBold(true);

                                CellStyle headerStyle1 = wb.createCellStyle();
                                headerStyle1.setFont(headerFont1);
                                headerStyle1
                                        .setFillForegroundColor(IndexedColors.RED
                                                .getIndex());
                                headerFont1.setColor(IndexedColors.BLACK
                                        .getIndex());

                                headerStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                                c.setCellStyle(headerStyle1);
                            }

                        }
                    }
                }
            }

            fis.close();
            fos = new FileOutputStream("./Results/Results " + curDate()
                    + ".xlsx");
            wb.write(fos);
            fos.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    // returns current date with time
    public static String curDate() {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    // returns current date
    public static String date() {
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    }

    // returns past date
    public static String past_furure_Date(int i) {
        SimpleDateFormat date = new SimpleDateFormat("MM-dd-yyyy");
        GregorianCalendar gc = new GregorianCalendar();
        gc.add(Calendar.DAY_OF_MONTH, -i);
        String pastDate = date.format(gc.getTime());
        return pastDate;
    }

    public static void takeScreenShot(WebDriver driver, String name)
            throws IOException {
        File scrFile = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        //FileUtils
        //      .copyFile(scrFile, new File(".\\Screenshot\\" + name + ".png"));

    }

    // to delete .png files in screenshot folder
    public static void deleteRecursive(File path) {
        File[] c = path.listFiles();
        System.out.println("Cleaning out folder:" + path.toString());
        for (File file : c) {
            if (file.isDirectory()) {
                System.out.println("Deleting file:" + file.toString());
                deleteRecursive(file);
                file.delete();
            } else {
                file.delete();
            }
        }
    }

}
package com.expertus.libraries;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.Calendar;
导入java.util.Date;
导入java.util.GregorianCalendar;
导入java.util.HashMap;
导入java.util.Set;
//导入org.apache.commons.io.FileUtils;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.IndexedColors;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
导入org.openqa.selenium.OutputType;
导入org.openqa.selenium.TakesScreenshot;
导入org.openqa.selenium.WebDriver;
导入org.apache.poi.ss.usermodel.FillPatternType;
导入org.apache.poi.ss.usermodel.*;
公共类泛型{
公共静态int getRowCount(字符串xlPath,字符串sheetName){
试一试{
FileInputStream fis=新的FileInputStream(xlPath);
工作簿wb=WorkbookFactory.create(fis);
返回wb.getSheet(sheetName).getLastRowNum();
}捕获(例外e){
返回-1;
}
}
公共静态字符串getCellValue(字符串xlPath、字符串sheetName、,
int rowNum,int cellNum){
文件输入流fis;
尝试
{
fis=新文件输入流(xlPath);
工作簿wb=WorkbookFactory.create(fis);
返回wb.getSheet(sheetName).getRow(rowNum).getCell(cellNum).getStringCellValue();
} 
捕获(例外e)
{
返回“”;
}
}
公共静态int getColumnCount(字符串xlPath、字符串sheetName、int rowNum){
文件输入流fis;
尝试
{
fis=新文件输入流(xlPath);
工作簿wb=WorkbookFactory.create(fis);
返回wb.getSheet(sheetName).getRow(rowNum.getLastCellNum();
} 
捕获(例外e)
{
返回-1;
}
}
公共静态void setCellValue(字符串xlPath、字符串sheetName、,
int rowNum、int cellNum、String cellVal){
文件输入流fis;
文件输出流;
试一试{
fis=新文件输入流(xlPath);
工作簿wb=WorkbookFactory.create(fis);
行r=wb.getSheet(sheetName).getRow(rowNum);
if(r==null)
r=wb.getSheet(sheetName).createRow(rowNum);
Cell c=r.getCell(cellNum);
如果(c==null)
c=r.createCell(cellNum);
c、 setCellValue(cellVal);
fis.close();
fos=新文件输出流(xlPath);
wb.write(fos);
fos.close();
} 
捕获(例外e)
{
}
}
公共静态void writers(字符串路径,HashMap hm)
{
文件输入流fis;
文件输出流;
尝试
{
fis=新文件输入流(fisPath);
工作簿wb=WorkbookFactory.create(fis);
CellStyle=wb.createCellStyle();
表mainSheet=wb.getSheet(“主”);
int rc=mainSheet.getLastRowNum();

对于(int i=1;i虽然这是一个老问题,OP很可能已经解决了它,但它是在谷歌搜索相同问题时首先出现的,我想添加我的解决方案,以防它对任何人都有帮助

对于poi 4.1.7,我使用了
font.setBoldweight(true)
而不是
font.setBoldweight(font.BOLDWEIGHT\u BOLD)

为了把它放在帖子的问题中,作者可以修改他们的代码

发件人:
headerFont2.setBoldweight(Font.BOLDWEIGHT\u BOLD);

致:
headerFont.font.setBold(true);

鉴于他们还导入了字体类:

import org.apache.poi.ss.usermodel.Font;