Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从json数组使用ApachePOI编写Excel工作表_Java_Json_Excel_Apache Poi - Fatal编程技术网

Java 从json数组使用ApachePOI编写Excel工作表

Java 从json数组使用ApachePOI编写Excel工作表,java,json,excel,apache-poi,Java,Json,Excel,Apache Poi,我试图提取5条json数据并将其写入excel工作表。我将数据转换为jsonarray,然后使用POI写入excel。出于某种原因,该程序目前只将第5个数据段写入第一行,而不对其他数据段执行任何操作。它应该在所有5行中循环,并将每一行放在它对应的行中。有什么想法吗 private void sendPost() { int rowNumber = 1; while (rowNumber < 5 ) { try { String id = censusIdValue

我试图提取5条json数据并将其写入excel工作表。我将数据转换为jsonarray,然后使用POI写入excel。出于某种原因,该程序目前只将第5个数据段写入第一行,而不对其他数据段执行任何操作。它应该在所有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++;

        }
}
private void sendPost(){
int rowNumber=1;
while(行数<5){
试一试{
字符串id=censusIdValue(行号);
字符串url=”https://www.broadbandmap.gov/broadbandmap/analyze/jun2014/summary/population/censusplace/ids/“+URLEncoder.encode(id,“UTF-8”)+”?格式=json”;
HttpClient client=HttpClientBuilder.create().build();
HttpGet HttpGet=新的HttpGet(url);
HttpResponse response=client.execute(httpGet);
InputStream is=response.getEntity().getContent();
字符串结果=getStringFromInputStream(is);
JSONObject jsonObj=新的JSONObject(result.toString());
JSONArray data=jsonObj.getJSONArray(“结果”);
int rowCount=0;
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(“哇”);
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=行.createCell(1);
cell1.setCellValue(geographyId);
XSSFCell cell2=行.createCell(2);
cell2.setCellValue(strStatusType);
XSSFCell cell3=row.createCell(3);
cell3.设置CellValue(numberOfWirelineProvidersEquals0);
try(FileOutputStream outputStream=newfileoutputstream(“data.xlsx”)){
workbook.write(outputStream);
}
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(不支持操作异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
行数++;
}
}

看起来您正在用每一行覆盖文件。尝试将写操作置于循环之外

另外,去掉
rowCount
变量。您想在那里使用
rowNumber
变量


最后,我认为你只循环了4次。(您在问题中表示需要5。)

看起来您正在用每一行覆盖文件。尝试将写操作置于循环之外

另外,去掉
rowCount
变量。您想在那里使用
rowNumber
变量


最后,我认为你只循环了4次。(您在问题中表示需要5。)

您应该在jsonarray上循环以写入工作表中的所有行。您应该在jsonarray上循环以写入工作表中的所有行。rowNumber是程序的另一部分,但感谢您提醒我注意它!问题是我需要将“int rowCount=0”移到循环之外,因为它一直在重置,因此没有创建新行。rowCount是程序的另一部分,但感谢您提醒我注意它!问题是我需要将“introwcount=0”移到循环之外,因为它一直在重置,所以没有创建新行。