java - 如何使用 GSON 库将 excel 文件数据转换为 JSON 格式?
问题描述
这里我有一个名为 Merged.xlsx 的 excel 文件。这个图如下:-
我希望这些数据以 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();
}
}
}
谢谢你
解决方案
您可以按以下方式执行此操作;
假设:
- 此表仅包含汽车相关数据
- 工作表中没有空行
- 所有行都有每列的数据值
重要的:
您必须key
为每个 json提供预期的 jsonvalue
作为 excel 表中的列名(例如:-sellerName
在 excel 文件中作为列名而不是卖方名称)
所需进口;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.util.*;
您应该获取 Cell 数据并放入 HashMap,然后将 HashMap 转换为JsonElement
;
String excelFilePath = "E:\\Merged.xlsx";
FileInputStream fileInputStream = new FileInputStream(excelFilePath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
JsonArray carArray = new JsonArray();
List<String> columnNames = new ArrayList<>();
Gson gson = new Gson();
// Get column names
Row row = sheet.getRow(0);
for (Iterator<Cell> it = row.cellIterator(); it.hasNext(); ) {
Cell cell = it.next();
columnNames.add(cell.getStringCellValue());
}
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
if (row.getRowNum()==0) {
continue;
}
Iterator<String> columnNameIterator = columnNames.iterator();
Iterator<Cell> cellIterator = row.cellIterator();
// Create a new map for the row
Map<String, Object> newCarMap = new HashMap<>();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String columnName = columnNameIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
newCarMap.put(columnName, cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
newCarMap.put(columnName, cell.getStringCellValue());
break;
}
}
// Convert the map to `JsonElement`
JsonElement carJson = gson.toJsonTree(newCarMap);
// Add the `JsonElement` to `JsonArray`
carArray.add(carJson);
}
// Add the `JsonArray` to `completeJson` object with the key as `Cars`
JsonObject completeJson = new JsonObject();
completeJson.add("Cars", carArray);
如果您打印completeJson
;
{
"Cars": [
{
"price": 111111,
"imageUrl": "https://example_url_1",
"sellerAddress": "address_1",
"sellerName": "name_1",
"date": "12-07-2020"
},
{
"price": 222222,
"imageUrl": "https://example_url_2",
"sellerAddress": "address_2",
"sellerName": "name_2",
"date": "19-06-2020"
}
// rest of the car json objects will come here
]
}
同样,您可以创建Motorcycle
json 数组。将 MotorCycle 添加JsonArray
到 completeJson 时,将密钥设置为Motorcycle
(或您喜欢的密钥)
推荐阅读
- php - Laravel + Docker 问题(更改不适用)
- c - 如果有的话,什么是技术上的理由来引导一个函数指针参数,然后是那个函数的参数,或者相反?
- python - 如何使用分治法解决 FindMaximumSubarray 的复发错误?
- javascript - 无法识别 instanceof 功能
- rtf - BI Publisher - 水平显示数据 MONTH
- vue.js - 在 vue 中推送组件
- python - TypeError:类似字节的对象,而不是字符串
- python - 如何使用方法补丁创建函数
- angular - 我们如何将调度结果存储到 Actions 中的变量中?
- php - 无法从表单获取复选框以在 php 中显示