使用来自json数组的Apache POI编写Excel工作表

我试图拉5块JSON数据,并将它们写入Excel表。 我将数据转换为jsonarray,然后使用POI写入excel。 出于某种原因,该程序目前只将第五行数据写入第一行,而对其他行不做任何处理。 它应该循环遍历所有5,并把它们对应的行。 有任何想法吗?

private void sendPost() { int rowNumber = 1; while (rowNumber < 5 ) { try { String id = censusIdValue(rowNumber); String url = "https://www.broadbandmap.gov/broadbandmap/analyze/jun2014/summary/population/censusplace/ids/" + URLEncoder.encode(id, "UTF-8") + "?format=json"; HttpClient client = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); HttpResponse response = client.execute(httpGet); InputStream is = response.getEntity().getContent(); String result = getStringFromInputStream(is); JSONObject jsonObj = new JSONObject(result.toString()); JSONArray data = jsonObj.getJSONArray("Results"); int rowCount = 0; XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("wow"); JSONObject rec = data.getJSONObject(0); String geographyId = rec.getString("geographyId"); String strStatusType = rec.getString("geographyName"); int numberOfWirelineProvidersEquals0 = rec.getInt("numberOfWirelineProvidersEquals0"); XSSFRow row = sheet.createRow(rowCount++); XSSFCell cell1 = row.createCell(1); cell1.setCellValue(geographyId); XSSFCell cell2 = row.createCell(2); cell2.setCellValue(strStatusType); XSSFCell cell3 = row.createCell(3); cell3.setCellValue(numberOfWirelineProvidersEquals0); try (FileOutputStream outputStream = new FileOutputStream("data.xlsx")) { workbook.write(outputStream); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedOperationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } rowNumber++; } } 

它看起来像覆盖每一行的文件。 尝试把写入外循环。

另外,摆脱rowCountvariables。 你想在那里使用你的rowNumbervariables。

最后,我想你只是循环4次。 (你表明你想在你的问题5。)