操作excel文件,此方法是我对我开发场景加工过的,使用时可稍加改造
/**读取Excel
* @author L_DY
* @version 1.0
* @date 2021/1/18 10:25
*/
public class ExcelReadUtil {
/*
*
* @param args
* @author L_DY
* @date 2021/1/18 10:25
* @return void
*/
public Map<String,List<String>> read(int fristIndex,int lastIndex,String path) throws IOException {
List<String> mapList=new ArrayList<>();//存放地图坐标
List<String> cellList = new ArrayList<>();//货位编码
Map<String,List<String>> excelMap = new HashMap<>();
//获取文件
XSSFWorkbook workbook = new XSSFWorkbook(path);
//获取文件的sheet
for (int i = 0; i <=lastIndex; i++) {
XSSFSheet sheetAt = workbook.getSheetAt(fristIndex);
//遍历文件,获取行
for (Row cells : sheetAt) {
//获取当前行的单元格
for (Cell cell : cells) {
//获取单元格的内容
cell.setCellType(CellType.STRING);//设置获取的单元格内容格式
String value = cell.getStringCellValue();//获取单元格内容
if (StrUtil.isEmpty(value)){
continue;
}
if(fristIndex%2==0){
mapList.add(value);
}else {
cellList.add(value);
}
}
}
fristIndex++;
// if (fristIndex==lastIndex){
// break;
// }
}
//释放资源
workbook.close();
excelMap.put("cellList",cellList);
excelMap.put("mapList",mapList);
return excelMap;
}