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 分钟前
Maven进阶与搭建私服
java·maven
大不点wow28 分钟前
Java序列化与反序列化:让对象走出JVM
java·开发语言·jvm
噢,我明白了33 分钟前
Java中日期和字符串的处理
java·开发语言·日期
dkbnull40 分钟前
Spring Boot请求处理组件对比详解
java·spring boot
jun_bai42 分钟前
Orthanc服务器使用java上传dicom影像文件
java·运维·服务器
顺风尿一寸43 分钟前
记一次 Spring AOP 与定时任务引发的死锁排查
java
用户2986985301444 分钟前
Python 实现 Excel 与 Markdown 互转的实用指南
后端·python·excel
用户446139430271 小时前
从单体到微服务:我们项目的拆分思路和踩坑记录
java
TDengine (老段)1 小时前
TDengine 免费版说明
java·大数据·数据库·物联网·时序数据库·tdengine
码上有光1 小时前
异常和智能指针
java·大数据·c++·servlet·异常·智能指针