目录:
/注意,这里的两个参数,第一个是表示列的,第二才表示行
cell=sheet.getCell(j, i);
//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
if(cell.getType()==CellType.NUMBER){
System.out.print(((NumberCell)cell).getValue());
}
else if(cell.getType()==CellType.DATE){
System.out.print(((DateCell)cell).getDate());
}
else{
System.out.print(cell.getContents());
}
手机、邮箱、姓名校验
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class PhoneFormatCheckUtils {
/**
* ^ 匹配输入字符串开始的位置
* \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
* $ 匹配输入字符串结尾的位置
*/
private static final Pattern HK_PATTERN = Pattern.compile("^(5|6|8|9)\\d{7}$");
private static final Pattern CHINA_PATTERN = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");
private static final Pattern NUM_PATTERN = Pattern.compile("[0-9]+");
/**
* 大陆号码或香港号码均可
*/
public static boolean isPhoneLegal(String str) throws PatternSyntaxException {
return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
}
/**
* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
* 此方法中前三位格式有:
* 13+任意数
* 145,147,149
* 15+除4的任意数(不要写^4,这样的话字母也会被认为是正确的)
* 166
* 17+3,5,6,7,8
* 18+任意数
* 198,199
*/
public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
Matcher m = CHINA_PATTERN.matcher(str);
return m.matches();
}
/**
* 香港手机号码8位数,5|6|8|9开头+7位任意数
*/
public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
Matcher m = HK_PATTERN.matcher(str);
return m.matches();
}
/**
* 判断是否是正整数的方法
*/
public static boolean isNumeric(String string) {
return NUM_PATTERN.matcher(string).matches();
}
/**
* 中国姓名验证
*/
public static boolean ChineseNameTest(String name) {
if (!name.matches("[\u4e00-\u9fa5]{2,4}")) {
return false;
}
return true;
}
/**
* 邮箱验证
*/
public static boolean isEmail(String email){
String str="^" +
"([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@" +
"([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+" +
"[\\.][A-Za-z]{2,3}" +
"([\\.][A-Za-z]{2})?" +
"$";
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(email);
return m.matches();
}
}
1.添加支持导入xls文件的jar包
net.sourceforge.jexcelapi
jxl
2.6.12
2.业务类
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.transaction.annotation.Transactional;
import cn.rntd.common.util.StringUtil;
import cn.rntd.common.util.log.LogUtil;
import cn.rntd.pc.shop.utils.PhoneFormatCheckUtils;
import cn.rntd.vo.gift.CompanyMemberUser;
import cn.rntd.vo.wx.wxUser.WxUser;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Test1 {
/**
* @Title: addCompanyMemberUsers
* @Description: 批量添加员工
* @param params
* @return
* @throws
* @author yiguang
* @date 2020-08-17
*/
@Override
@Transactional
public Object addCompanyMemberUsers(Map params) {
String fileSource = String.valueOf(params.get("fileSource"));
//String fileSource = "d://downloads/员工导入模板.xls";
String companyMemberId = String.valueOf(params.get("companyMemberId"));
//查询对应adminId和agentId
Map mapCompany = companyMemberUserMapper.findIdByCompanyMemberId(companyMemberId);
Date now=new Date();
SimpleDateFormat ft = new SimpleDateFormat ("yy-M-d");//时间格式处理
File file=new File(fileSource);
Map resultMap= new HashMap();
resultMap.put("code", "0");
if (StringUtil.isBlank(fileSource)) {
resultMap.put("code","-1");
resultMap.put("errmsg","文件不存在");
return resultMap;
}
//记录集合
List
2.dao层接口
@Mapper
public interface CompanyMemberUserMapper {
int insertSelective(CompanyMemberUser record);
@Select("select admin_id adminId,agent_id agentId from cloud_gift.tb_company_member where id = #{companyMemberId}")
Map findIdByCompanyMemberId(String companyMemberId);
@Select("select * from cloud_gift.tb_company_member_user where mobile = #{mobile} and admin_id = #{adminId} and is_delete = 0 limit 1")
CompanyMemberUser findCompanyMemberUserByMobile(@Param("mobile")String mobile,@Param("adminId") String adminId);
}
3.mybatis.xml 对应的插入到数据库的xml文件
insert into cloud_gift.tb_company_member_user
id,
company_member_id,
user_name,
mobile,
mailbox,
create_time,
update_time,
level,
admin_id,
agent_id,
is_delete,
birthday,
sex,
position,
entry_time,
#{id,jdbcType=VARCHAR},
#{companyMemberId,jdbcType=VARCHAR},
#{userName,jdbcType=VARCHAR},
#{mobile,jdbcType=VARCHAR},
#{mailbox,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP},
#{updateTime,jdbcType=TIMESTAMP},
#{level,jdbcType=INTEGER},
#{adminId,jdbcType=VARCHAR},
#{agentId,jdbcType=VARCHAR},
#{isDelete,jdbcType=INTEGER},
#{birthday,jdbcType=TIMESTAMP},
#{sex,jdbcType=VARCHAR},
#{position,jdbcType=VARCHAR},
#{entryTime,jdbcType=TIMESTAMP},
前端调用该接口返回结果:
如: