我如何获取Excel数据并将其转换为JSON [英] How can i get excel data and convert it to JSON
问题描述
我想读取excel文件(.xls,.xlsx)并将其转换为JSON格式并保存. 这是使我能够从excel文件中读取数据但无法获取如何将数据以JSON格式放入的代码. 谁能帮助我.
I want to read the excel files(.xls,.xlsx) and convert it into JSON format and save it. Here is a code which enable me to read data from the excel file but im not able to getting how to put data in JSON format. Can anyone help me out.
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();
}
}
}
推荐答案
如果要寻找简单的在线转换工具,请使用以下方法: http://www.convertcsv.com/csv-to-json.htm
If looking for a simple online conversion tool, use this: http://www.convertcsv.com/csv-to-json.htm
或者像这个家伙一样以编程方式进行操作: https://github.com/nullpunkt/excel-to-json/blob/master/src/main/java/net/nullpunkt/exceljson/convert/ExcelToJsonConverter.java
Or do it programmatically as this guy does: https://github.com/nullpunkt/excel-to-json/blob/master/src/main/java/net/nullpunkt/exceljson/convert/ExcelToJsonConverter.java
这篇关于我如何获取Excel数据并将其转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!