测试单元

package com.paf.backend.jsonexcel;

import com.paf.common.sequence.IdSequenceFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.;
import java.util.
;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.xssf.usermodel.*;

/**

  • @PACKAGE com.paf.backend.jsonexcel

  • @AUTHOR tan_yh

  • @Description

  • @Date 2020/5/5 11:19

  • @Version 1.0
    */
    public class JsonToExcel {

    public static void start(String [] args){
    readFile();
    }

    public static void readFile(){
    Vector ver=new Vector(); //用做堆栈;
    ver.add(“F:/temp/json/”);
    int num = 0;
    Map<String, HSSFWorkbook> workbookMap= new HashMap<>();
    Map<String, String> fileMap= new HashMap<>();
    try {
    while(ver.size()>0){
    File[] files = new File(ver.get(0).toString()).listFiles(); //获取该文件夹下所有的文件(夹)名
    ver.remove(0);
    int len=files.length;
    for(int i=0;i<len;i++){
    String filePath=files[i].getAbsolutePath();
    if(files[i].isDirectory()) { //如果是目录,则加入队列。以便进行后续处理
    if (num == 1){
    workbookMap.put(files[i].getAbsolutePath(), new HSSFWorkbook());
    fileMap.put(files[i].getAbsolutePath(), (files[i].getAbsolutePath()+"/a.xls"));
    }
    ver.add(filePath);

                 }else {
    
                     String fileType = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    
                     String fileName = filePath.substring(0, filePath.lastIndexOf("."));
                     HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
                     if (fileType.indexOf("json") > -1) {
                         for (String key :workbookMap.keySet()) {
                             if (filePath.contains(key)){
                                 fileName= fileMap.get(key);
                                 hssfWorkbook= workbookMap.get(key);
                             }
                         }
    
                         jsonFile(new File(filePath), fileName, hssfWorkbook);        //如果是文件,则直接输出文件名到指定的文件。
                     }
                 }
             }
             num = 1;
         }
     }catch (Exception e){
         e.printStackTrace();
     }
    

    }
    public static void jsonFile(File jsonFile, String fileName, HSSFWorkbook hssfWorkbook){
    try {
    FileReader reader = new FileReader(jsonFile);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = null;
    StringBuffer stringBuffer = new StringBuffer();
    while ((line= bufferedReader.readLine()) != null){
    stringBuffer.append(line);
    }
    JSONArray jsonArray = null;
    JSONObject labelObject = null;
    String cnLabel = “sheet”;
    JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
    if (jsonObject != null && jsonObject.containsKey(“properties”)) {
    jsonArray = jsonObject.getJSONArray(“properties”);
    labelObject = jsonObject.getJSONObject(“label”);
    if (labelObject.containsKey(“cn”)){
    cnLabel = labelObject.getString(“cn”);
    }
    }else{
    return;
    }
    // fileName = jsonFile.getOriginalFilename();
    // String filetype = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    if (jsonArray == null) return;
    FileOutputStream xlsStream = new FileOutputStream(fileName);
    build(xlsStream, jsonArray, new String[]{“dataType”,“hasSourceId”,“cardinality”,“en”,“cn”}, cnLabel, hssfWorkbook);
    xlsStream.close();
    }catch (Exception e){
    System.out.printf("===="+fileName);
    e.printStackTrace();
    }

    }
    public static void build(OutputStream output, JSONArray json, String[] properties, String cnLabel, HSSFWorkbook hssfWorkbook) {
    build(output, json, properties, properties, cnLabel, hssfWorkbook);
    }

    public static void build(OutputStream output, JSONArray array, String[] properties, String[] columnsNames, String cnLabel, HSSFWorkbook workbook) {
    try {
    // //读取excel文件
    // InputStream is = new FileInputStream(fileName);
    // //创建excel工作薄
    // XSSFWorkbook workbook = new XSSFWorkbook();
    // //创建一个工作表sheet
    // XSSFSheet sheet = workbook.getSheetAt(0);
    // XSSFRow header = sheet.createRow(0);

// HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(cnLabel);

    HSSFRow header = sheet.createRow(0);

    int e;
    for(e = 0; e < columnsNames.length; ++e) {
        String object = columnsNames[e];
        HSSFCell row = header.createCell(e);
        HSSFRichTextString j = new HSSFRichTextString(object);
        row.setCellValue(j);
    }

    for(e = 0; e < array.size(); ++e) {
        JSONObject var16 = array.getJSONObject(e);
        HSSFRow var17 = sheet.createRow(e + 1);

        for(int var18 = 0; var18 < properties.length; ++var18) {
            String string = properties[var18];
            HSSFCell cell = var17.createCell(var18);
            try {
                if (var16 == null) continue;
                if (var16.get(string) == null) continue;
                 String text = var16.get(string).toString();

// HSSFRichTextString text = new HSSFRichTextString(var16.get(string).toString());
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
// HSSFRichTextString text = new HSSFRichTextString(var16.getDouble(string) + “”);
// cell.setCellValue(text);
}

        }
    }
        workbook.write(output);

    } catch (Exception var15) {
        var15.printStackTrace();
    }
}

}
package com.paf.backend.jsonexcel;

import com.paf.common.sequence.IdSequenceFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.;
import java.util.
;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.xssf.usermodel.*;

/**

  • @PACKAGE com.paf.backend.jsonexcel

  • @AUTHOR tan_yh

  • @Description

  • @Date 2020/5/5 11:19

  • @Version 1.0
    */
    public class JsonToExcel {

    public static void start(String [] args){
    readFile();
    }

    public static void readFile(){
    Vector ver=new Vector(); //用做堆栈;
    ver.add(“F:/temp/json/”);
    int num = 0;
    Map<String, HSSFWorkbook> workbookMap= new HashMap<>();
    Map<String, String> fileMap= new HashMap<>();
    try {
    while(ver.size()>0){
    File[] files = new File(ver.get(0).toString()).listFiles(); //获取该文件夹下所有的文件(夹)名
    ver.remove(0);
    int len=files.length;
    for(int i=0;i<len;i++){
    String filePath=files[i].getAbsolutePath();
    if(files[i].isDirectory()) { //如果是目录,则加入队列。以便进行后续处理
    if (num == 1){
    workbookMap.put(files[i].getAbsolutePath(), new HSSFWorkbook());
    fileMap.put(files[i].getAbsolutePath(), (files[i].getAbsolutePath()+"/a.xls"));
    }
    ver.add(filePath);

                 }else {
    
                     String fileType = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    
                     String fileName = filePath.substring(0, filePath.lastIndexOf("."));
                     HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
                     if (fileType.indexOf("json") > -1) {
                         for (String key :workbookMap.keySet()) {
                             if (filePath.contains(key)){
                                 fileName= fileMap.get(key);
                                 hssfWorkbook= workbookMap.get(key);
                             }
                         }
    
                         jsonFile(new File(filePath), fileName, hssfWorkbook);        //如果是文件,则直接输出文件名到指定的文件。
                     }
                 }
             }
             num = 1;
         }
     }catch (Exception e){
         e.printStackTrace();
     }
    

    }
    public static void jsonFile(File jsonFile, String fileName, HSSFWorkbook hssfWorkbook){
    try {
    FileReader reader = new FileReader(jsonFile);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = null;
    StringBuffer stringBuffer = new StringBuffer();
    while ((line= bufferedReader.readLine()) != null){
    stringBuffer.append(line);
    }
    JSONArray jsonArray = null;
    JSONObject labelObject = null;
    String cnLabel = “sheet”;
    JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
    if (jsonObject != null && jsonObject.containsKey(“properties”)) {
    jsonArray = jsonObject.getJSONArray(“properties”);
    labelObject = jsonObject.getJSONObject(“label”);
    if (labelObject.containsKey(“cn”)){
    cnLabel = labelObject.getString(“cn”);
    }
    }else{
    return;
    }
    // fileName = jsonFile.getOriginalFilename();
    // String filetype = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    if (jsonArray == null) return;
    FileOutputStream xlsStream = new FileOutputStream(fileName);
    build(xlsStream, jsonArray, new String[]{“dataType”,“hasSourceId”,“cardinality”,“en”,“cn”}, cnLabel, hssfWorkbook);
    xlsStream.close();
    }catch (Exception e){
    System.out.printf("===="+fileName);
    e.printStackTrace();
    }

    }
    public static void build(OutputStream output, JSONArray json, String[] properties, String cnLabel, HSSFWorkbook hssfWorkbook) {
    build(output, json, properties, properties, cnLabel, hssfWorkbook);
    }

    public static void build(OutputStream output, JSONArray array, String[] properties, String[] columnsNames, String cnLabel, HSSFWorkbook workbook) {
    try {
    // //读取excel文件
    // InputStream is = new FileInputStream(fileName);
    // //创建excel工作薄
    // XSSFWorkbook workbook = new XSSFWorkbook();
    // //创建一个工作表sheet
    // XSSFSheet sheet = workbook.getSheetAt(0);
    // XSSFRow header = sheet.createRow(0);

// HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(cnLabel);

    HSSFRow header = sheet.createRow(0);

    int e;
    for(e = 0; e < columnsNames.length; ++e) {
        String object = columnsNames[e];
        HSSFCell row = header.createCell(e);
        HSSFRichTextString j = new HSSFRichTextString(object);
        row.setCellValue(j);
    }

    for(e = 0; e < array.size(); ++e) {
        JSONObject var16 = array.getJSONObject(e);
        HSSFRow var17 = sheet.createRow(e + 1);

        for(int var18 = 0; var18 < properties.length; ++var18) {
            String string = properties[var18];
            HSSFCell cell = var17.createCell(var18);
            try {
                if (var16 == null) continue;
                if (var16.get(string) == null) continue;
                 String text = var16.get(string).toString();

// HSSFRichTextString text = new HSSFRichTextString(var16.get(string).toString());
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
// HSSFRichTextString text = new HSSFRichTextString(var16.getDouble(string) + “”);
// cell.setCellValue(text);
}

        }
    }
        workbook.write(output);

    } catch (Exception var15) {
        var15.printStackTrace();
    }
}

}
package com.paf.backend.jsonexcel;

import com.paf.common.sequence.IdSequenceFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.;
import java.util.
;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.xssf.usermodel.*;

/**

  • @PACKAGE com.paf.backend.jsonexcel

  • @AUTHOR tan_yh

  • @Description

  • @Date 2020/5/5 11:19

  • @Version 1.0
    */
    public class JsonToExcel {

    public static void start(String [] args){
    readFile();
    }

    public static void readFile(){
    Vector ver=new Vector(); //用做堆栈;
    ver.add(“F:/temp/json/”);
    int num = 0;
    Map<String, HSSFWorkbook> workbookMap= new HashMap<>();
    Map<String, String> fileMap= new HashMap<>();
    try {
    while(ver.size()>0){
    File[] files = new File(ver.get(0).toString()).listFiles(); //获取该文件夹下所有的文件(夹)名
    ver.remove(0);
    int len=files.length;
    for(int i=0;i<len;i++){
    String filePath=files[i].getAbsolutePath();
    if(files[i].isDirectory()) { //如果是目录,则加入队列。以便进行后续处理
    if (num == 1){
    workbookMap.put(files[i].getAbsolutePath(), new HSSFWorkbook());
    fileMap.put(files[i].getAbsolutePath(), (files[i].getAbsolutePath()+"/a.xls"));
    }
    ver.add(filePath);

                 }else {
    
                     String fileType = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    
                     String fileName = filePath.substring(0, filePath.lastIndexOf("."));
                     HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
                     if (fileType.indexOf("json") > -1) {
                         for (String key :workbookMap.keySet()) {
                             if (filePath.contains(key)){
                                 fileName= fileMap.get(key);
                                 hssfWorkbook= workbookMap.get(key);
                             }
                         }
    
                         jsonFile(new File(filePath), fileName, hssfWorkbook);        //如果是文件,则直接输出文件名到指定的文件。
                     }
                 }
             }
             num = 1;
         }
     }catch (Exception e){
         e.printStackTrace();
     }
    

    }
    public static void jsonFile(File jsonFile, String fileName, HSSFWorkbook hssfWorkbook){
    try {
    FileReader reader = new FileReader(jsonFile);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = null;
    StringBuffer stringBuffer = new StringBuffer();
    while ((line= bufferedReader.readLine()) != null){
    stringBuffer.append(line);
    }
    JSONArray jsonArray = null;
    JSONObject labelObject = null;
    String cnLabel = “sheet”;
    JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
    if (jsonObject != null && jsonObject.containsKey(“properties”)) {
    jsonArray = jsonObject.getJSONArray(“properties”);
    labelObject = jsonObject.getJSONObject(“label”);
    if (labelObject.containsKey(“cn”)){
    cnLabel = labelObject.getString(“cn”);
    }
    }else{
    return;
    }
    // fileName = jsonFile.getOriginalFilename();
    // String filetype = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    if (jsonArray == null) return;
    FileOutputStream xlsStream = new FileOutputStream(fileName);
    build(xlsStream, jsonArray, new String[]{“dataType”,“hasSourceId”,“cardinality”,“en”,“cn”}, cnLabel, hssfWorkbook);
    xlsStream.close();
    }catch (Exception e){
    System.out.printf("===="+fileName);
    e.printStackTrace();
    }

    }
    public static void build(OutputStream output, JSONArray json, String[] properties, String cnLabel, HSSFWorkbook hssfWorkbook) {
    build(output, json, properties, properties, cnLabel, hssfWorkbook);
    }

    public static void build(OutputStream output, JSONArray array, String[] properties, String[] columnsNames, String cnLabel, HSSFWorkbook workbook) {
    try {
    // //读取excel文件
    // InputStream is = new FileInputStream(fileName);
    // //创建excel工作薄
    // XSSFWorkbook workbook = new XSSFWorkbook();
    // //创建一个工作表sheet
    // XSSFSheet sheet = workbook.getSheetAt(0);
    // XSSFRow header = sheet.createRow(0);

// HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(cnLabel);

    HSSFRow header = sheet.createRow(0);

    int e;
    for(e = 0; e < columnsNames.length; ++e) {
        String object = columnsNames[e];
        HSSFCell row = header.createCell(e);
        HSSFRichTextString j = new HSSFRichTextString(object);
        row.setCellValue(j);
    }

    for(e = 0; e < array.size(); ++e) {
        JSONObject var16 = array.getJSONObject(e);
        HSSFRow var17 = sheet.createRow(e + 1);

        for(int var18 = 0; var18 < properties.length; ++var18) {
            String string = properties[var18];
            HSSFCell cell = var17.createCell(var18);
            try {
                if (var16 == null) continue;
                if (var16.get(string) == null) continue;
                 String text = var16.get(string).toString();

// HSSFRichTextString text = new HSSFRichTextString(var16.get(string).toString());
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
// HSSFRichTextString text = new HSSFRichTextString(var16.getDouble(string) + “”);
// cell.setCellValue(text);
}

        }
    }
        workbook.write(output);

    } catch (Exception var15) {
        var15.printStackTrace();
    }
}

}
package com.paf.backend.jsonexcel;

import com.paf.common.sequence.IdSequenceFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.;
import java.util.
;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.xssf.usermodel.*;

/**

  • @PACKAGE com.paf.backend.jsonexcel

  • @AUTHOR tan_yh

  • @Description

  • @Date 2020/5/5 11:19

  • @Version 1.0
    */
    public class JsonToExcel {

    public static void start(String [] args){
    readFile();
    }

    public static void readFile(){
    Vector ver=new Vector(); //用做堆栈;
    ver.add(“F:/temp/json/”);
    int num = 0;
    Map<String, HSSFWorkbook> workbookMap= new HashMap<>();
    Map<String, String> fileMap= new HashMap<>();
    try {
    while(ver.size()>0){
    File[] files = new File(ver.get(0).toString()).listFiles(); //获取该文件夹下所有的文件(夹)名
    ver.remove(0);
    int len=files.length;
    for(int i=0;i<len;i++){
    String filePath=files[i].getAbsolutePath();
    if(files[i].isDirectory()) { //如果是目录,则加入队列。以便进行后续处理
    if (num == 1){
    workbookMap.put(files[i].getAbsolutePath(), new HSSFWorkbook());
    fileMap.put(files[i].getAbsolutePath(), (files[i].getAbsolutePath()+"/a.xls"));
    }
    ver.add(filePath);

                 }else {
    
                     String fileType = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    
                     String fileName = filePath.substring(0, filePath.lastIndexOf("."));
                     HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
                     if (fileType.indexOf("json") > -1) {
                         for (String key :workbookMap.keySet()) {
                             if (filePath.contains(key)){
                                 fileName= fileMap.get(key);
                                 hssfWorkbook= workbookMap.get(key);
                             }
                         }
    
                         jsonFile(new File(filePath), fileName, hssfWorkbook);        //如果是文件,则直接输出文件名到指定的文件。
                     }
                 }
             }
             num = 1;
         }
     }catch (Exception e){
         e.printStackTrace();
     }
    

    }
    public static void jsonFile(File jsonFile, String fileName, HSSFWorkbook hssfWorkbook){
    try {
    FileReader reader = new FileReader(jsonFile);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = null;
    StringBuffer stringBuffer = new StringBuffer();
    while ((line= bufferedReader.readLine()) != null){
    stringBuffer.append(line);
    }
    JSONArray jsonArray = null;
    JSONObject labelObject = null;
    String cnLabel = “sheet”;
    JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
    if (jsonObject != null && jsonObject.containsKey(“properties”)) {
    jsonArray = jsonObject.getJSONArray(“properties”);
    labelObject = jsonObject.getJSONObject(“label”);
    if (labelObject.containsKey(“cn”)){
    cnLabel = labelObject.getString(“cn”);
    }
    }else{
    return;
    }
    // fileName = jsonFile.getOriginalFilename();
    // String filetype = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    if (jsonArray == null) return;
    FileOutputStream xlsStream = new FileOutputStream(fileName);
    build(xlsStream, jsonArray, new String[]{“dataType”,“hasSourceId”,“cardinality”,“en”,“cn”}, cnLabel, hssfWorkbook);
    xlsStream.close();
    }catch (Exception e){
    System.out.printf("===="+fileName);
    e.printStackTrace();
    }

    }
    public static void build(OutputStream output, JSONArray json, String[] properties, String cnLabel, HSSFWorkbook hssfWorkbook) {
    build(output, json, properties, properties, cnLabel, hssfWorkbook);
    }

    public static void build(OutputStream output, JSONArray array, String[] properties, String[] columnsNames, String cnLabel, HSSFWorkbook workbook) {
    try {
    // //读取excel文件
    // InputStream is = new FileInputStream(fileName);
    // //创建excel工作薄
    // XSSFWorkbook workbook = new XSSFWorkbook();
    // //创建一个工作表sheet
    // XSSFSheet sheet = workbook.getSheetAt(0);
    // XSSFRow header = sheet.createRow(0);

// HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(cnLabel);

    HSSFRow header = sheet.createRow(0);

    int e;
    for(e = 0; e < columnsNames.length; ++e) {
        String object = columnsNames[e];
        HSSFCell row = header.createCell(e);
        HSSFRichTextString j = new HSSFRichTextString(object);
        row.setCellValue(j);
    }

    for(e = 0; e < array.size(); ++e) {
        JSONObject var16 = array.getJSONObject(e);
        HSSFRow var17 = sheet.createRow(e + 1);

        for(int var18 = 0; var18 < properties.length; ++var18) {
            String string = properties[var18];
            HSSFCell cell = var17.createCell(var18);
            try {
                if (var16 == null) continue;
                if (var16.get(string) == null) continue;
                 String text = var16.get(string).toString();

// HSSFRichTextString text = new HSSFRichTextString(var16.get(string).toString());
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
// HSSFRichTextString text = new HSSFRichTextString(var16.getDouble(string) + “”);
// cell.setCellValue(text);
}

        }
    }
        workbook.write(output);

    } catch (Exception var15) {
        var15.printStackTrace();
    }
}

}
package com.paf.backend.jsonexcel;

import com.paf.common.sequence.IdSequenceFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.;
import java.util.
;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.xssf.usermodel.*;

/**

  • @PACKAGE com.paf.backend.jsonexcel

  • @AUTHOR tan_yh

  • @Description

  • @Date 2020/5/5 11:19

  • @Version 1.0
    */
    public class JsonToExcel {

    public static void start(String [] args){
    readFile();
    }

    public static void readFile(){
    Vector ver=new Vector(); //用做堆栈;
    ver.add(“F:/temp/json/”);
    int num = 0;
    Map<String, HSSFWorkbook> workbookMap= new HashMap<>();
    Map<String, String> fileMap= new HashMap<>();
    try {
    while(ver.size()>0){
    File[] files = new File(ver.get(0).toString()).listFiles(); //获取该文件夹下所有的文件(夹)名
    ver.remove(0);
    int len=files.length;
    for(int i=0;i<len;i++){
    String filePath=files[i].getAbsolutePath();
    if(files[i].isDirectory()) { //如果是目录,则加入队列。以便进行后续处理
    if (num == 1){
    workbookMap.put(files[i].getAbsolutePath(), new HSSFWorkbook());
    fileMap.put(files[i].getAbsolutePath(), (files[i].getAbsolutePath()+"/a.xls"));
    }
    ver.add(filePath);

                 }else {
    
                     String fileType = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    
                     String fileName = filePath.substring(0, filePath.lastIndexOf("."));
                     HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
                     if (fileType.indexOf("json") > -1) {
                         for (String key :workbookMap.keySet()) {
                             if (filePath.contains(key)){
                                 fileName= fileMap.get(key);
                                 hssfWorkbook= workbookMap.get(key);
                             }
                         }
    
                         jsonFile(new File(filePath), fileName, hssfWorkbook);        //如果是文件,则直接输出文件名到指定的文件。
                     }
                 }
             }
             num = 1;
         }
     }catch (Exception e){
         e.printStackTrace();
     }
    

    }
    public static void jsonFile(File jsonFile, String fileName, HSSFWorkbook hssfWorkbook){
    try {
    FileReader reader = new FileReader(jsonFile);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = null;
    StringBuffer stringBuffer = new StringBuffer();
    while ((line= bufferedReader.readLine()) != null){
    stringBuffer.append(line);
    }
    JSONArray jsonArray = null;
    JSONObject labelObject = null;
    String cnLabel = “sheet”;
    JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
    if (jsonObject != null && jsonObject.containsKey(“properties”)) {
    jsonArray = jsonObject.getJSONArray(“properties”);
    labelObject = jsonObject.getJSONObject(“label”);
    if (labelObject.containsKey(“cn”)){
    cnLabel = labelObject.getString(“cn”);
    }
    }else{
    return;
    }
    // fileName = jsonFile.getOriginalFilename();
    // String filetype = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    if (jsonArray == null) return;
    FileOutputStream xlsStream = new FileOutputStream(fileName);
    build(xlsStream, jsonArray, new String[]{“dataType”,“hasSourceId”,“cardinality”,“en”,“cn”}, cnLabel, hssfWorkbook);
    xlsStream.close();
    }catch (Exception e){
    System.out.printf("===="+fileName);
    e.printStackTrace();
    }

    }
    public static void build(OutputStream output, JSONArray json, String[] properties, String cnLabel, HSSFWorkbook hssfWorkbook) {
    build(output, json, properties, properties, cnLabel, hssfWorkbook);
    }

    public static void build(OutputStream output, JSONArray array, String[] properties, String[] columnsNames, String cnLabel, HSSFWorkbook workbook) {
    try {
    // //读取excel文件
    // InputStream is = new FileInputStream(fileName);
    // //创建excel工作薄
    // XSSFWorkbook workbook = new XSSFWorkbook();
    // //创建一个工作表sheet
    // XSSFSheet sheet = workbook.getSheetAt(0);
    // XSSFRow header = sheet.createRow(0);

// HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(cnLabel);

    HSSFRow header = sheet.createRow(0);

    int e;
    for(e = 0; e < columnsNames.length; ++e) {
        String object = columnsNames[e];
        HSSFCell row = header.createCell(e);
        HSSFRichTextString j = new HSSFRichTextString(object);
        row.setCellValue(j);
    }

    for(e = 0; e < array.size(); ++e) {
        JSONObject var16 = array.getJSONObject(e);
        HSSFRow var17 = sheet.createRow(e + 1);

        for(int var18 = 0; var18 < properties.length; ++var18) {
            String string = properties[var18];
            HSSFCell cell = var17.createCell(var18);
            try {
                if (var16 == null) continue;
                if (var16.get(string) == null) continue;
                 String text = var16.get(string).toString();

// HSSFRichTextString text = new HSSFRichTextString(var16.get(string).toString());
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
// HSSFRichTextString text = new HSSFRichTextString(var16.getDouble(string) + “”);
// cell.setCellValue(text);
}

        }
    }
        workbook.write(output);

    } catch (Exception var15) {
        var15.printStackTrace();
    }
}

}
package com.paf.backend.jsonexcel;

import com.paf.common.sequence.IdSequenceFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.;
import java.util.
;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.xssf.usermodel.*;

/**

  • @PACKAGE com.paf.backend.jsonexcel

  • @AUTHOR tan_yh

  • @Description

  • @Date 2020/5/5 11:19

  • @Version 1.0
    */
    public class JsonToExcel {

    public static void start(String [] args){
    readFile();
    }

    public static void readFile(){
    Vector ver=new Vector(); //用做堆栈;
    ver.add(“F:/temp/json/”);
    int num = 0;
    Map<String, HSSFWorkbook> workbookMap= new HashMap<>();
    Map<String, String> fileMap= new HashMap<>();
    try {
    while(ver.size()>0){
    File[] files = new File(ver.get(0).toString()).listFiles(); //获取该文件夹下所有的文件(夹)名
    ver.remove(0);
    int len=files.length;
    for(int i=0;i<len;i++){
    String filePath=files[i].getAbsolutePath();
    if(files[i].isDirectory()) { //如果是目录,则加入队列。以便进行后续处理
    if (num == 1){
    workbookMap.put(files[i].getAbsolutePath(), new HSSFWorkbook());
    fileMap.put(files[i].getAbsolutePath(), (files[i].getAbsolutePath()+"/a.xls"));
    }
    ver.add(filePath);

                 }else {
    
                     String fileType = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    
                     String fileName = filePath.substring(0, filePath.lastIndexOf("."));
                     HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
                     if (fileType.indexOf("json") > -1) {
                         for (String key :workbookMap.keySet()) {
                             if (filePath.contains(key)){
                                 fileName= fileMap.get(key);
                                 hssfWorkbook= workbookMap.get(key);
                             }
                         }
    
                         jsonFile(new File(filePath), fileName, hssfWorkbook);        //如果是文件,则直接输出文件名到指定的文件。
                     }
                 }
             }
             num = 1;
         }
     }catch (Exception e){
         e.printStackTrace();
     }
    

    }
    public static void jsonFile(File jsonFile, String fileName, HSSFWorkbook hssfWorkbook){
    try {
    FileReader reader = new FileReader(jsonFile);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = null;
    StringBuffer stringBuffer = new StringBuffer();
    while ((line= bufferedReader.readLine()) != null){
    stringBuffer.append(line);
    }
    JSONArray jsonArray = null;
    JSONObject labelObject = null;
    String cnLabel = “sheet”;
    JSONObject jsonObject = JSONObject.fromObject(stringBuffer.toString());
    if (jsonObject != null && jsonObject.containsKey(“properties”)) {
    jsonArray = jsonObject.getJSONArray(“properties”);
    labelObject = jsonObject.getJSONObject(“label”);
    if (labelObject.containsKey(“cn”)){
    cnLabel = labelObject.getString(“cn”);
    }
    }else{
    return;
    }
    // fileName = jsonFile.getOriginalFilename();
    // String filetype = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
    if (jsonArray == null) return;
    FileOutputStream xlsStream = new FileOutputStream(fileName);
    build(xlsStream, jsonArray, new String[]{“dataType”,“hasSourceId”,“cardinality”,“en”,“cn”}, cnLabel, hssfWorkbook);
    xlsStream.close();
    }catch (Exception e){
    System.out.printf("===="+fileName);
    e.printStackTrace();
    }

    }
    public static void build(OutputStream output, JSONArray json, String[] properties, String cnLabel, HSSFWorkbook hssfWorkbook) {
    build(output, json, properties, properties, cnLabel, hssfWorkbook);
    }

    public static void build(OutputStream output, JSONArray array, String[] properties, String[] columnsNames, String cnLabel, HSSFWorkbook workbook) {
    try {
    // //读取excel文件
    // InputStream is = new FileInputStream(fileName);
    // //创建excel工作薄
    // XSSFWorkbook workbook = new XSSFWorkbook();
    // //创建一个工作表sheet
    // XSSFSheet sheet = workbook.getSheetAt(0);
    // XSSFRow header = sheet.createRow(0);

// HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(cnLabel);

    HSSFRow header = sheet.createRow(0);

    int e;
    for(e = 0; e < columnsNames.length; ++e) {
        String object = columnsNames[e];
        HSSFCell row = header.createCell(e);
        HSSFRichTextString j = new HSSFRichTextString(object);
        row.setCellValue(j);
    }

    for(e = 0; e < array.size(); ++e) {
        JSONObject var16 = array.getJSONObject(e);
        HSSFRow var17 = sheet.createRow(e + 1);

        for(int var18 = 0; var18 < properties.length; ++var18) {
            String string = properties[var18];
            HSSFCell cell = var17.createCell(var18);
            try {
                if (var16 == null) continue;
                if (var16.get(string) == null) continue;
                 String text = var16.get(string).toString();

// HSSFRichTextString text = new HSSFRichTextString(var16.get(string).toString());
cell.setCellValue(text);
} catch (Exception var15) {
var15.printStackTrace();
// HSSFRichTextString text = new HSSFRichTextString(var16.getDouble(string) + “”);
// cell.setCellValue(text);
}

        }
    }
        workbook.write(output);

    } catch (Exception var15) {
        var15.printStackTrace();
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值