RuoYi/src/main/java/com/ruoyi/common/utils/ExcelImportUtils.java
yangzhengze dcbd2b2210 1.优化上传个人头像错误提示框。
2.增加bootstrap fileinput插件。
3.增加通过Excel文件批量添加用户功能。
2018-06-05 23:03:11 +08:00

79 lines
2.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.common.utils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import java.text.DecimalFormat;
import java.util.Date;
/**
* 导入Excel工具类
*/
public class ExcelImportUtils {
/** 是否是2003的excel返回true是2003Excel文件**/
public static boolean isExcel2003(String filePath){
return filePath.matches("^.+\\.(?i)(xls)$");
}
/** 是否是2007以上的excel返回true是2007Excel文件**/
public static boolean isExcel2007(String filePath){
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
/**
* 验证EXCEL文件
*
* @param filePath
* @return
*/
public static boolean validateExcel(String filePath) {
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
return false;
}
return true;
}
/**
* 获取单元格的值
* @param cell
* @return
*/
public static String getCellValue(Cell cell) {
String value = "";
if (cell != null) {
switch(cell.getCellTypeEnum()){
case NUMERIC:// 数字
value = cell.getNumericCellValue()+ " ";
if(HSSFDateUtil.isCellDateFormatted(cell)){
Date date = cell.getDateCellValue();
if(date != null){
value = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date); // 日期格式化
}else{
value = "";
}
}else {
// 解析cell时候 数字类型默认是double类型的 但是想要获取整数类型 需要格式化
value = new DecimalFormat("0").format(cell.getNumericCellValue());
}
break;
case STRING: // 字符串
value = cell.getStringCellValue();
break;
case BOOLEAN: // Boolean类型
value = cell.getBooleanCellValue()+"";
break;
case BLANK: // 空值
value = "";
break;
case ERROR: // 错误类型
value ="非法字符";
break;
default:
value = "未知类型";
break;
}
}
return value.trim();
}
}