gpt4 book ai didi

java - 如何获取 Excel 数据并将其转换为 JSON?

转载 作者:行者123 更新时间:2023-12-01 20:20:45 27 4
gpt4 key购买 nike

我想读取excel文件(.xls,.xlsx)并将其转换为JSON格式并保存。

这是一个代码,使我能够从 Excel 文件中读取数据,但我无法了解如何将数据放入 JSON 格式。

谁能帮帮我。

public class ReadExcelFile {
private static XSSFWorkbook mybook;
static String fileLocation = "D://Traniee-SPG//Book1.xlsx";

public static void main(String[] args){
try{
File newFile = new File(fileLocation);
FileInputStream fIO = new FileInputStream(newFile);
mybook = new XSSFWorkbook(fIO); //finds the Excelfile
XSSFSheet mySheet = mybook.getSheetAt(0);// Return first sheet from the XLSX workbook
Iterator<Row> rowIterator = mySheet.iterator(); //create a cursor called iterator to all rows in sheet
Row r;
Cell c;
//to travel into the Excel spreadsheet
while(rowIterator.hasNext()) {
r = rowIterator.next();
//Cursor points to row
Iterator<Cell> cell_Iterator = r.cellIterator();
while(cell_Iterator.hasNext()) {
c = cell_Iterator.next();
//Cursor points to cell
switch (c.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(c.getStringCellValue()+"\t");
//System.out.println("Case String");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(c.getNumericCellValue()+"\t");
//System.out.println("Case number");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(c.getBooleanCellValue()+"\t");
System.out.println("Case boolean");
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print(c.getCellFormula()+"\t");
//System.out.println("Case formula");
break;
default:
}
}
System.out.println(" ");//next to display in table format
}
mybook.close();
fIO.close();
}
catch(FileNotFoundException ef){
ef.printStackTrace();
}
catch(IOException ei){
ei.printStackTrace();
}
}

}

最佳答案

关于java - 如何获取 Excel 数据并将其转换为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795100/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com