poi解析excel表格数据
前提:maven项目
1.导入依赖
<!--poi包-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.8</version>
</dependency>
2.编写工具类
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PoiExcelUtil {
private Sheet sheet;//表格实例
private List<Map<Integer,String>> datalist;//保存每个单元格的数据
/**
* 读取excel文件,创建表格实例
* @param path
*/
public void loadExcel(String path){
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(new File(path));
Workbook workbook= WorkbookFactory.create(inputStream);
sheet=workbook.getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取单元格的值
* @param cell
* @return
*/
public String getCellValue(Cell cell) {
String cellValue="";
DataFormatter formatter=new DataFormatter();
if (cell!=null) {
//判断单元格数据的类型,不同类型调用不同的方法
switch (cell.getCellType()) {
//数值类型
case Cell.CELL_TYPE_NUMERIC:
//进一步判断 ,单元格格式是日期格式
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = formatter.formatCellValue(cell);
} else {
//数值
double value = cell.getNumericCellValue();
int intValue = (int) value;
cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
}
break;
case Cell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
//判断单元格是公式格式,需要做一种特殊处理来得到相应的值
case Cell.CELL_TYPE_FORMULA: {
try {
cellValue = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
cellValue = String.valueOf(cell.getRichStringCellValue());
}
}
break;
case Cell.CELL_TYPE_BLANK:
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR:
cellValue = "";
break;
default:
cellValue = cell.toString().trim();
break;
}
}
return cellValue.trim();
}
/**
* 初始化表格中的每一行,并且得到每一个单元格的值
*/
public void init(){
int rowNum=sheet.getLastRowNum()+1;
datalist=new ArrayList<>();
for (int i=0;i<rowNum;i++){
Row row=sheet.getRow(i);
Map<Integer,String> obj=new HashMap<>();
obj.put(i,getCellValue(row.getCell(0)));
datalist.add(obj);
}
}
public List<Map<Integer, String>> getDatalist() {
return datalist;
}
}
3.测试
public static void main(String[] arg){
PoiExcelUtil poiExcelUtil=new PoiExcelUtil();
poiExcelUtil.loadExcel(path);
poiExcelUtil.init();
String emailAndPwd=poiExcelUtil.getDatalist().get(0).get(0);
String info[]=emailAndPwd.split("----");
SSOAction ssoAction=new SSOAction();
Result result=ssoAction.interRegister(info[0],info[1]);
System.out.println(result.getCode());
}