zoukankan      html  css  js  c++  java
  • 将JSON文件转换为Excel

    package com.duan;
    import cn.afterturn.easypoi.excel.ExcelExportUtil;
    import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.apache.poi.ss.usermodel.Workbook;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import java.io.*;
    import java.util.*;
    
    public class JsonRead {
        private static List<JSONArray> jsonList = new ArrayList<>();
        private static void process(String txtStr)  {
            JSONObject json = JSONObject.fromObject(txtStr);
            JSONArray datas = json.getJSONArray("data");
            jsonList.add(datas);
        }
    
        public static void saveFile(List<JSONArray> l) throws IOException{
            List<Proj> list = new ArrayList<>();
            for (JSONArray datas:l) {
                for (int i = 0; i < datas.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    JSONObject obj = datas.getJSONObject(i);
                    String propertyTypes = obj.getString("propertyTypes");
                    String pid = obj.getString("pid");
                    String projName = obj.getString("projName");
                    String saleStatus = obj.getString("saleStatus");
                    String status = obj.getString("status");
    
                    Proj proj = new Proj(propertyTypes, pid, projName, saleStatus, status);
                    list.add(proj);
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            String excelName = "/Users/root/Desktop/export/test1.xlsx";    //所需保存的模板文件
            TemplateExportParams params = new TemplateExportParams(excelName);
            Workbook work = ExcelExportUtil.exportExcel(params, map);
            FileOutputStream fos = new FileOutputStream("/Users/root/Desktop/export/test1_map.xlsx"); //所需生成的Excel文件 
            work.write(fos);
            fos.close();
        }
    
        public static void main(String[] args) throws IOException {
            String fileName = "/Users/root/Desktop/未命名文件夹";  //Json文件所在的文件夹路径~
            File file = new File(fileName);		        //获取其file对象
            File[] fs = file.listFiles();
            for (File f: fs
                 ) {
                if(!f.getName().contains("json")){
                    continue;
                }
                BufferedReader bufferedReader = new BufferedReader(new FileReader(f));
                String context = null;
                StringBuilder json = new StringBuilder();
    
                //json内容转化为Map集合通过遍历集合来进行封装
                while ((context = bufferedReader.readLine()) != null) {
                    //Context就是读到的json数据
                    json.append(context);
                }
                String txtStr = json.toString();
                if (txtStr != null) {
                    JSONObject json1 = JSONObject.fromObject(txtStr);
                    JSONArray datas = json1.getJSONArray("data");
                    jsonList.add(datas);
                } else {
                    System.out.println("Read the content is empty!");
                }
            }
            saveFile(jsonList);
        }
    }
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    class Proj{
        String propertyTypes;
        String pid;
        String projName;
        String saleStatus;  //销售状态
        String status;      //是否隐藏状态
    }
    

    1、所需导出的Excel模版文件如图所示:

     2、所需依赖如下,相应版本可根据自己环境而定!!!

    <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.4</version>
                <classifier>jdk15</classifier>
            </dependency>
            <dependency>
                <groupId>cn.afterturn</groupId>
                <artifactId>easypoi-base</artifactId>
                <version>4.1.0</version>
            </dependency>
            <dependency>
                <groupId>cn.afterturn</groupId>
                <artifactId>easypoi-web</artifactId>
                <version>4.1.0</version>
            </dependency>
            <dependency>
                <groupId>cn.afterturn</groupId>
                <artifactId>easypoi-annotation</artifactId>
                <version>4.1.0</version>
            </dependency>
  • 相关阅读:
    Jmeter系列(64)- JMeter JSR223 入门
    Jmeter系列(63)- Beanshell 入门
    Jmeter系列(62)- 详解 JSON 断言
    Jmeter系列(61)- 详解断言持续时间
    Jmeter系列(60)- 详解响应断言
    取球游戏|2012年蓝桥杯B组题解析第十题-fishers
    夺冠概率|2012年蓝桥杯B组题解析第九题-fishers
    密码发生器|2012年蓝桥杯B组题解析第八题-fishers
    放棋子|2012年蓝桥杯B组题解析第七题-fishers
    大数乘法|2012年蓝桥杯B组题解析第六题-fishers
  • 原文地址:https://www.cnblogs.com/Duancf/p/14591076.html
Copyright © 2011-2022 走看看