public Result importGoodsInfoExcel(
@RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
List result = this.fileToObject(provinceCode, cityCode, file, createBy, createByName);
// 返回错误信息
if (result.get(0) instanceof ExcelErrorInfo) {
StringBuilder sb = new StringBuilder();
ExcelErrorInfo exceerror1 = new ExcelErrorInfo();
for (Object exceerror : result) {
exceerror1 = (ExcelErrorInfo) exceerror;
if (exceerror1.getColumn().equals("文件格式不正确,请导入excel文件")) {
exceerror1.setMsg("文件格式不正确,请导入excel文件");
sgecResult.setDatas(exceerror1);
return super.outJsonStringFail(sgecResult);
} else {
// sb.append("sheet名为").append(exceerror1.getSheetNo()).append("的")
// .append(sb.append("第").append(exceerror1.getRow()).append("行"))
// .append(exceerror1.getColumn()).append(exceerror1.getError()).append(";");
sb.append("sheet名为").append(exceerror1.getSheetNo()).append("的").
append("第").append(exceerror1.getRow()).append("行")
.append(exceerror1.getColumn()).append(exceerror1.getError()).append(";");
}
}
exceerror1.setMsg(sb.toString());
exceerror1.setError(null);
exceerror1.setRow(null);
exceerror1.setColumn(null);
sgecResult.setDatas(exceerror1);
// sgecResult.setDatas(result);
return super.outJsonStringFail(sgecResult);
--------------------------------------------
private List fileToObject(MultipartFile file, String createBy,
) throws IOException, ParseException {
List<实体类> powerDevicesList = new ArrayList<实体类>();
// 创建错误信息集合
List
// MultipartFile file=pdfi.getFile();
String fileName = file.getOriginalFilename();
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!extension.equals("xls") && !extension.equals("xlsx")) {
ExcelErrorInfo exceerror = new ExcelErrorInfo();
exceerror.setColumn("文件格式不正确,请导入excel文件");
errorList.add(exceerror);
return errorList;
}
Workbook workbook = null;
if (extension.equals("xlsx")) {
workbook = new XSSFWorkbook(file.getInputStream());
} else if (extension.equals("xls")) {
workbook = new HSSFWorkbook(file.getInputStream());
}
int numberOfSheets = workbook.getNumberOfSheets();
// 循环sheet
for (int iout = 0; iout < numberOfSheets; iout++) {
Sheet sheet = workbook.getSheetAt(iout);
// 获得到最后一行的行数
int end = sheet.getLastRowNum();
// 跳出空行
for (int i = 1; i <= end; i++) {
if (isRowEmpty(sheet.getRow(i))) {
continue;
}
Row row = sheet.getRow(i);
// 年份 getStringCellValue()这个函数转为String类型
String yearv = getStringCellValue(row.getCell(0)).trim();
// 月份
String monthv = getStringCellValue(row.getCell(1)).trim();
// 第几天
String dayv = getStringCellValue(row.getCell(2)).trim();
// 第1小时
String hour1 = getStringCellValue(row.getCell(3)).trim();
// 和下一行的某一列比较判断重复
for (int j = i + 1; j <= end; j++) {
if (null == sheet.getRow(j)) {
continue;
}
}
XnySplydemandLoadPredict xslp = new XnySplydemandLoadPredict();
xslp.setProvinceCode(provinceCode);
xslp.setCityCode(cityCode);
// xslp.setCountyCode(countyCode);
xslp.setYearv(yearv);
xslp.setMonthv(monthv);
xslp.setDayv(dayv);
xslp.setCreateBy(createBy);
xslp.setCreateByName(createByName);
xslp.setCreateOn(new Date());
// 校验填写小时数据是否是数字
if (!StringUtils.isEmpty(hour1)) {
if (!this.isInteger(hour1)) {
ExcelErrorInfo ei = new ExcelErrorInfo();
ei.setSheetNo(sheet.getSheetName());
ei.setRow(i + 1);
ei.setColumn("D列");
ei.setError("列值必须是数字");
errorList.add(ei);
} else {
xslp.setHourv1(BigDecimal.valueOf(Integer.parseInt(hour1)));
}
}
powerDevicesList.add(xslp);
}
}
if (errorList.size() > 0) {
return errorList;
}
return powerDevicesList;
}
-------------------------------
public class ExcelErrorInfo { private String sheetNo; //sheet private Integer row; //行 private String error; //错误 private String column; //列 private String msg; //信息 }
----------------------
/** * 获取单元格值(转为String类型) */ private String getStringCellValue(Cell cell) { if (cell == null) return ""; String strCell = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: strCell = String.valueOf((cell.getNumericCellValue())); break; case Cell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } return strCell; }
---------------------
/** * TODO 判断是否是空行 * * @return boolean * @Param [row] **/ public static boolean isRowEmpty(Row row) { for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) return false; } return true; }