5 개의 json 데이터를 가져 와서 Excel 시트에 쓰려고합니다. 데이터를 jsonarray로 변환 한 다음 POI를 사용하여 Excel에 쓰려고합니다. 어떤 이유로 프로그램은 현재 첫 번째 행에 다섯 번째 데이터 조각 만 쓰고 다른 행은 수행하지 않습니다. 그것은 모두 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를 반복해야합니다. –