java 读取json文件并写入Excel

依赖于lombok、easyExcel

java 复制代码
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hsj
 * @description:
 * @date 2024-8-28 17:54
 */
public class ReadJsonFile {
	public static void main(String[] args) throws Exception {
		File file = new File("I:\\BladeX-Boot\\src\\test\\java\\org\\springblade\\税220304.json");
		FileReader fileReader = new FileReader(file);
		Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
		int ch = 0;
		StringBuffer sb = new StringBuffer();
		while ((ch = reader.read()) != -1) {
			sb.append((char) ch);
		}
		fileReader.close();
		reader.close();
		String jsonStr = sb.toString();

		JSONArray ja = JSON.parseArray(jsonStr);
		List<F> list = new ArrayList<F>();
		for (Object o : ja) {
			JSONObject jb = (JSONObject) o;
			F f = F.builder()
				.name(String.format("*%s*%s", jb.getString("spfwjc"), jb.getString("xmmc")))
				.id(jb.getString("sphfwssflhbbm"))
				.model(jb.getString("ggxh"))
				.unit(jb.getString("dw"))
				.num(jb.getString("spsl"))
				.price(jb.getBigDecimal("hsdj"))
				.total(jb.getBigDecimal("hsje"))
				.tax0(jb.getBigDecimal("slv"))
				.tax1(jb.getBigDecimal("se"))
				.build();
			list.add(f);
		}

		EasyExcel.write("I:\\BladeX-Boot\\src\\test\\java\\org\\springblade\\税220304.xlsx", F.class).sheet("税220304").doWrite(list);
	}
}

@Getter
@Setter
@EqualsAndHashCode
@Builder
class F {
	@ExcelProperty("项目名称")
	private String name;
	@ExcelProperty("ID")
	private String id;
	@ExcelProperty("规格型号")
	private String model;
	@ExcelProperty("单位")
	private String unit;
	@ExcelProperty("数量")
	private String num;
	@ExcelProperty("单价含税")
	private BigDecimal price;
	@ExcelProperty("金额含税")
	private BigDecimal total;
	@ExcelProperty("税率")
	private BigDecimal tax0;
	@ExcelProperty("税额")
	private BigDecimal tax1;
}
相关推荐
桦说编程2 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen2 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研2 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员3 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋3 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国3 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~3 小时前
《设计模式》装饰模式
java·设计模式
A尘埃3 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交4 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
CHEN5_024 小时前
【Java基础面试题】Java基础概念
java·开发语言