//可解决excel中空格转为空字符串的问题 Row.MissingCellPolicy.CREATE_NULL_AS_BLANK
row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).toString()!=null
public static List<Merchant> importMer(MultipartFile file, HttpServletResponse response)
throws InvalidFormatException, DxException {
List<Merchant> mList = new ArrayList<>();
if (file.isEmpty()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return null;
} else {
try {
// InputStream stream = file.getInputStream();
Workbook workbook = WorkbookFactory.create(file.getInputStream());
Sheet sheet0 = workbook.getSheetAt(0);
for (Row row : sheet0) {
Merchant mer = new Merchant(); //要放入模板数据的实体类
if (row.getRowNum() <= 0)
continue; // 跳过第一行标题
System.out.println(row.getRowNum() + "行数");
if ("".equals(row.getCell(0).toString())) {// 从第二行第一格开始
//自定义异常见另一篇文章
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行的内容是否为空!");
} else {
mer.setName(row.getCell(0).toString());
}
if ("".equals(row.getCell(1).toString())) {// 第二个
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行 的内容是否为空!");
} else {
mer.setIntroduce(row.getCell(1).toString());
}
if ("".equals(row.getCell(2).toString())) {// 第三格
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行 的内容是否为空!");
} else {
mer.setType(var);
}
if ("".equals(row.getCell(3).toString())) {// 第四格
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行 的内容是否为空!");
} else {
mer.setcc(row.getCell(3).toString());
}
if ("".equals(row.getCell(4).toString())) {// 第五格
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行 的内容是否为空!");
} else {
mer.setNo(row.getCell(4).toString());
}
if ("".equals(row.getCell(5).toString())) {// 第六格
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行 的内容是否为空!");
} else {
mer.setBus(row.getCell(4).toString());
}
if ("".equals(row.getCell(6).toString())) {// 第七格
throw new DxException("请注意导入的第" + (row.getRowNum() + 1) + "行 的内容是否为空!");
} else {
mer.setPvince(row.getCell(6).toString());
}
mList.add(mer);
}
}
} catch (IOException e) {
//dx异常抛出
e.printStackTrace();
}
return mList;
}
读取excel中模板的内容
于 2021-09-24 10:51:42 首次发布