【Tools】Json转Excel 与 Excel转Json(最简单基础的实现演示)

说明

有时会使用一些演示数据做演示项目或者需要excel文件数据时,为了方便数据的类型转换,使用JAVA实现,超简单的json到excel;使用了一个线上的跨平台的文件转换工具,可实现excel文件转为json数据。

Json转Excel

创建Maven项目,使用JDK8。
在这里插入图片描述
导入依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.48</version>
        </dependency>
    </dependencies>

基础代码

package com.lingfei;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.*;

/**
 * Description:
 *
 * @Author: LingFei
 * @Create: 2022/12/13 - 15:54
 */
public class JsonToExcel {
    
    

    public static void main(String[] args) {
    
    
        transform();
    }

    public static void transform() {
    
    
        StringBuilder sb = new StringBuilder();
        try {
    
    
            FileReader in = new FileReader(new File("D:/test.json"));
            BufferedReader inBR = new BufferedReader(in);
            String sText = null;
            //读取每一行内容
            while ((sText = inBR.readLine()) != null) {
    
    
                sb.append(sText);
            }
            inBR.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }

        try {
    
    
            //创建文件
            String uploadFile = "D:/test.xlsx";
            OutputStream fos = new FileOutputStream(uploadFile);
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet();

            XSSFRow row = null;
            XSSFCell cell = null;

            //创建表头 可自定义
            row = sheet.createRow(0);
            String[] names = {
    
    "empNo", "name", "phone", "type", "Q1_Str", "Q1_Id", "Q2_Str", "Q2_Id", "Q2_Value"};
            for (int index = 0; index < 9; index++) {
    
    
                cell = row.createCell(index);
                cell.setCellValue(names[index]);
            }

            int count = 1;
            //使用fastjson转换
            JSONArray jsonArray = JSON.parseArray(sb.toString());
            int size = jsonArray.size();
            for (int i = 0; i < size; i++) {
    
    
                JSONObject item = jsonArray.getJSONObject(i);

				//填入数据 可自定义
                String empNo = item.getString("empNo");
                String name = item.getString("name");
                String phone = item.getString("phone");
                String type = item.getString("type");
                String Q1_Str = item.getString("Q1_Str");
                String Q1_Id = item.getString("Q1_Id");
                String Q2_Str = item.getString("Q2_Str");
                String Q2_Id = item.getString("Q2_Id");
                String Q2_Value = item.getString("Q2_Value");

                row = sheet.createRow(count);

                cell = row.createCell(0);
                cell.setCellValue(empNo);

                cell = row.createCell(1);
                cell.setCellValue(name);

                cell = row.createCell(2);
                cell.setCellValue(phone);

                cell = row.createCell(3);
                cell.setCellValue(type);

                cell = row.createCell(4);
                cell.setCellValue(Q1_Str);

                cell = row.createCell(5);
                cell.setCellValue(Q1_Id);

                cell = row.createCell(6);
                cell.setCellValue(Q2_Str);

                cell = row.createCell(7);
                cell.setCellValue(Q2_Id);

                cell = row.createCell(8);
                cell.setCellValue(Q2_Value);
				//以上数据填入可自定义根据需求设计
				
                count++;
            }
            //写入Excel中
            workBook.write(fos);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

test.json

[
    {
    
    
        "empNo": 123,
        "name": "测试1",
        "phone": "18888888888",
        "type": "后勤",
        "Q1_Str": "xxx",
        "Q1_Id": "T2s_GM",
        "Q2_Str": "xxx",
        "Q2_Id": "T2s_GM_1",
        "Q2_Value": "6"
    },
    {
    
    
        "empNo": 321,
        "name": "测试2",
        "phone": "18888888888",
        "type": "后勤",
        "Q1_Str": "xxx",
        "Q1_Id": "T2s_GM",
        "Q2_Str": "xxx",
        "Q2_Id": "T2s_GM_2",
        "Q2_Value": "4中立"
    }
]

转换结果
在这里插入图片描述

Excel转Json

一个很方便的工具

猜你喜欢

转载自blog.csdn.net/weixin_42029283/article/details/128303231
今日推荐