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;
}
相关推荐
楚韵天工17 分钟前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
helloworddm19 分钟前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#
失散1322 分钟前
分布式专题——43 ElasticSearch概述
java·分布式·elasticsearch·架构
ajsbxi23 分钟前
【Java 基础】核心知识点梳理
java·开发语言·笔记
聪明的笨猪猪1 小时前
Java JVM “调优” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
重整旗鼓~1 小时前
28.redisson源码分析分布式锁
java·开发语言
Query*1 小时前
Java 设计模式——工厂模式:从原理到实战的系统指南
java·python·设计模式
懒羊羊不懒@2 小时前
Java基础语法—最小单位、及注释
java·c语言·开发语言·数据结构·学习·算法
ss2732 小时前
手写Spring第4弹: Spring框架进化论:15年技术变迁:从XML配置到响应式编程的演进之路
xml·java·开发语言·后端·spring
DokiDoki之父2 小时前
MyBatis—增删查改操作
java·spring boot·mybatis