导包
<!-- 阿里巴巴Excel工具组件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.4</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
</exclusions>
</dependency>
<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>
工具类
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
public class YcsExcelUtils {
public static List<List<String>> parseToContent(InputStream inputStream) {
List<List<String>> result = new ArrayList<>();
EasyExcel.read(inputStream, new ContentListener(result)).sheet().doRead();
return result;
}
public static <T> List<T> parseToObject(InputStream inputStream, Class<T> type) {
List<T> result = new ArrayList<>();
EasyExcel.read(inputStream, type, new ObjectListener(result)).sheet().doRead();
return result;
}
public static <T> void objectToExcel(Class<?> type, List<T> data, OutputStream outputStream) {
EasyExcel.write(outputStream, type).sheet().doWrite(data);
}
public static void contentToExcel(List<List<String>> head, List<List<Object>> data, OutputStream outputStream) {
EasyExcel.write(outputStream).head(head).sheet().doWrite(data);
}
}
class ObjectListener<T> extends AnalysisEventListener<T> {
private final List<T> result;
public ObjectListener(List<T> result) {
this.result = result;
}
@Override
public void invoke(T t, AnalysisContext analysisContext) {
result.add(t);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
class ContentListener extends AnalysisEventListener<Map<Integer, String>> {
private final List<List<String>> result;
public ContentListener(List<List<String>> result) {
this.result = result;
}
@Override
public void invoke(Map<Integer, String> rowMap, AnalysisContext analysisContext) {
Set<Integer> colIndexSet = rowMap.keySet();
List<String> rowList = new ArrayList<>();
for (int colIndex : colIndexSet) {
rowList.add(rowMap.get(colIndex));
}
result.add(rowList);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
测试
public class SelfTest {
private File desktopDir;
private String desktopPath;
@Before
public void desktopDirTest() {
desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
desktopPath = desktopDir.getAbsolutePath();
}
@Test
public void ExcelUtils() throws FileNotFoundException {
InputStream inputStream = new FileInputStream(desktopPath + "/year.xlsx");
List<List<String>> lists = YcsExcelUtils.parseToContent(inputStream);
System.out.println(JSON.toJSONString(lists));
List<ChannelInit> channelInits = YcsExcelUtils.parseToObject(inputStream, ChannelInit.class);
System.out.println(JSON.toJSONString(channelInits));
}
}