解析Excel表格的工具类
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.8</version>
</dependency>
import bigdata.itcast.cn.es.bean.EsArticle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelParseUtil {
public static List<EsArticle> parseExcel(String path) throws IOException {
List<EsArticle> esArticles = new ArrayList<>();
FileInputStream inputStream = new FileInputStream(path);
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
XSSFSheet sheet = sheets.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for(int i = 1;i<=lastRowNum ;i++){
XSSFRow row = sheet.getRow(i);
String id = row.getCell(0).toString();
String title = row.getCell(1).toString();
String from = row.getCell(2).toString();
String time = row.getCell(3).toString();
String readCount = row.getCell(4).toString();
String content = row.getCell(5).toString();
EsArticle esArticle = new EsArticle(id, title, from, time, readCount, content);
esArticles.add(esArticle);
}
return esArticles;
}
}