Apache POI 在电子表格中仅记录 1 行 [英] Apache POI recording only 1 row in the spreadsheet

查看:27
本文介绍了Apache POI 在电子表格中仅记录 1 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using selenium and java to scrape data on a specific site, but with the code below I can write only 1 data in the spreadsheet, it is not recording all the data in sequence as it should be.

我无法正确构建循环.

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

推荐答案

目前您只得到第 0 个元素.

Currently you are getting only the 0th element.

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

You need to iterate the below with a for loop

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

这篇关于Apache POI 在电子表格中仅记录 1 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆