私は5つのjsonデータを取り出し、Excelシートに書き込もうとしています。私はデータをjsonarrayに変換してから、POIを使ってExcelに書き出します。何らかの理由で、プログラムは現在、5行目のデータを最初の行に書き込むだけで、他の行には何もしません。それはすべての5をループし、それに対応する行にそれぞれを入れる必要があります。何か案は?json配列からApache POIを使ってExcelシートを書く
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++;
}
}
あなたのシートにすべての行を書き込むには、jsonarrayをループする必要があります。 –