Java 如何在excel单元格中查找要写入的列名

Java 如何在excel单元格中查找要写入的列名,java,Java,我正在使用eclipse、SeleniumWeb驱动程序、ApachePOI、java。我对这个和java非常陌生 我有三列xls文件:电子邮件,pw,结果。我有3行数据。结果列为空 每次迭代,脚本都应该阅读电子邮件和pw,然后执行操作。然后将结果写入结果列 继续,直到到达最后一行 我硬编码了列号,它可以工作。我不知道如何找到标题为“result”的列号,然后如何在单元格中写入对应行 File file = new File("C://dd.xls"); try {

我正在使用eclipse、SeleniumWeb驱动程序、ApachePOI、java。我对这个和java非常陌生

我有三列xls文件:电子邮件,pw,结果。我有3行数据。结果列为空

每次迭代,脚本都应该阅读电子邮件和pw,然后执行操作。然后将结果写入结果列

继续,直到到达最后一行

我硬编码了列号,它可以工作。我不知道如何找到标题为“result”的列号,然后如何在单元格中写入对应行

    File file =    new File("C://dd.xls");

    try {
        // Open the Excel file
        FileInputStream fis = new FileInputStream(file);

        // Access the required test data sheet
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheet("Sheet1");
        int TotalRow;
        int TotalCol;
        TotalRow = sheet.getLastRowNum();
        TotalCol = sheet.getRow(0).getLastCellNum();
        System.out.println("row " + TotalRow +  " col " +  TotalCol);


        // Loop through all rows in the sheet
        // Start at row 1 as row 0 is our header row
        for(int count = 1;count<=sheet.getLastRowNum();count++){
            //for(int col = 1;col<=TotalCol;col++){


             HSSFRow row = sheet.getRow(count);
             System.out.println("Running test case " + row.getCell(0).toString() +  " " +  row.getCell(1).toString());
             HSSFCell hSSFCell = row.getCell(TotalCol-1);
             String value = count + "aa";
             hSSFCell.setCellValue(value);}
          // }
        fis.close();
        FileOutputStream outputStream = new FileOutputStream(file);
        wb.write(outputStream);
        outputStream.close();

} catch (IOException e) {
    System.out.println("Test data file not found");
} 
File File=new文件(“C://dd.xls”);
试一试{
//打开Excel文件
FileInputStream fis=新的FileInputStream(文件);
//访问所需的测试数据表
HSSF工作手册wb=新的HSSF工作手册(fis);
HSSFSheet sheet=wb.getSheet(“Sheet1”);
整数行;
int TotalCol;
TotalRow=sheet.getLastRowNum();
TotalCol=sheet.getRow(0.getLastCellNum();
System.out.println(“行”+TotalRow+“列”+TotalCol”);
//循环浏览图纸中的所有行
//从第1行开始,因为第0行是标题行
对于(int count=1;count
HSSF工作簿wb=新的HSSF工作簿(fis);
HSSFSheet sheet=wb.getSheet(“Sheet1”);
整数行;
int TotalCol;
TotalRow=sheet.getLastRowNum();
HSSFRow headerRow=sheet.getRow(0);
字符串结果=”;
int resultCol=-1;
用于(单元格:headerRow){
结果=cell.getStringCellValue();
如果(结果等于(“结果”){
resultCol=cell.getColumnIndex();
打破
}
}
if(resultCol==-1){
System.out.println(“在表中未找到结果列”);
返回;
}   
System.out.println(“行”+TotalRow+“列”+TotalCol”);
//循环浏览图纸中的所有行
//从第1行开始,因为第0行是标题行

for(int count=1;count从A1开始并读取单元格文本(A2、A3等)当你找到你的标题时,我会打断你???谢谢你,但我得到了错误。请查看上面的错误。如果你需要其他信息,我可以提供给你。你使用了哪个版本的java,POI?对于这些代码,我建议你使用java 8和apache POI 3.15Java 1.8 apache POI 3.9。请查看上面的google文档了解错误。谢谢你,但我仍然有错误。我们有两个错误。你能再看看谷歌文档吗?我通过添加“)”修正了第二个错误。所以第一个错误没有修正。
    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sheet = wb.getSheet("Sheet1");
    int TotalRow;
    int TotalCol;
    TotalRow = sheet.getLastRowNum();
    HSSFRow headerRow = sheet.getRow(0);
    String result = "";
    int resultCol = -1;
    for (Cell cell : headerRow){
        result = cell.getStringCellValue();
        if (result.equals("Result"){
            resultCol = cell.getColumnIndex();
            break;
         }
    }
    if (resultCol == -1){
        System.out.println("Result column not found in sheet");
        return;
    }   
    System.out.println("row " + TotalRow +  " col " +  TotalCol);


    // Loop through all rows in the sheet
    // Start at row 1 as row 0 is our header row
    for(int count = 1;count<=sheet.getLastRowNum();count++){
         HSSFRow row = sheet.getRow(count);
         System.out.println("Running test case " + row.getCell(0).toString() +  " " +  row.getCell(1).toString());
         HSSFCell hSSFCell = row.getCell(resultCol);
         String value = count + "aa";
         hSSFCell.setCellValue(value);}
      // }
    fis.close();
    FileOutputStream outputStream = new FileOutputStream(file);
    wb.write(outputStream);
    outputStream.close();