技术概述
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
学习该技术的原因:因为项目需要解析用户上传的Excel的数据。
技术详述
流程图:
Excel内容:
传入一个EXCEL文件并读取。
public static List<Student> getExcelInfo(MultipartFile mFile) {
String fileName = mFile.getOriginalFilename();
if (fileName == null) {
fileName = "";
}
List<Student> stuList = null;
try {
if (!validateExcel(fileName)) {
return null;
}
boolean isExcel2003 = true;
if (isExcel2007(fileName)) {
isExcel2003 = false;
}
stuList = createExcel(mFile.getInputStream(), isExcel2003);
} catch (Exception e) {
e.printStackTrace();
}
return stuList;
}
根据Excel里面的内容读取客户信息。
public static List<Student> createExcel(InputStream is, boolean isExcel2003) {
List<Student> stuList = null;
try{
Workbook wb = null;
if (isExcel2003) { // 当excel是2003时,创建excel2003
wb = new HSSFWorkbook(is);
} else { // 当excel是2007时,创建excel2007
wb = new XSSFWorkbook(is);
}
stuList = readExcelValue(wb);
} catch (IOException e) {
e.printStackTrace();
}
return stuList;
}
读取Excel里面的信息。
private static List<Student> readExcelValue(Workbook wb) {
//获取第一个Sheet
Sheet sheet = wb.getSheetAt(0);
totalRows = sheet.getPhysicalNumberOfRows();
if (totalRows > 1 && sheet.getRow(0) != null) {
totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
List<Student> studentList = new ArrayList<Student>();
//遍历该Sheet的行
for (int r = 1; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null){
continue;
}
Student student = new Student();
//遍历该行的列
for (int c = 0; c < totalCells; c++) {
Cell cell = row.getCell(c);
if (c == 0) {
student.setAccount(getValue(cell));
} else if(c == 1) {
student.setStudentName(getValue(cell));
} else if(c == 2) {
student.setEmail(getValue(cell));
}
}
studentList.add(student);
}
return studentList;
}
解决excel类型问题。
public static String getValue(Cell cell) {
String value = "";
if(null==cell){
return value;
}
switch (cell.getCellType()) {
//数值型
case NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = format.format(date);;
}else {
BigDecimal big= BigDecimal.valueOf(cell.getNumericCellValue());
value = big.toString();
if(null!=value&&!"".equals(value.trim())){
String[] item = value.split("[.]");
if(1<item.length&&"0".equals(item[1])){
value=item[0];
}
}
}
break;
//字符串类型
case STRING:
value = cell.getRichStringCellValue().toString();
break;
//公式类型
case FORMULA:
//读公式计算值
value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {
value = cell.getStringCellValue().toString();
}
break;
//布尔类型
case BOOLEAN:
value = ""+ cell.getBooleanCellValue();
break;
default:
value = cell.getStringCellValue().toString();
}
if("null".endsWith(value.trim())){
value="";
}
return value;
}
验证Excel格式。
public static boolean validateExcel(String filePath) {
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
errorMsg = "文件名不是excel格式";
return false;
}
return true;
}
技术使用中遇到的问题和解决过程
不懂怎么解析Excel。去百度并学习了用poi解析Excel,读取Excel中的数据。
总结
POI可以很方便地对Excel进行解析,读取Excel中的数据。
参考文献、参考博客
SSM中使用POI实现excel的导入导出
POI操作excel基础用法详解
SSM项目的excel文件上传并添加到数据库
java解析excel解决excel类型问题