高级知识点:excel4j实现java操作excel文件的读写

介绍

java操作excel文件最经典的是POI,但是其api 较多,代码量巨大,反复重复
excel4j ap则简洁太多,利用pojo建模,利用注解进行标识@ExcelField(title = “URL”)

准备条件

1、引入jar支持

		<dependency>
			<groupId>com.github.crab2died</groupId>
			<artifactId>Excel4J</artifactId>
			<version>3.0.0-Alpha</version>
		</dependency>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.6</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.tedu</groupId>
	<artifactId>zp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>zp</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.14.3</version>
		</dependency>
		<dependency>
			<groupId>com.github.crab2died</groupId>
			<artifactId>Excel4J</artifactId>
			<version>3.0.0-Alpha</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

2、准备测试的excel文件

excel文件

注意文件的路径:
在这里插入图片描述

在这里插入图片描述

pojo文件

pojo中的列和excel中的列对应,第一行就作为标题头 header,配合注解 @ExcelField

package zp;

import com.github.crab2died.annotation.ExcelField;

public class InfoExcel {
	@ExcelField(title = "编号")
	private String id;
	
	@ExcelField(title = "URL")
	private String subUrl;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSubUrl() {
		return subUrl;
	}

	public void setSubUrl(String subUrl) {
		this.subUrl = subUrl;
	}
}

读 excel 内容到对应的pojo对象集合中

写 把pojo对象集合写入到excel中

测试类

package zp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.github.crab2died.ExcelUtils;

public class TestExcel4J {
	public static void main(String[] args) throws Exception {
		String path_read = System.getProperty("user.dir") + File.separator + "data" + File.separator + "test.xlsx";
		String path_writeString = System.getProperty("user.dir") + File.separator + "data" + File.separator
				+ "result.xlsx";
		// 读
		//List<InfoExcel> list = TestExcel4J.excel4j_readExc(path_read);
		
		List<InfoExcel> list =new ArrayList<InfoExcel>();
		InfoExcel ie = new InfoExcel();
		ie.setId("100");
		ie.setSubUrl("act.codeboy.com");
		list.add(ie);
		
		// 写
		TestExcel4J.excel4j_writeExc(list, path_writeString,InfoExcel.class);
	}
	
	public static List<InfoExcel> excel4j_readExc(String excelPathRead) throws Exception {
		List<InfoExcel> list = ExcelUtils.getInstance().readExcel2Objects(excelPathRead, InfoExcel.class);
		System.out.println(list);
		return list;
	}
	
	public static <E> void excel4j_writeExc(List<E> list, String excelPathWrite,Class clazz) throws Exception {
		ExcelUtils.getInstance().exportObjects2Excel(list, clazz, excelPathWrite);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值