java 读取excel文件转换成json格式的实例代码
需要读取excel数据转换成json数据,写了个测试功能,转换正常:
JSON转换:org.json.jar
测试类: importFile.java:
packagecom.siemens.util; importjava.util.ArrayList; importjava.util.List; importorg.json.JSONException; importorg.json.JSONObject; importorg.apache.poi.ss.usermodel.Row; importorg.apache.poi.ss.usermodel.Sheet; importorg.apache.poi.ss.usermodel.Workbook; //importcom.siemens.entity.master; //importcom.siemens.service.masterService; //importcom.siemens.serviceImpl.masterServiceImpl; //importcom.siemens.serviceImpl.webServiceImpl; publicclassimportFile{ publicstaticvoidmain(String[]args)throwsJSONException{ //mastermasters=newmaster(); //ApplicationContextac=newClassPathXmlApplicationContext("applicationContext.xml"); //masterServicems=(masterService)ac.getBean("masterservice"); Workbookwb=null; Sheetsheet=null; Rowrow=null; StringcellData=null; //文件路径, StringfilePath="f:/haoxy/Worktable222.xls"; wb=EXCELBean.readExcel(filePath); if(wb!=null){ //用来存放表中数据 ListlistMap=newArrayList (); //获取第一个sheet sheet=wb.getSheetAt(0); //获取最大行数 intrownum=sheet.getPhysicalNumberOfRows(); //获取第一行 row=sheet.getRow(0); //获取最大列数 intcolnum=row.getPhysicalNumberOfCells(); //这里创建json对象,实测用map的话,json数据会有问题 JSONObjectjsonMap=newJSONObject(); //循环行 for(inti=1;i list=newArrayList (); //循环列 for(intj=0;j list3=newArrayList (); list3.add("non-empty-placeholder"); jsonObject2.put("children",list3); } listMap.add(jsonObject2); }else{ break; } }//endforrow //最外层加个key-gridData jsonMap.put("gridData",listMap); System.out.println(jsonMap); } } }
读取excel工具类,看到网友的读取方法引用一下:
EXCELBean.java:
packagecom.siemens.util; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.io.InputStream; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; importorg.apache.poi.ss.usermodel.Cell; importorg.apache.poi.ss.usermodel.DateUtil; importorg.apache.poi.ss.usermodel.Workbook; importorg.apache.poi.xssf.usermodel.XSSFWorkbook; publicclassEXCELBean{ //读取excel publicstaticWorkbookreadExcel(StringfilePath){ Workbookwb=null; if(filePath==null){ returnnull; } StringextString=filePath.substring(filePath.lastIndexOf(".")); InputStreamis=null; try{ is=newFileInputStream(filePath); if(".xls".equals(extString)){ returnwb=newHSSFWorkbook(is); }elseif(".xlsx".equals(extString)){ returnwb=newXSSFWorkbook(is); }else{ returnwb=null; } }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } returnwb; } publicstaticObjectgetCellFormatValue(Cellcell){ ObjectcellValue=null; if(cell!=null){ //判断cell类型 switch(cell.getCellType()){ caseCell.CELL_TYPE_NUMERIC:{ cellValue=String.valueOf(cell.getNumericCellValue()); break; } caseCell.CELL_TYPE_FORMULA:{ //判断cell是否为日期格式 if(DateUtil.isCellDateFormatted(cell)){ //转换为日期格式YYYY-mm-dd cellValue=cell.getDateCellValue(); }else{ //数字 cellValue=String.valueOf(cell.getNumericCellValue()); } break; } caseCell.CELL_TYPE_STRING:{ cellValue=cell.getRichStringCellValue().getString(); break; } default: cellValue=""; } }else{ cellValue=""; } returncellValue; } }
总结
以上所述是小编给大家介绍的java读取excel文件转换成json格式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!