Java 如何使用GSON库将excel文件数据转换为JSON格式?
这里有一个名为Merged.xlsx的excel文件。其数字如下:- 我希望此数据以JSON格式显示为:-Java 如何使用GSON库将excel文件数据转换为JSON格式?,java,excel,gson,apache-poi,Java,Excel,Gson,Apache Poi,这里有一个名为Merged.xlsx的excel文件。其数字如下:- 我希望此数据以JSON格式显示为:- { "Cars":[ {"SellerName":"govinda lamgade","Seller Address":"-Dallu,Kathmandu",...}, ], "Motorcycle":[ {Same as above},
{
"Cars":[
{"SellerName":"govinda lamgade","Seller Address":"-Dallu,Kathmandu",...},
],
"Motorcycle":[
{Same as above},
]
}
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();
}
}
到目前为止,我得到的结果是
{"rows":[{"cell":["Skoda Rapid On Sale And Exchange"," govinda lamgade ","- Dallu, Kathmandu","19-06-2020",1450000.0,"https://cdn.hamrobazaar.com/21...
这不是我想要的。代码如下:-
{
"Cars":[
{"SellerName":"govinda lamgade","Seller Address":"-Dallu,Kathmandu",...},
],
"Motorcycle":[
{Same as above},
]
}
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();
}
}
String excelFilePath=“E:\\Merged.xlsx”;
FileInputStream FileInputStream=新的FileInputStream(excelFilePath);
工作簿工作簿=新XSSF工作簿(fileInputStream);
工作表=工作簿。getSheetAt(0);
JsonObject JsonObject=新的JsonObject();
JsonArray行=新的JsonArray();
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
if(row.getRowNum()==0){
继续;
}
否则{
JsonObject jRow=新的JsonObject();
JsonArray cells=新的JsonArray();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u数值:
cells.add(cell.getNumericCellValue());
打破
case Cell.Cell\u类型\u字符串:
cells.add(cell.getStringCellValue());
打破
}
添加(“单元格”,单元格);
添加(jRow);
}
添加(“行”,行);
}
System.out.println(jsonObject.toString());
fileInputStream.close();
}
}
}
谢谢你你可以这样做: 假设:
- 本表仅包含与汽车相关的数据
- 工作表中没有空行
- 所有行的每一列都有数据值
值提供预期的json键
,作为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.*;
您应该获取单元格数据并将其放入HashMap,然后将HashMap转换为jsoneElement
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);
像wise一样,您可以创建Motorcycle
json数组。当添加MotorCycleJsonArray
以完成JSON时,将按键设置为MotorCycle
(或您喜欢的按键)