maven jar 依赖包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
工具类:
/**
* 获取Excel 指定单元格的内容
*
* @param obj
* @param i
* @return
*/
public static String getValue(Object obj, int i) {
if (obj instanceof XSSFRow) {
XSSFRow row = (XSSFRow) obj;
if (null == row.getCell(i)) {
return null;
}
row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
return row.getCell(i).getStringCellValue();
}
if (obj instanceof HSSFRow) {
HSSFRow row = (HSSFRow) obj;
if (null == row.getCell(i)) {
return null;
}
row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
return row.getCell(i).getStringCellValue();
}
return null;
}
```java
Map<String, String> data = new HashMap<>();
StringBuffer sb = new StringBuffer();
if (file == null || file.isEmpty()) {
data.put("result", "fail");
data.put("message", "上传失败,内容为空");
return data;
}
if (file.getSize() > 5 * 1024 * 1024) {
data.put("result", "fail");
data.put("message", "上传文件过大,请确认小于5M");
return data;
}
String fileName = file.getOriginalFilename();
// 获取文件后缀格式
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
System.out.println(fileType);
if (!"xls".equals(fileType) && !"xlsx".equals(fileType)) {
data.put("result", "fail");
data.put("message", "上传文件格式错误,仅支持.xls或.xlsx");
return data;
}
// 用来检测程序运行时间
long startTime = System.currentTimeMillis();
System.out.println("fileName:" + file.getOriginalFilename());
try {
// 获取输入流 CommonsMultipartFile 中可以直接得到文件的流
InputStream is = file.getInputStream();
try {
// 读取excel
org.apache.poi.ss.usermodel.Workbook wb = WorkbookFactory.create(is);
// 获取excel中的数据拼成list
Map<String, String> map = getXSSFResultOrHSSFResult(wb);
//处理拼装返回结果
} catch (Exception e) {
data.put("result", "fail");
data.put("message", "文件版本有误,目前只支持 2007/2003版本");
return data;
}
//关闭流
is.close();
data.put("result", "success");
data.put("message", sb.toString());
} catch (
Exception e) {
data.put("result", "fail");
data.put("message", "上传失败,系统异常:" + e.getMessage());
logger.error("文件上传失败", e);
}
long endTime = System.currentTimeMillis();
System.out.println("方法一的运行时间:" + (endTime - startTime) + "ms");
System.out.println(data.get("message"));
return data;
解析文件中的内容
/**
* 解析导入文件 并进行注册
*
* @return
*/
public Map getXSSFResultOrHSSFResult(org.apache.poi.ss.usermodel.Workbook wb) {
Map<String, String> map = new HashMap<>();
//记录总的检索到的手机号--空不算
int total_num = 0, success_num = 0;
//未识别的手机号
StringBuffer unidentified_phone = new StringBuffer();
//已注册的手机号
StringBuffer registered_phone = new StringBuffer();
//注册失败的手机号
StringBuffer error_info = new StringBuffer();
//excel版本2007
if (wb instanceof HSSFWorkbook) {
try {
// 循环工作表Sheet
for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = (HSSFSheet) wb.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row 从第二行开始取数据
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow xssfRow = hssfSheet.getRow(rowNum);
if (null == xssfRow) {
continue;
}
//封住客户参数信息
getCustomerReqModel(xssfRow);
}
}
map.put("total_num", String.valueOf(total_num));
map.put("success_num", String.valueOf(success_num));
map.put("unidentified_phone", unidentified_phone.toString());
map.put("registered_phone", registered_phone.toString());
map.put("error_info", error_info.toString());
return map;
} catch (Exception e) {
logger.error("文件解析失败!", e);
throw new OpenplatKnownException("", "文件解析失败!");
}
} else if (wb instanceof XSSFWorkbook) {
//excel版本2003
try {
// 循环工作表Sheet
for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = (HSSFSheet) wb.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
XSSFSheet xssfSheet = (XSSFSheet) wb.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// 循环行Row 从第二行开始取数据
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (null == xssfRow) {
continue;
}
//处理业务
getCustomerReqModel(xssfRow);
total_num++;
}
}
map.put("total_num", String.valueOf(total_num));
map.put("success_num", String.valueOf(success_num));
map.put("unidentified_phone", unidentified_phone.toString());
map.put("registered_phone", registered_phone.toString());
map.put("error_info", error_info.toString());
return map;
} catch (Exception e) {
logger.error("文件解析失败!", e);
throw new OpenplatKnownException("", "文件解析失败!");
}
} else {
map.put("error_info", "暂不支持该文件版本");
return map;
}
}
//解析封装Excel文件中的内容
private void getCustomerReqModel(Object xssfRow) {
//模板内容
String customerName = XSSFWorkUtil.getValue(xssfRow, 0);
String businessName = XSSFWorkUtil.getValue(xssfRow, 1);
String phone = XSSFWorkUtil.getValue(xssfRow, 2);
String email = XSSFWorkUtil.getValue(xssfRow, 3);
String constantName = XSSFWorkUtil.getValue(xssfRow, 4);
String constantTel = XSSFWorkUtil.getValue(xssfRow, 5);
String constantAdders = XSSFWorkUtil.getValue(xssfRow, 5);
String txNo = XSSFWorkUtil.getValue(xssfRow, 5);
}