需要讀取excel數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù),寫了個測試功能,轉(zhuǎn)換正常:
專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)濱州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。JSON轉(zhuǎn)換:org.json.jar
測試類: importFile.java:
package com.siemens.util; import java.util.ArrayList; import java.util.List; import org.json.JSONException; import org.json.JSONObject; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; //import com.siemens.entity.master; //import com.siemens.service.masterService; //import com.siemens.serviceImpl.masterServiceImpl; //import com.siemens.serviceImpl.webServiceImpl; public class importFile { public static void main(String[] args) throws JSONException{ // master masters = new master(); // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // masterService ms = (masterService)ac.getBean("masterservice"); Workbook wb =null; Sheet sheet = null; Row row = null; String cellData = null; //文件路徑, String filePath = "f:/haoxy/Worktable222.xls"; wb =EXCELBean.readExcel(filePath); if(wb != null){ //用來存放表中數(shù)據(jù) List<JSONObject> listMap = new ArrayList<JSONObject>(); //獲取第一個sheet sheet = wb.getSheetAt(0); //獲取大行數(shù) int rownum = sheet.getPhysicalNumberOfRows(); //獲取第一行 row = sheet.getRow(0); //獲取大列數(shù) int colnum = row.getPhysicalNumberOfCells(); //這里創(chuàng)建json對象,實(shí)測用map的話,json數(shù)據(jù)會有問題 JSONObject jsonMap = new JSONObject(); //循環(huán)行 for (int i = 1; i < rownum; i++) { row = sheet.getRow(i); if(row !=null){ //創(chuàng)建list對象接收讀出的excel數(shù)據(jù) List<String> list = new ArrayList<String>(); //循環(huán)列 for (int j=0;j<colnum;j++){ cellData = (String) EXCELBean.getCellFormatValue(row.getCell(j)); list.add(cellData); } //System.out.println(list.get(59)); //下面具體是本人對數(shù)據(jù)按需求進(jìn)行格式處理 ---創(chuàng)建json對象會報異常,捕捉一下。 JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("skvDorCode",list.get(0)); jsonObject2.put("description", list.get(1)); jsonObject2.put("discipline", list.get(2)); jsonObject2.put("prefabricatedSkids", list.get(3)); jsonObject2.put("onRack", list.get(4)); jsonObject2.put("offRack", list.get(5)); jsonObject2.put("yard", list.get(6)); jsonObject2.put("hsrg", list.get(7)); JSONObject jsonPptData = new JSONObject(); jsonPptData.put("SC FF",list.get(8)); jsonPptData.put("CC SS CT",list.get(9)); jsonPptData.put("CC SS OCC",list.get(10)); jsonPptData.put("CC SS ACC",list.get(11)); jsonPptData.put("CC MS CT",list.get(12)); jsonPptData.put("CC MS OCC",list.get(13)); jsonPptData.put("CC MS ACC",list.get(14)); //turnkey JSONObject jsonTurnkey = new JSONObject(); jsonTurnkey.put("plantDesign", list.get(26)); jsonTurnkey.put("basicDesign", list.get(27)); jsonTurnkey.put("detailDesign", list.get(28)); jsonTurnkey.put("supplier", list.get(29)); jsonTurnkey.put("errection", list.get(30)); jsonTurnkey.put("commissioning", list.get(31)); jsonTurnkey.put("blackBox", list.get(32)); jsonTurnkey.put("optionalScope", list.get(33)); jsonTurnkey.put("remark", list.get(34)); jsonTurnkey.put("internalRemark", list.get(35)); jsonTurnkey.put("revision", list.get(36)); //PowerCore JSONObject jsonPowerCore = new JSONObject(); jsonPowerCore.put("plantDesign", list.get(37)); jsonPowerCore.put("basicDesign", list.get(38)); jsonPowerCore.put("detailDesign", list.get(39)); jsonPowerCore.put("supplier", list.get(40)); jsonPowerCore.put("errection", list.get(41)); jsonPowerCore.put("commissioning", list.get(42)); jsonPowerCore.put("blackBox", list.get(43)); jsonPowerCore.put("optionalScope", list.get(44)); jsonPowerCore.put("remark", list.get(45)); jsonPowerCore.put("internalRemark", list.get(46)); jsonPowerCore.put("revision", list.get(47)); //PowerIsland JSONObject jsonPowerIsland = new JSONObject(); jsonPowerIsland.put("plantDesign", list.get(48)); jsonPowerIsland.put("basicDesign", list.get(49)); jsonPowerIsland.put("detailDesign", list.get(50)); jsonPowerIsland.put("supplier", list.get(51)); jsonPowerIsland.put("errection", list.get(52)); jsonPowerIsland.put("commissioning", list.get(53)); jsonPowerIsland.put("blackBox", list.get(54)); jsonPowerIsland.put("optionalScope", list.get(55)); jsonPowerIsland.put("remark", list.get(56)); jsonPowerIsland.put("internalRemark", list.get(57)); jsonPowerIsland.put("revision", list.get(58)); //創(chuàng)建jsonBmt對象,進(jìn)一步把以上對象嵌套 JSONObject jsonBmt = new JSONObject(); jsonBmt.put("Turnkey", jsonTurnkey); jsonBmt.put("PowerCore", jsonPowerCore); jsonBmt.put("PowerIsland", jsonPowerIsland); //把以上幾個嵌套入第一層對象中 jsonObject2.put("powerPlantTypes",jsonPptData); jsonObject2.put("businessMixTypes",jsonBmt); jsonObject2.put("treeDepth",Integer.parseInt(list.get(59).substring(0,list.get(59).indexOf('.')))); if(Integer.parseInt(list.get(59).substring(0,list.get(59).indexOf('.')))<=2){ List<String> list3 = new ArrayList<String>(); list3.add("non-empty-placeholder"); jsonObject2.put("children",list3); } listMap.add(jsonObject2); }else{ break; } }// end for row //最外層加個key-gridData jsonMap.put("gridData", listMap); System.out.println(jsonMap); } } }
網(wǎng)站名稱:java讀取excel文件轉(zhuǎn)換成json格式的實(shí)例代碼-創(chuàng)新互聯(lián)
鏈接地址:http://vcdvsql.cn/article20/dgoico.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、小程序開發(fā)、服務(wù)器托管、網(wǎng)頁設(shè)計公司、網(wǎng)站維護(hù)、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容