【问题标题】:How to find the column name to write in excel cell如何在excel单元格中找到要写入的列名
【发布时间】:2017-06-17 20:59:31
【问题描述】:

我正在使用 eclipse、selenium web 驱动程序、apache POI、java。我对这个和 java 很陌生。

我有一个包含 3 列的 xls 文件:电子邮件、密码、结果。我有 3 行数据。结果栏为空白。

每次迭代,脚本都应该读取电子邮件和密码,然后执行操作。然后将结果写入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");
} 

https://drive.google.com/open?id=1jqefYaffunknolrj6i4SXOoq2LiKT3xTgSsdzaxsBfQ

【问题讨论】:

  • 从 A1 开始并读取单元格的文本(A2、A3 等)并在找到标题时中断???

标签: java


【解决方案1】:
    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();

【讨论】:

  • 谢谢,但我遇到了错误。请参阅上面的错误。如果您需要更多信息,我可以为您提供。
  • 你用的是哪个版本的java,POI?对于该代码,我建议您使用 java 8 和 apache POI 3.15
  • Java 1.8 Apache POI 3.9。请参阅上面的谷歌文档了解错误。
  • 谢谢,但我仍然有错误。我们有两个错误。你能再看看谷歌文档吗?
  • 我通过添加“)”修复了第二个错误。所以第一个错误没有修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多