Java读取Excel文件的内容,归档到json文件中(对json数据进行读取、添加/修改put、删除remove操作)

今天有个需求,写个脚本文件,对Excel中的数据,归档到json文件中(其实就是将Excel文件中的内容,添加或修改到json文件中所对应的属性字段:uid、name和head)。

其实我也就是使用Java写了个工具类,使用文件流来实现上面的操作(需求)。

不废话,直接上代码:

package com.dw.datamodificationscript.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.junit.Test;

import java.io.*;

public class ExcelToJsonUtil {
    public static final String USER_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\user111.xls"; //user.xls
    public static final String DUB_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\dub111.json";  //dub.json
    public static final String INDEX_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\index111.json";  //index.json
    public static final String NEW_DUB_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\new_dub.json";  //new_dub.json
    public static final String NEW_INDEX_PATH = "C:\\Users\\Windows 10\\Desktop\\tempFile\\new_index.json";  //new_index.json

    @Test
    public void dubJsonFile_test(){ //归档第一个json文件,先读取json文件,更新(添加或替换,其实都是put操作)内容,通过流输出到一个新的json文件
        try{
            File dub_file = new File(NEW_DUB_PATH);
            if (!dub_file.exists()){
                try {
                    dub_file.createNewFile();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }

            String dub = readJsonFile(DUB_PATH);
            JSONObject json_dub = JSON.parseObject(dub);
            JSONArray dubs = json_dub.getJSONArray("dubs");
            JSONArray new_dubs = new JSONArray(dubs.size());

            Workbook wb = readExcelFile(USER_PATH);
            //int sheetSize = wb.getNumberOfSheets(); //获取sheet总页数
            Sheet sheet = wb.getSheet(0);  //获取第一页
            int row_total = sheet.getRows();
            //System.out.println("========================user的长度:" + row_total);
            //System.out.println("========================dubs的长度:" + dubs.size());
            for (int i = 0 ; i < dubs.size(); i++) {
          //更新操作,具体逻辑操作可以不用看,知道大致流程就好(具体问题,具体分析) JSONObject dub_item
= dubs.getJSONObject(i); JSONObject user = dub_item.getJSONObject("user"); int j = i + 1; //排除第一行标题 Cell[] cells = sheet.getRow(j); user.put("uid", cells[0].getContents()); user.put("name", cells[1].getContents()); user.put("head", cells[2].getContents()); new_dubs.add(dub_item); } json_dub.put("dubs", new_dubs); writeJsonFile(json_dub.toString(), NEW_DUB_PATH); }catch (Exception e) { e.printStackTrace(); } } @Test public void indexJsonFile_test(){ //归档第二个json文件,操作类似,区别就是读取json文件中的需要进行修改的字段的json对象(说白了就是怎么获取该字段json对象,并对其进行更新操作) try{ File index_file = new File(NEW_INDEX_PATH); if (!index_file.exists()){ try { index_file.createNewFile(); }catch (IOException e) { e.printStackTrace(); } } String index = readJsonFile(INDEX_PATH); JSONObject json_listens = JSON.parseObject(index); JSONArray listens = json_listens.getJSONArray("listens"); JSONArray new_listens = new JSONArray(listens.size()); Workbook wb = readExcelFile(USER_PATH); //int sheetSize = wb.getNumberOfSheets(); //获取sheet总页数 Sheet sheet = wb.getSheet(0); //获取第一页 int row_total = sheet.getRows(); //System.out.println("========================user的长度:" + row_total); //System.out.println("========================listens的长度:" + listens.size()); for (int i = 0 ; i < listens.size(); i++) {
          // 更新操作,具体操作逻辑可以忽略不看,知道大致流程就好(具体问题,具体分析) JSONObject listens_item
= listens.getJSONObject(i); JSONObject dub = listens_item.getJSONObject("dub"); JSONObject user = dub.getJSONObject("user"); int j = i + 21; //为了避免重复使用,排除第一行标题 + 后面的有效数据20行(dub.json文件已使用的行数) Cell[] cells = sheet.getRow(j); user.put("uid", cells[0].getContents()); user.put("name", cells[1].getContents()); user.put("head", cells[2].getContents()); new_listens.add(listens_item); } json_listens.put("listens", new_listens); writeJsonFile(json_listens.toString(), NEW_INDEX_PATH); }catch (Exception e) { e.printStackTrace(); } } /** * @Function 读取Excel文件内容 * @Date 2021年10月9日16:29:52 * @Author Liangjw */ public Workbook readExcelFile(String filePath) { try { Workbook wb = null; InputStream is = new FileInputStream(USER_PATH); wb = Workbook.getWorkbook(is); is.close(); return wb; }catch (Exception e) { e.printStackTrace(); return null; }finally { } } /** * @Function 读取json文件 * @Date 2021年10月9日11:21:24 * @Author Liangjw */ public static String readJsonFile(String filePath) { String jsonStr = ""; try { File jsonFile = new File(filePath); //FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } //fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null; } } /** * @Function 写出json文件 * @Date 2021年10月9日14:21:24 * @Author Liangjw */ public static void writeJsonFile(String newJsonString, String path){ try { FileWriter fw = new FileWriter(path); PrintWriter out = new PrintWriter(fw); out.write(newJsonString); out.println(); fw.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }

 

// 删除操作我就不演示了,其实就是remove操作
/*for(int  i = 0; i < xx.size; i++){
     JSONObject jsonObject = jsonArray.getJSONObject(i);
     if(model.getModelid().equals(jsonObject.getString("id"))){
        jsonArray.remove(jsonObject);
     }
}*/

 

其实,主要流程就是:

1. 读取Excel、json文件到文件流中,通过文件流获取内容;

2. 操作自己的业务逻辑(添加、删除、更新等操作)

3. 将文件流输出到新的json文件

 

 

posted @ 2021-10-11 09:22  向着阳光漫步  阅读(560)  评论(0编辑  收藏  举报