0

I have a data like this(got from Parsing a JSON file). I want to convert it to excel sheet using Apache POI or any other jars. The data is :

heading1  ---> 2014-01-15 00:00:00.0
heading2  ---> A5CD51
heading3
heading4
     sub-head41---> 1235148727
     sub-head42---> 8015
     sub-head43---> 9999-12-31
     sub-head44---> 1962-07-09
heading5
     sub-head51---> 2014-01-15
     sub-head52---> 0
     sub-head53---> 175_INFA-BATCH
heading6---> 994127F
heading1---> 2014-11-24 00:00:00.0
heading2---> 28F313
heading3
heading4
     sub-head41 ---> 1235148727
     sub-head42 ---> 8015
     sub-head43 ---> 9999-12-31
     sub-head44 ---> 1962-07-09
heading5
     heading51---> 2014-11-24
     heading52---> 1
     heading53---> 343_INFA-BATCH
heading6---> 324343

This should be converted to excel as shown below :

enter image description here

An example JSON from where I get the data is

"heading1 ":"994127F79137C42A38823F498F1496C2",
"heading3"{   },
"heading2 ":"A5CD51AC07214EF3FEB6563E4B1CEE38",
  "heading3":"2014-01-15 00:00:00.0",
   "heading4":{  
               "sub-head41":"2F94846E636273BD837BA47732D66613",
                "sub-head42":"178",
                  "sub-head43":"1962-07-09"
},

Sample code I use to parse the JSON and get the data :

String innerObjKey = (String) segmentSetitr.next();

if(innerObj.get(innerObjKey)instanceof JSONObject)
{
    JSONObject innerEle = (JSONObject) innerObj.get(innerObjKey);
    System.out.println(innerObjKey);
    Set<String> innerObjSet = innerObj.keySet();
    Iterator innerObjSetitr = innerObjSet.iterator();
    while (innerObjSetitr.hasNext()) {
        String key = (String) innerObjSetitr.next();
        System.out.println("\t " + key + " ---> " + innerObj.get(key));
}else
{

    System.out.println(innerObjKey + "  ---> " + innerObj.get(innerObjKey));
    cell.setCellValue((String) innerObjKey);

}

In this all the fields are dynamic. The number of headings and sub heading will vary dynamically. Can anyone please tell me how this can be done.

Nagesh
  • 434
  • 1
  • 8
  • 26
  • 1
    What did you try? What is your issue? –  Sep 20 '16 at 11:17
  • @RC.I am new to Apache POI. I am not sure how to do. I started by trying with some examples. But it coming completely wrong and not as I expect – Nagesh Sep 20 '16 at 11:48
  • 4
    Try the following doc: https://poi.apache.org/spreadsheet/quick-guide.html - this usually leaves no open questions. If specific problems remain, come back to SO with the concrete code which misbehaves. – mtj Sep 20 '16 at 12:04

0 Answers0