Excel
方式
1,POI方式
HSSFWorkbook用来操作Excel2003以前(包括2003)的版本,”.xls”行数最多65536行。
XSSFWorkbook用来操作Excel2007之后(包括2007)的版本,”.xlsx”行数最多1048576行。
2,EasyExcel方式
EasyExcel依赖POI版本最低为3.17;
POI 原理是将数据全部读取,放入内存中,容易内存溢出。
EasyExcel 原理是避免把数据全部放入内存中,并放弃一些不重要的样式。
3,HuTool方式
HuTool—4依赖POI版本最低为3.17; HuTool—5依赖POI版本最低为4.1.2; HuTool—5.6依赖POI版本最低为5;
写入
package com.lgx.utils.excel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelTest {
public static void main(String[] args) throws IOException {
UserPojo userPojo = new UserPojo();
userPojo.setUserName("zs");
userPojo.setPhone("123");
UserPojo userPojo2 = new UserPojo();
userPojo2.setUserName("ls");
userPojo2.setPhone("321");
List<UserPojo> userPojos = new ArrayList<UserPojo>();
userPojos.add(userPojo);
userPojos.add(userPojo2);
String fileName = "/Users/liguoxi/tmp/aaa.xlsx";
Poi.writeExcel(userPojos, fileName);
EasyExcel.writeExcel(userPojos, fileName);
HuTool.writeExcel(userPojos, fileName);
}
}
package com.lgx.utils.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class UserPojo {
@ExcelProperty(index = 0, value = {"用户账号", "用户账号"})
private String userName;//用户账号
@ExcelProperty(index = 1, value = {"手机号码", "手机号码"})
private String phone;//手机号码
}
1,POI方式
maven引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
代码如下
package com.lgx.utils.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.List;
public class Poi {
public static void writeExcel(List<UserPojo> userPojos, String fileName) throws IOException {
//1,创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//2,创建工作表
XSSFSheet sheet = workbook.createSheet("当前sheet页的名称");
//取第二个sheet页
//XSSFSheet sheet = workbook.getSheetAt(1);
// 3,创建行,创建列
XSSFRow row;
XSSFCell cell;
//4,创建第一行,标题行
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("用户名称");
cell = row.createCell(1);
cell.setCellValue("手机号码");
cell = row.createCell(2);
//5,填充其余行,表数据
for (int i = 0; i < userPojos.size(); i++) {
UserPojo userPojo = userPojos.get(i);
//第一行是表头 数据要从第二行开始
row = sheet.createRow(i + 1);
Cell cell2 = row.createCell(0);
cell2.setCellValue(userPojo.getUserName());
Cell cell5 = row.createCell(1);
cell5.setCellValue(userPojo.getPhone());
}
//6,写入文件中
FileOutputStream out = new FileOutputStream(new File(fileName));
workbook.write(out);
//7,关闭流
out.close();
}
}
2,EasyExcel方式
maven引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
代码如下
package com.lgx.utils.excel;
import java.util.List;
public class EasyExcel {
public static void writeExcel(List<UserPojo> userPojos, String fileName) {
com.alibaba.excel.EasyExcel.write(fileName, UserPojo.class).sheet("当前sheet页的名称").doWrite(userPojos);
}
}
3,HuTool方式
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.0</version>
</dependency>
代码如下所示
package com.lgx.utils.excel;
import cn.hutool.poi.excel.ExcelWriter;
import java.util.List;
public class HuTool {
public static void writeExcel(List<UserPojo> userPojos, String fileName) {
ExcelWriter writer = new ExcelWriter(fileName);
//自定义标题别名
writer.addHeaderAlias("userName", "用户账号");
writer.addHeaderAlias("phone", "手机号码");
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(userPojos, true);
// 关闭writer,释放内存
writer.close();
}
}