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

Posted 长安紫薯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高级知识点: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);
	}
}

以上是关于高级知识点:excel4j实现java操作excel文件的读写的主要内容,如果未能解决你的问题,请参考以下文章

2021最新Java知识体系总结,高级面试题+解析

Java 高级开发必修知识---反射

如何用JAVA语言建立含有若干个元素的顺序表,并实现插入,删除,查找等基本操作

Java高级部分知识总结快速进阶

深入剖析!Java高级开发岗必问知识点

java知识点-高级