1. 程式人生 > >Java操作json資料匯入Excel

Java操作json資料匯入Excel

話不多說,直接上程式碼

package com.linkage.bss.crm.commons;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ImportExcel {
    public static void main(String[] args) {
        try{
            String filePath="F:/test.xlsx";
            //讀取excel檔案
            InputStream is = new FileInputStream(filePath);
            //建立excel工作薄
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            //建立一個工作表sheet
            XSSFSheet sheet = workbook.getSheetAt(0);

            //讀取json資料並解析
            JSONArray jsonData = getJsonData();
            int colunm = 1;//行數計算器,從第二行開始寫入
            for(int i=0;i<jsonData.size();i++) {
                JSONObject firstModularObject = jsonData.getJSONObject(i);
                String firstModularName=firstModularObject.get("NAME").toString();
                JSONArray secondModular=(JSONArray)firstModularObject.get("children");
                if(secondModular.size()>0){
                    //第二級目錄
                    for(int l=0;l<secondModular.size();l++) {
                        JSONObject secondModularJSONObject = secondModular.getJSONObject(l);
                        String secondModularName=secondModularJSONObject.get("NAME").toString();
                        JSONArray thirdModular=(JSONArray)secondModularJSONObject.get("children");
                        if(thirdModular.size()>0){
                            //第三級目錄
                            for(int j=0;j<thirdModular.size();j++) {
                                JSONObject thirdModularJSONObject = thirdModular.getJSONObject(j);
                                String thirdModularName=thirdModularJSONObject.get("NAME").toString();
                                JSONArray fourthModular=(JSONArray)thirdModularJSONObject.get("children");
                                if(fourthModular.size()>0){
                                    //第四級目錄
                                    for(int h=0;h<fourthModular.size();h++) {
                                        JSONObject fourthModularJSONObject = fourthModular.getJSONObject(h);
                                        String fourthModularName = fourthModularJSONObject.get("NAME").toString();
                                        String fourthModularURL=fourthModularJSONObject.get("URI").toString();
                                        String fourthModularId=fourthModularJSONObject.get("SYS_MODULAR_ID").toString();

                                        XSSFRow row = sheet.createRow(colunm);
                                        XSSFCell cell_firstName = row.createCell(0);
                                        cell_firstName.setCellValue(firstModularName);
                                        XSSFCell cell_secondName = row.createCell(1);
                                        cell_secondName.setCellValue(secondModularName);
                                        XSSFCell cell_thirdName = row.createCell(2);
                                        cell_thirdName.setCellValue(thirdModularName);
                                        XSSFCell cell_fourthName = row.createCell(3);
                                        cell_fourthName.setCellValue(fourthModularName);
                                        XSSFCell cell_ModularId = row.createCell(4);
                                        cell_ModularId.setCellValue(fourthModularId);
                                        XSSFCell cell_URL = row.createCell(5);
                                        cell_URL.setCellValue(fourthModularURL);

                                        colunm++;
                                    }
                                }else{
                                    String thirdModularURL=thirdModularJSONObject.get("URI").toString();
                                    String thirdModularId=thirdModularJSONObject.get("SYS_MODULAR_ID").toString();

                                    XSSFRow row = sheet.createRow(colunm);
                                    XSSFCell cell_firstName = row.createCell(0);
                                    cell_firstName.setCellValue(firstModularName);
                                    XSSFCell cell_secondName = row.createCell(1);
                                    cell_secondName.setCellValue(secondModularName);
                                    XSSFCell cell_thirdName = row.createCell(2);
                                    cell_thirdName.setCellValue(thirdModularName);
                                    XSSFCell cell_ModularId = row.createCell(4);
                                    cell_ModularId.setCellValue(thirdModularId);
                                    XSSFCell cell_URL = row.createCell(5);
                                    cell_URL.setCellValue(thirdModularURL);

                                    colunm++;
                                }
                            }
                        }else{
                            String secondModularURL=secondModularJSONObject.get("URI").toString();
                            String secondModularId=secondModularJSONObject.get("SYS_MODULAR_ID").toString();

                            XSSFRow row = sheet.createRow(colunm);
                            XSSFCell cell_firstName = row.createCell(0);
                            cell_firstName.setCellValue(firstModularName);
                            XSSFCell cell_secondName = row.createCell(1);
                            cell_secondName.setCellValue(secondModularName);
                            XSSFCell cell_ModularId = row.createCell(4);
                            cell_ModularId.setCellValue(secondModularId);
                            XSSFCell cell_URL = row.createCell(5);
                            cell_URL.setCellValue(secondModularURL);

                            colunm++;
                        }
                    }
                }else{
                    String firstModularURL=firstModularObject.get("URI").toString();
                    String firstModularId=firstModularObject.get("SYS_MODULAR_ID").toString();

                    XSSFRow row = sheet.createRow(colunm);
                    XSSFCell cell_firstName = row.createCell(0);
                    cell_firstName.setCellValue(firstModularName);
                    XSSFCell cell_firstModularId = row.createCell(4);
                    cell_firstModularId.setCellValue(firstModularId);
                    XSSFCell cell_firstURL = row.createCell(5);
                    cell_firstURL.setCellValue(firstModularURL);

                    colunm++;
                }
            }//end for
            //儲存到excel
            FileOutputStream outputStream = new FileOutputStream(filePath);
            workbook.write(outputStream);
            System.out.println("寫入成功");
            outputStream.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 讀取json資料並解析
     * @return
     * @throws IOException
     */
    public static JSONArray getJsonData() throws IOException {
        String filePath="F:/jsonData.json";
        File file = new File(filePath);
        JSONArray jsonArray = null;
        try {
           String input = FileUtils.readFileToString(file, "UTF-8");
            JSONObject jsonObject = JSONObject.fromObject(input);
            if (jsonObject != null) {
             jsonArray = jsonObject.getJSONArray("list");
            }
        } catch (Exception e) {
              e.printStackTrace();
              jsonArray = null;
        }
        return jsonArray;
    }
}

json測試資料

{
	"list": [{
		"SYS_MODULAR_ID": 999,
		"NAME": "調查問卷",
		"MODULAR_TYPE_CD": 1,
		"SYS_SYS_MODULAR_ID": null,
		"URI": "www.baidu.com",
		"children": []
	},
	{
		"SYS_MODULAR_ID": 1000,
		"NAME": "資源管理",
		"MODULAR_TYPE_CD": 2,
		"SYS_SYS_MODULAR_ID": null,
		"URI": " ",
		"children": [{
			"SYS_MODULAR_ID": 1001,
			"NAME": "地址資源",
			"MODULAR_TYPE_CD": 2,
			"SYS_SYS_MODULAR_ID": 1000,
			"URI": " ",
			"children": [{
				"SYS_MODULAR_ID": 1011,
				"NAME": "標準地址",
				"MODULAR_TYPE_CD": 2,
				"SYS_SYS_MODULAR_ID": 1001,
				"URI": "www.taobao.com",
				"children": []
			}]
		}]
	}]
}