最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 讀取excel文件轉(zhuǎn)換成json格式的實例代碼

 更新時間:2018年04月13日 09:04:28   作者:biubiukey  
這篇文章主要介紹了 java 讀取excel文件轉(zhuǎn)換成json格式的實例代碼,需要的朋友可以參考下

需要讀取excel數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù),寫了個測試功能,轉(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對象,實測用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ù)按需求進行格式處理   ---創(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對象,進一步把以上對象嵌套 
                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);       
      }       
  }   
} 

讀取excel 工具類,看到網(wǎng)友的讀取方法引用一下:

EXCELBean.java:

package com.siemens.util; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.DateUtil; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
public class EXCELBean { 
  //讀取excel 
  public static Workbook readExcel(String filePath){ 
    Workbook wb = null; 
    if(filePath==null){ 
      return null; 
    } 
    String extString = filePath.substring(filePath.lastIndexOf(".")); 
    InputStream is = null; 
    try { 
      is = new FileInputStream(filePath); 
      if(".xls".equals(extString)){ 
        return wb = new HSSFWorkbook(is); 
      }else if(".xlsx".equals(extString)){ 
        return wb = new XSSFWorkbook(is); 
      }else{ 
        return wb = null; 
      } 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return wb; 
  } 
  public static Object getCellFormatValue(Cell cell){ 
    Object cellValue = null; 
    if(cell!=null){ 
      //判斷cell類型 
      switch(cell.getCellType()){ 
      case Cell.CELL_TYPE_NUMERIC:{ 
        cellValue = String.valueOf(cell.getNumericCellValue()); 
        break; 
      } 
      case Cell.CELL_TYPE_FORMULA:{ 
        //判斷cell是否為日期格式 
        if(DateUtil.isCellDateFormatted(cell)){ 
          //轉(zhuǎn)換為日期格式Y(jié)YYY-mm-dd 
          cellValue = cell.getDateCellValue(); 
        }else{ 
          //數(shù)字 
          cellValue = String.valueOf(cell.getNumericCellValue()); 
        } 
        break; 
      } 
      case Cell.CELL_TYPE_STRING:{ 
        cellValue = cell.getRichStringCellValue().getString(); 
        break; 
      } 
      default: 
        cellValue = ""; 
      } 
    }else{ 
      cellValue = ""; 
    } 
    return cellValue; 
  } 
} 

總結(jié)

以上所述是小編給大家介紹的java 讀取excel文件轉(zhuǎn)換成json格式 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 使用Java后臺實現(xiàn)彈出框頁面的代碼案例

    使用Java后臺實現(xiàn)彈出框頁面的代碼案例

    在現(xiàn)代Web應(yīng)用中,彈出框(Modal)是一個常見且重要的UI組件,通過彈出框,我們可以實現(xiàn)用戶交互、表單提交、信息提示等功能,本篇博客將詳細介紹如何使用Java后臺實現(xiàn)彈出框頁面,并展示具體的代碼案例和運行效果,需要的朋友可以參考下
    2024-08-08
  • java加密枝術(shù)深入理解

    java加密枝術(shù)深入理解

    java.security包中的MessageDigest類提供了計算消息摘要的方法,本文將詳細介紹,需要了解的朋友可以參考下
    2012-11-11
  • Java使用集合實現(xiàn)斗地主分牌完整代碼

    Java使用集合實現(xiàn)斗地主分牌完整代碼

    在斗地主游戲中,通常是將一副牌平均分成3份,每份17張牌,并留3張底牌,我們可以使用集合來實現(xiàn)這一功能,這篇文章主要給大家介紹了關(guān)于Java使用集合實現(xiàn)斗地主分牌的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • 淺談java web中常用對象對應(yīng)的實例化接口

    淺談java web中常用對象對應(yīng)的實例化接口

    下面小編就為大家?guī)硪黄獪\談java web中常用對象對應(yīng)的實例化接口。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Java Filter 過濾器詳細介紹及實例代碼

    Java Filter 過濾器詳細介紹及實例代碼

    Filter也稱之為過濾器,它是Servlet技術(shù)中最實用的技術(shù),本文章WEB開發(fā)人員通過Filter技術(shù),對web服務(wù)器管理的所有web資源進行攔截,從而實現(xiàn)一些特殊的功能,本文章將向大家介紹Java 中的 Filter 過濾器,需要的朋友可以參考一下
    2016-12-12
  • java中線程掛起的幾種方式詳解

    java中線程掛起的幾種方式詳解

    這篇文章主要介紹了java中線程掛起的幾種方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Spring Boot實現(xiàn)跨域訪問實現(xiàn)代碼

    Spring Boot實現(xiàn)跨域訪問實現(xiàn)代碼

    本文通過實例代碼給大家介紹了Spring Boot實現(xiàn)跨域訪問的知識,然后在文中給大家介紹了spring boot 服務(wù)器端設(shè)置允許跨域訪問 的方法,感興趣的朋友一起看看吧
    2017-07-07
  • Maven環(huán)境安裝配置和新建項目介紹

    Maven環(huán)境安裝配置和新建項目介紹

    這篇文章介紹了Maven環(huán)境安裝配置和新建項目介紹,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • SpringBoot參數(shù)校驗及原理全面解析

    SpringBoot參數(shù)校驗及原理全面解析

    文章介紹了SpringBoot中使用@Validated和@Valid注解進行參數(shù)校驗的方法,包括基本用法和進階用法,如自定義驗證注解、多屬性聯(lián)合校驗和嵌套校驗,并簡要介紹了實現(xiàn)原理
    2024-11-11
  • 源碼分析SpringMvc日志打印被忽略輸出問題

    源碼分析SpringMvc日志打印被忽略輸出問題

    這篇文章主要為大家介紹了源碼分析SpringMvc日志打印被忽略輸出問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08

最新評論

伊吾县| 东乡| 越西县| 墨玉县| 福鼎市| 尤溪县| 合川市| 闵行区| 新干县| 阳新县| 莫力| 密云县| 桑日县| 于都县| 新和县| 乌鲁木齐市| 永泰县| 汝阳县| 岑溪市| 东城区| 通道| 屏东县| 武乡县| 读书| 霍城县| 临汾市| 沐川县| 霍山县| 芷江| 罗山县| 金溪县| 汝南县| 铁岭市| 托克逊县| 大庆市| 民和| 康定县| 罗源县| 北海市| 新竹市| 巨鹿县|