JAVA使⽤easyexcel操作Excel
作者对easyexcel的介绍是:
使⽤easyexcel,⾸先我们需要添加maven依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.0.1</version>
</dependency>
⾸先,我们先来看看如何写Excel,写⼊Excel,我们可以通过l.ExcelWriter类实现,下⾯我们来看⼀下最简单的⽆表头的实现
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import l.ExcelWriter;
import l.metadata.Sheet;
import l.support.ExcelTypeEnum;
public class ExcelWriteTest {
/**
* 每⾏数据是List<String>⽆表头
*
* @throws IOException
*/
@Test
public void writeWithoutHead() throws IOException {
try (OutputStream out = new FileOutputStream("withoutHead.xlsx");) {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, false);
Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("sheet1");
List<List<String>> data = new ArrayList<>();
for (int i = 0; i < 100; i++) {
List<String> item = new ArrayList<>();
item.add("item0" + i);
item.add("item1" + i);
item.add("item2" + i);
data.add(item);
}
writer.write0(data, sheet1);
writer.finish();
}
}
}
很多时候,我们在⽣成Excel的时候都是需要添加表头的,使⽤easyexcel可以很容易的实现,我们可以对上⾯的例⼦进⾏简单的改造,为其添加表头
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import l.ExcelWriter;
import l.metadata.Sheet;
import l.metadata.Table;
import l.support.ExcelTypeEnum;
public class ExcelWriteTest {
@Test
public void writeWithoutHead() throws IOException {
try (OutputStream out = new FileOutputStream("withHead.xlsx");) {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("sheet1");
List<List<String>> data = new ArrayList<>();
for (int i = 0; i < 100; i++) {
List<String> item = new ArrayList<>();
item.add("item0" + i);
item.add("item1" + i);
item.add("item2" + i);
data.add(item);
}
List<List<String>> head = new ArrayList<List<String>>();
List<String> headCoulumn1 = new ArrayList<String>();
List<String> headCoulumn2 = new ArrayList<String>();
List<String> headCoulumn3 = new ArrayList<String>();
headCoulumn1.add("第⼀列");
headCoulumn2.add("第⼆列");
headCoulumn3.add("第三列");
head.add(headCoulumn1);
head.add(headCoulumn2);
head.add(headCoulumn3);
Table table = new Table(1);
table.setHead(head);
writer.write0(data, sheet1, table);
writer.finish();
}
}
}
效果如下:
这⾥写图⽚描述
除了上⾯添加表头的⽅式,我们还可以使⽤实体类,为其添加l.annotation.ExcelProperty注解来⽣成表头,实体类数据作为Excel数据
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import l.ExcelWriter;
import l.annotation.ExcelProperty;
import l.metadata.BaseRowModel;
import l.metadata.Sheet;
import l.metadata.Table;
import l.support.ExcelTypeEnum;
public class ExcelWriteTest {
@Test
public void writeWithHead() throws IOException {
try (OutputStream out = new FileOutputStream("withHead.xlsx");) { ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX); Sheet sheet1 = new Sheet(1, 0, ExcelPropertyIndexModel.class);
sheet1.setSheetName("sheet1");
List<ExcelPropertyIndexModel> data = new ArrayList<>();
for (int i = 0; i < 100; i++) {
ExcelPropertyIndexModel item = new ExcelPropertyIndexModel(); item.name = "name" + i;
item.age = "age" + i;
item.address = "address" + i;
item.sax = "sax" + i;
item.heigh = "heigh" + i;
item.last = "last" + i;
data.add(item);
}
writer.write(data, sheet1);
writer.finish();
}
}
public static class ExcelPropertyIndexModel extends BaseRowModel {
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ExcelProperty(value = "年龄", index = 1)
private String age;
@ExcelProperty(value = "邮箱", index = 2)
private String email;
@ExcelProperty(value = "地址", index = 3)
private String address;
@ExcelProperty(value = "性别", index = 4)
private String sax;
@ExcelProperty(value = "⾼度", index = 5)
private String heigh;
@ExcelProperty(value = "备注", index = 6) private String last;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
}
public String getAddress() {
return address;
}
public void setAddress(String address) { this.address = address;
}
public String getSax() {
return sax;
}
public void setSax(String sax) {
this.sax = sax;
}
public String getHeigh() {
return heigh;
}
public void setHeigh(String heigh) {
this.heigh = heigh;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
}
}
效果如下:
这⾥写图⽚描述
如果单⾏表头表头还不满⾜需求,没关系,还可以使⽤多⾏复杂的表头
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import l.ExcelWriter;
import l.annotation.ExcelProperty;
import l.metadata.BaseRowModel;enum怎么用
import l.metadata.Sheet;
import l.metadata.Table;
import l.support.ExcelTypeEnum;
public class ExcelWriteTest {
@Test
public void writeWithMultiHead() throws IOException {
try (OutputStream out = new FileOutputStream("withMultiHead.xlsx");) { ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet1 = new Sheet(1, 0, MultiLineHeadExcelModel.class);
sheet1.setSheetName("sheet1");
List<MultiLineHeadExcelModel> data = new ArrayList<>();
for (int i = 0; i < 100; i++) {
MultiLineHeadExcelModel item = new MultiLineHeadExcelModel(); item.p1 = "p1" + i;
item.p2 = "p2" + i;
item.p3 = "p3" + i;
item.p4 = "p4" + i;
item.p5 = "p5" + i;
item.p6 = "p6" + i;
item.p7 = "p7" + i;
item.p8 = "p8" + i;
item.p9 = "p9" + i;
data.add(item);
}
writer.write(data, sheet1);
writer.finish();
}
}
public static class MultiLineHeadExcelModel extends BaseRowModel {
@ExcelProperty(value = { "表头1", "表头1", "表头31" }, index = 0)
private String p1;
@ExcelProperty(value = { "表头1", "表头1", "表头32" }, index = 1)
private String p2;
@ExcelProperty(value = { "表头3", "表头3", "表头3" }, index = 2)
private String p3;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论