如何迭代遍历一个包含多个逗号分隔值的 xlsx 文件,并将其转换为 JSONArray。

huangapple go评论67阅读模式
英文:

How to iterate over a xlsx file with multiple comma separated values in cell in to a JSONArray

问题

我正在使用Java Apache POI工具读取一个xlsx文件。在这个xlsx文件中,有三列分别是Teacher,class&Section,Sunbect。最后两列在单元格中有多个逗号分隔的值。我需要迭代并构建一个JSONArray。

我已经尝试了一些代码:

try {
    FileInputStream inputStream = new FileInputStream(new File("C:/Users/HP/Downloads/school.xlsx"));
    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet AddCatalogSheet  = workbook.getSheetAt(0);
    int rowcount = AddCatalogSheet.getLastRowNum() - AddCatalogSheet.getFirstRowNum();
    System.out.println("Total row number: " + rowcount);
    for (int i = 1; i < rowcount + 1; i++) {
        Row row = AddCatalogSheet.getRow(i);
        List<String> arrName = new ArrayList<String>();
        for (int j = 0; j < row.getLastCellNum(); j++) {
            Cell cell = row.getCell(j);
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    arrName.add(NumberToTextConverter.toText(cell.getNumericCellValue()));
                    break;
                case Cell.CELL_TYPE_STRING:
                    arrName.add(cell.getStringCellValue());
                    break;
            }
        }
        System.out.println(arrName);
        System.out.println("Size of the arrayList: " + arrName.size());
        JSONObject teacher = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (int counter = 0; counter < arrName.size(); counter++) {
            System.out.println(arrName.get(counter));
            jsonArray.put(arrName.get(counter));
        }
        System.out.println(jsonArray.toString());
    }
} catch (IOException e) {
    e.printStackTrace();
}

我的期望结果如下:

[
  {
    "Teacher_code": "23424234",
    "class": [
      {
        "class": "6",
        "section": "A"
      },
      {
        "class": "7",
        "section": "B"
      },
      {
        "class": "8",
        "section": "A"
      }
    ],
    "subject_name": [
      {
        "subject": "Tamil"
      },
      {
        "subject": "English"
      },
      {
        "subject": "Maths"
      }
    ]
  }
]

请帮助我解决这个问题。

英文:

I am reading a xlsx file using java Apache poi util. In which the xlsx file has 3 columns as Teacher, class&Section, Sunbect. The last two columns have multiple comma separated values in the cell. I need to iterate over and construct a JSONArray.

XLSX sheet

I have tried some code

try {
FileInputStream inputStream = new FileInputStream(new File(&quot;C:/Users/HP/Downloads/school.xlsx&quot;));
//FileInputStream inputStream = new FileInputStream(new File(&quot;TestExecution.xlsx&quot;));
//HashMap&lt;Integer, Data&gt; mp= new HashMap&lt;Integer, Data&gt;();
HashMap&lt;String, List&lt;String&gt;&gt; mp= new HashMap&lt;&gt;();
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet AddCatalogSheet  = workbook.getSheetAt(0);
//Find number of rows in excel file
int rowcount = AddCatalogSheet.getLastRowNum()- AddCatalogSheet.getFirstRowNum();
System.out.println(&quot;Total row number: &quot;+rowcount);
for(int i=1; i&lt;rowcount+1; i++){
//Create a loop to get the cell values of a row for one iteration
Row row = AddCatalogSheet.getRow(i);
List&lt;String&gt; arrName = new ArrayList&lt;String&gt;();
for(int j=0; j&lt;row.getLastCellNum(); j++){
// Create an object reference of &#39;Cell&#39; class
Cell cell = row.getCell(j);
switch (cell.getCellType()) 
{
case Cell.CELL_TYPE_NUMERIC:
//System.out.print(cell.getNumericCellValue() + &quot;\t&quot;);
arrName.add(NumberToTextConverter.toText(cell.getNumericCellValue()));
break;
case Cell.CELL_TYPE_STRING:
//System.out.print(cell.getStringCellValue() + &quot;\t&quot;);
arrName.add(cell.getStringCellValue());
break;
}
// Add all the cell values of a particular row
}
System.out.println(arrName);
System.out.println(&quot;Size of the arrayList: &quot;+arrName.size());
// Create an iterator to iterate through the arrayList- &#39;arrName&#39;
JSONObject teacher = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (int counter = 0; counter &lt; arrName.size(); counter++) { 		      
System.out.println(arrName.get(counter)); 	
jsonArray.put(arrName.get(counter));
} 
System.out.println(jsonArray.toString()); 
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

My Expected Result like :

[
{
&quot;Teacher_code&quot;: &quot;23424234&quot;,
&quot;class&quot;: [
{
&quot;class&quot;: &quot;6&quot;,
&quot;section&quot;: &quot;A&quot;
},
{
&quot;class&quot;: &quot;7&quot;,
&quot;section&quot;: &quot;B&quot;
},
{
&quot;class&quot;: &quot;8&quot;,
&quot;section&quot;: &quot;A&quot;
}
],
&quot;subject_name&quot;: [
{
&quot;subject&quot;: &quot;Tamil&quot;
},
{
&quot;subject&quot;: &quot;English&quot;
},
{
&quot;subject&quot;: &quot;Maths&quot;
}
]
}
]

Please help me to figure out this

答案1

得分: 0

我认为不应该在你的代码中使用JsonObject,这会使你的代码变得冗长且难以理解。正确的做法是将其封装在一个POJO中。

public static void main(String[] args) throws Exception {
    try (InputStream in = Files.newInputStream(Paths.get("E:\\test\\res\\school.xlsx"), READ);
         Workbook workbook = new XSSFWorkbook(in)) {
        Sheet sheet  = workbook.getSheetAt(0);

        Row headRow = sheet.getRow(sheet.getFirstRowNum());
        //获取第一行
        List<String> head = IntStream.range(0, headRow.getLastCellNum())
                .mapToObj(i -> headRow.getCell(i).getStringCellValue())
                .collect(Collectors.toList());

        List<JSONObject> jsonObjects = new ArrayList<>();
        //跳过第一行
        for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            JSONObject jsonObject = new JSONObject();
            for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                Cell cell = row.getCell(j);
                switch (cell.getCellType()) {
                    case STRING:
                        String strV = cell.getStringCellValue();
                        List<JSONObject> collect = Arrays.stream(strV.split(","))
                                .map(s -> {
                                    JSONObject classObj = new JSONObject();
                                    if (!s.contains("-")) {
                                        classObj.put("subject", s);
                                        return classObj;
                                    }
                                    String[] classAndSection = s.split("-");
                                    classObj.put("class", classAndSection[0]);
                                    classObj.put("section", classAndSection[1]);
                                    return classObj;
                                })
                                .collect(Collectors.toList());
                        jsonObject.put(head.get(j), collect);
                        break;
                    case NUMERIC:
                        //修复 2.34566E
                        NumberFormat nf = NumberFormat.getInstance();
                        nf.setGroupingUsed(false);
                        String format = nf.format(cell.getNumericCellValue());
                        //教师
                        if (j == row.getFirstCellNum()) {
                            jsonObject.put(head.get(j), format);
                            break;
                        }
                        JSONObject classObj = new JSONObject();
                        classObj.put("class", format);
                        jsonObject.put(head.get(j), classObj);
                        break;
                }
            }
            jsonObjects.add(jsonObject);
        }
        System.out.println(JSON.toJSONString(jsonObjects));
    }
}

代码运行结果

[
  {
    "teacher": "23424234",
    "classAndSection": [
      {
        "section": "A",
        "class": "8"
      },
      {
        "section": "B",
        "class": "9"
      }
    ],
    "subject_name": [
      {
        "subject": "English"
      },
      {
        "subject": "Math"
      }
    ]
  },
  {
    "teacher": "23424234",
    "classAndSection": {
      "class": "8"
    },
    "subject_name": [
      {
        "subject": "English"
      }
    ]
  }
]
英文:

i think don't use JsonObject inside your code,this will make your code very bloated and difficult to understand. The correct way to do it is to wrap it in a pojo.

public static void main(String[] args) throws Exception {
try (InputStream in = Files.newInputStream(Paths.get(&quot;E:\\test\\res\\school.xlsx&quot;), READ);
Workbook workbook = new XSSFWorkbook(in)) {
Sheet sheet  = workbook.getSheetAt(0);
Row headRow = sheet.getRow(sheet.getFirstRowNum());
//get first row
List&lt;String&gt; head = IntStream.range(0, headRow.getLastCellNum())
.mapToObj(i -&gt; headRow.getCell(i).getStringCellValue())
.collect(Collectors.toList());
List&lt;JSONObject&gt; jsonObjects = new ArrayList&lt;&gt;();
//skip fist row
for (int i = sheet.getFirstRowNum() + 1; i &lt;= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
JSONObject jsonObject = new JSONObject();
for (int j = row.getFirstCellNum(); j &lt; row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()) {
case STRING:
String strV = cell.getStringCellValue();
List&lt;JSONObject&gt; collect = Arrays.stream(strV.split(&quot;,&quot;))
.map(s -&gt; {
JSONObject classObj = new JSONObject();
if (!s.contains(&quot;-&quot;)) {
classObj.put(&quot;subject&quot;, s);
return classObj;
}
String[] classAndSection = s.split(&quot;-&quot;);
classObj.put(&quot;class&quot;, classAndSection[0]);
classObj.put(&quot;section&quot;, classAndSection[1]);
return classObj;
})
.collect(Collectors.toList());
jsonObject.put(head.get(j), collect);
break;
case NUMERIC:
//fix 2.34566E
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
String format = nf.format(cell.getNumericCellValue());
//teacher
if (j == row.getFirstCellNum()) {
jsonObject.put(head.get(j), format);
break;
}
JSONObject classObj = new JSONObject();
classObj.put(&quot;class&quot;, format);
jsonObject.put(head.get(j), classObj);
break;
}
}
jsonObjects.add(jsonObject);
}
System.out.println(JSON.toJSONString(jsonObjects));
}
}

code running result

[
{
&quot;teacher&quot;: &quot;23424234&quot;,
&quot;classAndSection&quot;: [
{
&quot;section&quot;: &quot;A&quot;,
&quot;class&quot;: &quot;8&quot;
},
{
&quot;section&quot;: &quot;B&quot;,
&quot;class&quot;: &quot;9&quot;
}
],
&quot;subject_name&quot;: [
{
&quot;subject&quot;: &quot;English&quot;
},
{
&quot;subject&quot;: &quot;Math&quot;
}
]
},
{
&quot;teacher&quot;: &quot;23424234&quot;,
&quot;classAndSection&quot;: {
&quot;class&quot;: &quot;8&quot;
},
&quot;subject_name&quot;: [
{
&quot;subject&quot;: &quot;English&quot;
}
]
}
]

huangapple
  • 本文由 发表于 2023年3月1日 14:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定