@AutoLog(value = "导入excel")
@ApiOperation(value = "导入excel", notes = "需要传入的参数为excel文件的绝对路径:filePhth 需要注意:excel表格的第一列为空(由于某些原因获取不到第一列的值)")
@PostMapping(value = "/importExcel")
public Result<?> importExcel(String filePath) {
List<Saler> infoList = new ArrayList<Saler>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
infoList.clear();
try {
InputStream is = new FileInputStream(filePath);
int index = filePath.lastIndexOf(".");
String postfix = filePath.substring(index + 1);
Workbook workbook = null;
if ("xls".equals(postfix)) {
workbook = new HSSFWorkbook(is);
} else if ("xlsx".equals(postfix)) {
workbook = new XSSFWorkbook(is);
}
// 获取第1张表
Sheet sheet = workbook.getSheetAt(0);
// 总的行数
int rows = sheet.getLastRowNum();
// 总的列数--->最后一列为null则有问题,读取不完整,将表头的数目作为总的列数,没有的则补为null
int columns = sheet.getRow(0).getLastCellNum();
// 先列后行
for (int i = 1; i <= rows; i++) {
Row row = sheet.getRow(i);
if (null != row && row.getFirstCellNum() == -1) {// 这一行是空行,不读取
continue;
}
// 这一行的总列数
// columns = row.getLastCellNum();
List<String> contentList = new ArrayList<String>();
contentList.clear();
for (int j = 1; j < columns; j++) {
if (row.getCell(j) != null) {
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
contentList.add(row.getCell(j).getStringCellValue());
} else {
contentList.add("");
}
}
map.put("StorageInfo" + i, contentList);
}
// 遍历map集合,封装成bean
excel导入导出
最新推荐文章于 2023-08-26 19:04:52 发布