话不多说,直接上代码
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": []
}]
}]
}]
}