poi
导入依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi 03版-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml 07版-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
写数据
测试类
@Test
void test03() throws Exception {
Workbook workbook = new HSSFWorkbook();//HSSFWorkbook03版
Sheet mysheet = workbook.createSheet("mysheet");
Row row = mysheet.createRow(0);
Cell cell1 = row.createCell(0);
cell1.setCellValue("hello");
Cell cell2 = row.createCell(1);
cell2.setCellValue("hello");
FileOutputStream fileOutputStream = new FileOutputStream("E:\\code\\pig" + "\\test.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
@Test
void test07() throws Exception {
Workbook workbook = new XSSFWorkbook();//XSSFWorkbook03版
Sheet mysheet = workbook.createSheet("mysheet");
Row row = mysheet.createRow(0);
Cell cell1 = row.createCell(0);
cell1.setCellValue("hello");
Cell cell2 = row.createCell(1);
cell2.setCellValue("hello");
FileOutputStream fileOutputStream = new FileOutputStream("E:\\code\\pig" + "\\test.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
数据多一点
@Test
void test031() throws Exception {
Long start = System.currentTimeMillis();
Workbook workbook = new HSSFWorkbook();
Sheet mysheet = workbook.createSheet("mysheet");
for (int i = 0; i < 65536; i++) {
Row row = mysheet.createRow(i);
Cell cell1 = row.createCell(0);
cell1.setCellValue("hello");
Cell cell2 = row.createCell(1);
cell2.setCellValue("hello");
}
FileOutputStream fileOutputStream = new FileOutputStream("E:\\code\\pig" + "\\test.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
Long end = System.currentTimeMillis();
System.out.println(end-start);//729
}
@Test
void test071() throws Exception {
Long start = System.currentTimeMillis();
Workbook workbook = new XSSFWorkbook();
Sheet mysheet = workbook.createSheet("mysheet");
for (int i = 0; i < 100000; i++) {
Row row = mysheet.createRow(i);
Cell cell1 = row.createCell(0);
cell1.setCellValue("hello");
Cell cell2 = row.createCell(1);
cell2.setCellValue("hello");
}
FileOutputStream fileOutputStream = new FileOutputStream("E:\\code\\pig" + "\\test.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
Long end = System.currentTimeMillis();
System.out.println(end-start);//4993
}
03版的最多可以65536行,多了就会报错,把数据全部读出来一起写入文件,内存消耗较多,耗时就少
07班的没有限制,会一行一行的写入文件,内存消耗较少,耗时就多
使用SXSSFWorkbook类比XSSFWorkbook类要快一点
@Test
void test0711() throws Exception {
Long start = System.currentTimeMillis();
Workbook workbook = new SXSSFWorkbook();
Sheet mysheet = workbook.createSheet("mysheet");
for (int i = 0; i < 100000; i++) {
Row row = mysheet.createRow(i);
Cell cell1 = row.createCell(0);
cell1.setCellValue("hello");
Cell cell2 = row.createCell(1);
cell2.setCellValue("hello");
}
FileOutputStream fileOutputStream = new FileOutputStream("E:\\code\\pig" + "\\test.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
//清楚临时文件
((SXSSFWorkbook)workbook).dispose();
Long end = System.currentTimeMillis();
System.out.println(end-start);//1269
}
写文件主要是实现类的区别
读数据
简单读取数据
@Test
void testRead03() throws Exception {
FileInputStream fileInputStream=new FileInputStream("E:\\code\\pig\\test.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
Sheet sheetAt = workbook.getSheetAt(0);
Row row = sheetAt.getRow(0);
Cell cell = row.getCell(0);
String stringCellValue = cell.getStringCellValue();//一定注意数据类型
System.out.println(stringCellValue);//hello
fileInputStream.close();
}
@Test
void testRead07() throws Exception {
FileInputStream fileInputStream=new FileInputStream("E:\\code\\pig\\test.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
Sheet sheetAt = workbook.getSheetAt(0);
Row row = sheetAt.getRow(0);
Cell cell = row.getCell(0);
String stringCellValue = cell.getStringCellValue();//一定注意数据类型
System.out.println(stringCellValue);//hello
fileInputStream.close();
}
获取值的时候要注意数据类型
row.getPhysicalNumberOfCells();//获取某一行有数据的个数
cell.getCellType()//单元格的数据类型,对应的是CellType.STRING,CellType是枚举,原来是CELL_TYPE_STRING
//获取公式
if(cell.getCellType()== CellType.FORMULA){
FormulaEvaluator formulaEvaluator=new HSSFFormulaEvaluator(workbook);
CellValue evaluate = formulaEvaluator.evaluate(cell);
System.out.println(evaluate.getNumberValue());
}
循环遍历每一个cell并且要判断数据类型,最好用switch
easyExcel
官网https://www.yuque.com/easyexcel/doc/easyexcel
添加pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
</dependency>
记得把先前导入的poi依赖去掉,easyexcel其实也是使用的poi,是对poi的封装,变得简单可用
写数据
//easyexcel是通过实体类来写数据的
@Data
public class DemoData {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
/**
* 忽略这个字段
*/
@ExcelIgnore
private String ignore;
}
/**
* 最简单的写
* <p>1. 创建excel对应的实体对象 参照{@link DemoData}
* <p>2. 直接写即可
*/
@Test
public void simpleWrite() {
// 写法1
String fileName = "E:\\code\\pig\\" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
// 写法2
/*fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写
ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcel.write(fileName, DemoData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet);
} finally {
// 千万别忘记finish 会帮忙关闭流
if (excelWriter != null) {
excelWriter.finish();
}
}*/
}
private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + i);
data.setDate(new Date());
data.setDoubleData(0.56);
list.add(data);
}
return list;
}
在官网还有很多对写数据时excel的样式设置
读数据
官网默认是直接给的存到数据库的例子
public class DemoDAO {
public void save(List<DemoData> list) {
// 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
/* for (DemoData demoData : list) {
System.out.println(demoData);
}*/
}
}
public class DemoDataListener extends AnalysisEventListener<DemoData> {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
/**
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<DemoData> list = new ArrayList<DemoData>();
/**
* 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。
*/
private DemoDAO demoDAO;
public DemoDataListener() {
// 这里是demo,所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
demoDAO = new DemoDAO();
}
/**
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
*
* @param demoDAO
*/
public DemoDataListener(DemoDAO demoDAO) {
this.demoDAO = demoDAO;
}
/**
* 这个每一条数据解析都会来调用
*
* @param data
* one row value. Is is same as {@link AnalysisContext#readRowHolder()}
* @param context
*/
@Override
public void invoke(DemoData data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data);
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
if (list.size() >= BATCH_COUNT) {
saveData();
// 存储完成清理 list
list.clear();
}
}
/**
* 所有数据解析完成了 都会来调用
*
* @param context
*/
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 这里也要保存数据,确保最后遗留的数据也存储到数据库
saveData();
LOGGER.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveData() {
LOGGER.info("{}条数据,开始存储数据库!", list.size());
demoDAO.save(list);
LOGGER.info("存储数据库成功!");
}
}
@Test
public void simpleRead() {
// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
// 写法1:
String fileName = "E:\\code\\pig\\" + "simpleWrite1610502469259.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
// 写法2:
/*fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
ExcelReader excelReader = null;
try {
excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet);
} finally {
if (excelReader != null) {
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish();
}
}*/
}
读数据时设置每读取多少条数据就先存进数据库,然后清理掉内存防止oom
#测试数据
10:15:28.850 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串0"}
10:15:28.851 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串1"}
10:15:28.852 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串2"}
10:15:28.853 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串3"}
10:15:28.853 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串4"}
10:15:28.854 [main] INFO com.zhu.excel.pojo.DemoDataListener - 5条数据,开始存储数据库!
10:15:28.854 [main] INFO com.zhu.excel.pojo.DemoDataListener - 存储数据库成功!
10:15:28.854 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串5"}
10:15:28.856 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串6"}
10:15:28.857 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串7"}
10:15:28.857 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串8"}
10:15:28.858 [main] INFO com.zhu.excel.pojo.DemoDataListener - 解析到一条数据:{"date":1610502469000,"doubleData":0.56,"string":"字符串9"}
10:15:28.858 [main] INFO com.zhu.excel.pojo.DemoDataListener - 5条数据,开始存储数据库!
10:15:28.858 [main] INFO com.zhu.excel.pojo.DemoDataListener - 存储数据库成功!
10:15:28.858 [main] INFO com.zhu.excel.pojo.DemoDataListener - 0条数据,开始存储数据库!
10:15:28.858 [main] INFO com.zhu.excel.pojo.DemoDataListener - 存储数据库成功!
10:15:28.858 [main] INFO com.zhu.excel.pojo.DemoDataListener - 所有数据解析完成!
读取时官网也给了很多例子
也给了很多常见的api
套路:
写数据:通过实体类写入数据
读数据:通过监听器实现,每读取一行就可以走一次监听器