java 代码读取excel内容。
使用阿里巴巴easyexcel读取excel内容。
##excel内容
##依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.5</version>
</dependency>
##实体类TableExcelInfo
package com.yym.entity;
import com.alibaba.excel.annotation.ExcelProperty;
import java.io.Serializable;
public class TableExcelInfo implements Serializable {
@ExcelProperty("表名称")
private String c_A;
@ExcelProperty("用途")
private String c_B;
@ExcelProperty("数量")
private int c_C;
@ExcelProperty("框架&业务共用")
private String c_D;
@ExcelProperty("是否再用")
private String c_E;
public String getC_A() {
return c_A;
}
public void setC_A(String c_A) {
this.c_A = c_A;
}
public String getC_B() {
return c_B;
}
public void setC_B(String c_B) {
this.c_B = c_B;
}
public int getC_C() {
return c_C;
}
public void setC_C(int c_C) {
this.c_C = c_C;
}
public String getC_D() {
return c_D;
}
public void setC_D(String c_D) {
this.c_D = c_D;
}
public String getC_E() {
return c_E;
}
public void setC_E(String c_E) {
this.c_E = c_E;
}
}
##读取excel工具类ReadExcelUtils
package com.yym.utils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSONObject;
import com.yym.entity.TableExcelInfo;
import java.util.ArrayList;
import java.util.List;
public class ReadExcelUtils {
/**
* 单次缓存的数据量
*/
public static final int BATCH_COUNT = 100000;
/**
*临时存储
*/
public static List<TableExcelInfo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
public static List<String> tablesName = new ArrayList<>();
public static void readExcel(String fileName, String sheetName) {
EasyExcel.read(fileName, TableExcelInfo.class, new ReadListener<TableExcelInfo>() {
@Override
public void invoke(TableExcelInfo data, AnalysisContext context) {
cachedDataList.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
for(int i=0;i<cachedDataList.size();i++) {
System.out.println(JSONObject.toJSONString(cachedDataList.get(i)));
if("是".equals(cachedDataList.get(i).getC_E())) {
tablesName.add(cachedDataList.get(i).getC_A());
}
}
System.out.println(JSONObject.toJSONString(tablesName));
}
}).sheet(sheetName).doRead();
}
public static void main(String[] args) {
readExcel("F:\\information_schema.xlsx", "Sheet1");
}
}
##调用工具类
ReadExcelUtils.readExcel("F:\\\\information_schema.xlsx", "Sheet3");
##结果