我正在使用 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);
}
}
最佳答案
目前您仅获得第 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/