如何在JAVA中将excel/CSV表转换为JSON对象?
我想将excel表(或CSV表)转换为键值对形式的JSON对象。我不能使用任何在线服务,因为文件非常大 例如,我的表格如下所示: 名称 地方 动物 希曼苏 印度 猫 卡莫尔 伦敦 狗 约翰 土耳其 狮子如何在JAVA中将excel/CSV表转换为JSON对象?,java,json,excel,csv,Java,Json,Excel,Csv,我想将excel表(或CSV表)转换为键值对形式的JSON对象。我不能使用任何在线服务,因为文件非常大 例如,我的表格如下所示: 名称 地方 动物 希曼苏 印度 猫 卡莫尔 伦敦 狗 约翰 土耳其 狮子 使用apachepoi将excel数据转换为Java对象(可以使用类保存数据)&然后使用JSONObject将其转换为JSON public static void main(String[] args) throws IOException, JSONException { //Out
使用
apachepoi
将excel数据转换为Java对象(可以使用类保存数据)&然后使用JSONObject
将其转换为JSON
public static void main(String[] args) throws IOException, JSONException {
//Out put JSONArray
JSONArray data = new JSONArray();
//Read the File
FileInputStream fileInputStream = new FileInputStream("D:\\test.xls");
//Load the file into workbook using Apache POI
Workbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.iterator();
int index = 0;
while(rows.hasNext()) {
Row row = rows.next();
if(index!=0) {
//Add to JSON Array
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", row.getCell(0).getStringCellValue());
jsonObject.put("Place", row.getCell(1).getStringCellValue());
jsonObject.put("Animal", row.getCell(2).getStringCellValue());
data.put(jsonObject);
}
index++;
}
//print JsonArray
System.out.println(data.toString());
}
请添加您迄今为止尝试过的内容(以及缺少的逗号)@美元。我已经更新了。“一定要看一看!”希曼苏瑟尔贴出了一个答案,请看一看。
[
{
"Animal":"Cat",
"Place":"India",
"Name":"Himanshu"
},
{
"Animal":"Dog",
"Place":"London",
"Name":"Kamal"
},
{
"Animal":"Lion",
"Place":"Turkey",
"Name":"John"
}
]