**这里我有一个名为Merged.xlsx的excel文件。该图为:-**x1c 0d1x
并且我希望此数据以JSON格式显示为:-
{
"Cars":[
{"SellerName":"govinda lamgade","Seller Address":"-Dallu,Kathmandu",...},
],
"Motorcycle":[
{Same as above},
]
}
"到目前为止,我得到的结果是"
{"rows":[{"cell":["Skoda Rapid On Sale And Exchange"," govinda lamgade ","- Dallu, Kathmandu","19-06-2020",1450000.0,"https://cdn.hamrobazaar.com/21...
这不是我想要的。代码是:-
String excelFilePath = "E:\\Merged.xlsx";
FileInputStream fileInputStream = new FileInputStream(excelFilePath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
JsonObject jsonObject = new JsonObject();
JsonArray rows = new JsonArray();
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()){
Row row = rowIterator.next();
if(row.getRowNum() == 0) {
continue;
}
else{
JsonObject jRow = new JsonObject();
JsonArray cells = new JsonArray();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
cells.add(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
cells.add(cell.getStringCellValue());
break;
}
jRow.add("cell",cells);
rows.add(jRow);
}
jsonObject.add("rows", rows);
}
System.out.println(jsonObject.toString());
fileInputStream.close();
}
}
}
"谢谢你"
1条答案
按热度按时间wnvonmuf1#
您可以按如下方式执行此操作:
假设条件:
重要提示:
您必须为每个json
value
给予预期的jsonkey
作为Excel工作表中的列名(例如:-在Excel文件中将sellerName
作为列名,而不是卖方名称)所需的导入;
您应该获取Cell数据并将其放入一个HashMap中,然后将HashMap转换为
JsonElement
;如果打印
completeJson
;类似地,你可以创建
Motorcycle
json数组。当添加MotorCycleJsonArray
以完成Json时,将键设置为Motorcycle
(或你喜欢的键)