接口读取
package com.qiangesoft.easyexcel.controller;
import com.qiangesoft.easyexcel.read.ReadData;
import com.qiangesoft.easyexcel.read.ExcelRead;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/easyExcel")
public class EasyExcelController {
private static final String fileName = "demo/demo.xlsx";
@GetMapping("/read")
public List<ReadData> read() throws IOException {
ExcelRead.read(fileName);
ExcelRead.read(fileName, 0);
ExcelRead.read(fileName, 0, 1);
ExcelRead.readExtra(fileName);
ExcelRead.readBuiltIn(fileName);
ExcelRead.readAnonymous(fileName);
ExcelRead.readAllSheet(fileName);
ExcelRead.readManySheet(fileName);
List<ReadData> readDataList = ExcelRead.readSync(fileName);
return readDataList;
}
}
读取工具类
package com.qiangesoft.easyexcel.read;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.ListUtils;
import com.qiangesoft.easyexcel.common.ExcelStringConverter;
import com.qiangesoft.easyexcel.util.ResourceFileUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Slf4j
public class ExcelRead {
public static void read(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
EasyExcel.read(inputStream, ReadData.class, new ReadDataListener())
.sheet()
.doRead();
}
public static void read(String fileName, int sheetNo) throws IOException {
read(fileName, sheetNo, 1);
}
public static void read(String fileName, int sheetNo, int headRowNumber) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
try (com.alibaba.excel.ExcelReader excelReader = EasyExcel.read(inputStream, ReadData.class, new ReadDataListener()).build()) {
ReadSheet readSheet = EasyExcel.readSheet(sheetNo).headRowNumber(headRowNumber).build();
excelReader.read(readSheet);
}
}
public static void readAllSheet(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
EasyExcel.read(inputStream, ReadData.class, new ReadDataListener()).doReadAll();
}
public static void readManySheet(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
try (com.alibaba.excel.ExcelReader excelReader = EasyExcel.read(inputStream).build()) {
ReadSheet readSheet1 = EasyExcel.readSheet(0).head(ReadData.class).registerReadListener(new ReadDataListener()).build();
ReadSheet readSheet2 = EasyExcel.readSheet(1).head(ReadData.class).registerReadListener(new ReadDataListener()).build();
excelReader.read(readSheet1, readSheet2);
}
}
public static void readExtra(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
EasyExcel.read(inputStream, ReadData.class, new ReadDataListener())
.registerConverter(new ExcelStringConverter())
.extraRead(CellExtraTypeEnum.COMMENT)
.extraRead(CellExtraTypeEnum.HYPERLINK)
.extraRead(CellExtraTypeEnum.MERGE)
.sheet()
.doRead();
}
public static List<ReadData> readSync(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
return EasyExcel.read(inputStream)
.head(ReadData.class)
.sheet()
.doReadSync();
}
public static void readBuiltIn(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
EasyExcel.read(inputStream, ReadData.class, new PageReadListener<ReadData>(dataList -> {
log.info("{}条数据存储数据库成功!", dataList.size());
}, 100)).sheet().doRead();
}
public static void readAnonymous(String fileName) throws IOException {
InputStream inputStream = ResourceFileUtil.getResourceByClassPathResource(fileName);
EasyExcel.read(inputStream, ReadData.class, new ReadListener<ReadData>() {
private static final int BATCH_COUNT = 100;
private List<ReadData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
@Override
public void invoke(ReadData data, AnalysisContext context) {
cachedDataList.add(data);
if (cachedDataList.size() >= BATCH_COUNT) {
this.saveData();
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
this.saveData();
log.info("所有数据解析完成!");
}
private void saveData() {
log.info("{}条数据存储数据库成功!", cachedDataList.size());
}
}).sheet().doRead();
}
}
读取监听器
package com.qiangesoft.easyexcel.read;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
@Slf4j
public class ReadDataListener implements ReadListener<ReadData> {
private static final int BATCH_COUNT = 100;
private List<ReadData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
public ReadDataListener() {
}
@Override
public void onException(Exception exception, AnalysisContext context) {
if (exception instanceof ExcelDataConvertException) {
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
}
}
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
log.info("{}条头部解析完成!", headMap.size());
}
@Override
public void invoke(ReadData data, AnalysisContext context) {
cachedDataList.add(data);
if (cachedDataList.size() >= BATCH_COUNT) {
this.saveData();
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
this.saveData();
log.info("所有数据解析完成!");
}
private void saveData() {
log.info("{}条数据存储数据库成功!", cachedDataList.size());
}
}
读取类
package com.qiangesoft.easyexcel.read;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.qiangesoft.easyexcel.common.ExcelStringConverter;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Getter
@Setter
@EqualsAndHashCode
public class ReadData {
@ExcelProperty(index = 0, converter = ExcelStringConverter.class)
private String string;
@DateTimeFormat("yyyy年MM月dd日 HH时mm分ss秒")
@ExcelProperty(index = 1)
private Date date;
@NumberFormat("#.##%")
@ExcelProperty(value = "数字")
private Double doubleData;
}
资源文件工具类
package com.qiangesoft.easyexcel.util;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import java.io.IOException;
import java.io.InputStream;
public class ResourceFileUtil {
public static String getProjectPath() {
return System.getProperty("user.dir");
}
public static String getClassPath() {
return ResourceFileUtil.class.getClass().getResource("/").getPath();
}
public static String getClassPathByClassLoader() {
return ClassUtils.getDefaultClassLoader().getResource("").getPath();
}
public static InputStream getResourceByClassPathResource(String fileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(fileName);
return classPathResource.getInputStream();
}
public static InputStream getResourceByClassLoader(String fileName) {
return ResourceFileUtil.class.getClassLoader().getResourceAsStream(fileName);
}
public static InputStream getResourceByResourceLoader(String fileName) throws IOException {
Resource resource = new DefaultResourceLoader().getResource(fileName);
return resource.getInputStream();
}
public static void main(String[] args) throws IOException {
System.out.println(getResourceByResourceLoader("com/qiangesoft/easyexcel/EasyexcelApplication.class"));
}
}