java - Apache POI 在电子表格中仅记录 1 行

标签 java excel selenium-webdriver apache-poi

我正在使用 selenium 和 java 来抓取特定站点上的数据,但是使用下面的代码我只能在电子表格中写入 1 个数据,它没有按应有的顺序记录所有数据。

我无法正确构建循环。

public void gravarDados() throws IOException {

    int i = 0;
    File localPlanilha = new File("tools/resultado_da_pesquisa.xlsx");

    FileInputStream planilhaExistente = new FileInputStream(localPlanilha);

    XSSFWorkbook plan = new XSSFWorkbook(planilhaExistente);

    XSSFSheet sheetExistente = plan.getSheetAt(0);

for (int i = 0; i < inicio; i++) {
    // Writing data
    sheetExistente.getRow(2).createCell(5).setCellValue(TitulosHoteis.get(i).getText());


    FileOutputStream fechandoArquivo = new FileOutputStream(localPlanilha);

    plan.write(fechandoArquivo);
  }
}

poi

最佳答案

目前您仅获得第 0 个元素。

您需要使用 for 循环迭代下面的内容

TitulosHoteis.get(i).getText());

将结果写入行和列。

请修改如下

for (int i = 0; i < inicio; i++) {
    // Writing data
    sheetExistente.getRow(i+2).createCell(5).setCellValue(TitulosHoteis.get(i).getText());

}
    FileOutputStream fechandoArquivo = new FileOutputStream(localPlanilha);

    plan.write(fechandoArquivo);

关于java - Apache POI 在电子表格中仅记录 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60420348/

相关文章:

java - Spring Boot - JPA - Postgres 错误 : cross-database references are not implemented:

java - Graphviz语言从jsp View 动态

java - 检索所有 View 对象,android

vba - 将匹配的行复制到另一张纸中

java - 通过 Java 和 Selenium 启动 Internet Explorer 时出现 "OsProcess checkForError : CreateProcess error=193, %1 is not a valid Win32 application"

python : no module named selenium

java - 数据存储区/动态文本文件中的 Google App Engine (Java) 文本

excel - 将字符串从用户表单移动到调用表单的事件单元格

python -\t 用于 excel 粘贴到新单元格

c# - Selenium webdriver c# 等待文本出现