Jfinal 文件的下载信息模板模块:
public void downLoad() {
renderFile("/static/model/客户信息导入模板.xls");
}
Jfinal 文件的上传客户信息:
public void upload() throws FileNotFoundException {
User user = UserUtils.getUser();
Long officeId = user.getOfficeId();
Long companyId = user.getCompanyId();
UploadFile uploadFile = this.getFile("file");
if (uploadFile == null) {
setAttr("message","请导入客户信息excel文件");
render("excelInSystem.jsp");
return ;
}
File file = uploadFile.getFile();
FileInputStream filein = new FileInputStream(file);
PoiExt poi = new PoiExt();
int count = 0;//正确录入的客户数量
int falseCount = 0;//错误录入的商品客户数量
int allCount = 0;//导入的总条数
String falseColumn = "";//没有导入成功的行数
Object[][] objectExcel = poi.ReadExcel(filein,12);
for (Object[] objects : objectExcel) {
allCount++;
Boolean falseFlag = false;
String name = null;//公司名称
String type = null; //类型
String mobile = null;//电话
String address = null;//地址
String plateNumber = null;//车牌号
String brandName = null;//车品牌
String carTypeName = null;//车系
String displacement = null;//排量
String plateNumber1 = null;
String brandName1 = null;
String carTypeName1 = null;
String displacement1 = null;
if (objects[0] != null) {
name = objects[0].toString();
}
if (objects[1] != null) {
type = objects[1].toString();
}
if (objects[2] != null) {
mobile = objects[2].toString();
}
if (objects[3] != null) {
address = objects[3].toString();
}
if (objects[4] != null) {
plateNumber = objects[4].toString();
}
if (objects[5] != null) {
brandName = objects[5].toString();
}
if (objects[6] != null) {
carTypeName = objects[6].toString();
}
if (objects[7] != null) {
displacement = objects[7].toString();
}
if (objects[8] != null) {
plateNumber1 = objects[8].toString();
}
if (objects[9] != null) {
brandName1 = objects[9].toString();
}
if (objects[10] != null) {
carTypeName1 = objects[10].toString();
}
if (objects[11] != null) {
displacement1 = objects[11].toString();
}
Customer customer = new Customer();
if ((name != null && !"".equals(name))) {
customer.setOfficeId(officeId);
customer.setName(name);
if (type != null && !"".equals(type)) {
if ("公司".equals(type.trim())) {
customer.setType(1L);
}
if ("个人".equals(type.trim())) {
customer.setType(0L);
}
}else {
customer.setType(0L);
}
if (mobile != null && !"".equals(mobile)) {
//检查手机号是否是11位
Pattern p = Pattern.compile("^[1][3-8][0-9]{9}$"); // 验证手机号
Matcher m = p.matcher(mobile);
if (m.matches()) {
//验证手机号是否已经录入到系统中
List<Customer> customerList = Customer.dao.getCustomerWithMobile(officeId,mobile);
if (customerList.size() <= 0) {
customer.setMobile(mobile.trim());
} else {
falseCount++;
falseFlag = true;
if (falseColumn == "") {
falseColumn += allCount;
} else {
falseColumn += ","+allCount;
}
}
} else {
falseCount++;
falseFlag = true;
if (falseColumn == "") {
falseColumn += allCount;
} else {
falseColumn += ","+allCount;
}
}
}
if (address != null && !"".equals(address)) {
customer.setAddress(address.trim());
}
if (falseFlag) {
continue;
}
customer.setFromId(6L);
customer.setCreateBy(user.getId());
customer.setCreateDate(new Date());
customer.setUpdateBy(user.getId());
customer.setUpdateDate(new Date());
customer.setOfficeId(officeId);
customer.save();
Car car = new Car();
CarBrand carb = new CarBrand();
CarType cart = new CarType();
if (plateNumber != null && !"".equals(plateNumber)) {
car.setPlateNumber(plateNumber);
if (brandName != null && !"".equals(brandName)) {
CarBrand cb = CarBrand.dao.getCarBrandByName(brandName,companyId,officeId);
if (cb == null) {
carb.setCompanyId(companyId);
carb.setOfficeId(officeId);
carb.setName(brandName);
carb.setCreateBy(user.getId());
carb.setCreateDate(new Date());
carb.save();
car.setBrandId(carb.getId());
} else {
car.setBrandId(cb.getId());
}
}
if (carTypeName != null && !"".equals(carTypeName)) {
CarType ct = CarType.dao.getCarTypeByName(carTypeName,companyId,officeId);
if (ct == null && car.getBrandId() != null) {
cart.setCompanyId(companyId);
cart.setOfficeId(officeId);
cart.setName(carTypeName);
cart.setCarBrandId(car.getBrandId());
cart.setCreateBy(user.getId());
cart.setCreateDate(new Date());
cart.save();
car.setCarTypeId(cart.getId());
} else {
car.setCarTypeId(ct.getId());
}
}
if (displacement != null && !"".equals(displacement)) {
car.setDisplacement(displacement);
}
car.setOfficeId(officeId);
car.setCustomerId(customer.getId());
car.setCreateBy(user.getId());
car.setCreateDate(new Date());
car.setUpdateBy(user.getId());
car.setUpdateDate(new Date());
car.save();
}
Car car1 = new Car();
CarBrand carb1 = new CarBrand();
CarType cart1 = new CarType();
if (plateNumber1 != null && !"".equals(plateNumber1)) {
car1.setPlateNumber(plateNumber1);
if (brandName1 != null && !"".equals(brandName1)) {
CarBrand cb = CarBrand.dao.getCarBrandByName(brandName1,companyId,officeId);
if (cb == null) {
carb1.setCompanyId(companyId);
carb1.setOfficeId(officeId);
carb1.setName(brandName1);
carb1.setCreateBy(user.getId());
carb1.setCreateDate(new Date());
carb1.save();
car1.setBrandId(carb1.getId());
} else {
car1.setBrandId(cb.getId());
}
}
if (carTypeName1 != null && !"".equals(carTypeName1)) {
CarType ct = CarType.dao.getCarTypeByName(carTypeName1,companyId,officeId);
if (ct == null && car1.getBrandId() != null) {
cart1.setCompanyId(companyId);
cart1.setOfficeId(officeId);
cart1.setName(carTypeName1);
cart1.setCarBrandId(car1.getBrandId());
cart1.setCreateBy(user.getId());
cart1.setCreateDate(new Date());
cart1.save();
car1.setCarTypeId(cart1.getId());
} else {
car1.setCarTypeId(ct.getId());
}
}
if (displacement1 != null && !"".equals(displacement1)) {
car1.setDisplacement(displacement1);
}
car1.setOfficeId(officeId);
car1.setCustomerId(customer.getId());
car1.setCreateBy(user.getId());
car1.setCreateDate(new Date());
car1.setUpdateBy(user.getId());
car1.setUpdateDate(new Date());
car1.save();
}
}
count++;
}
String falseStrShow = "";
if (!"".equals(falseColumn)) {
falseStrShow = ",客户信息表中"+falseColumn+"行没有导入成功";
}
setFlash("messageStr","本次导入"+allCount+"条客户信息,"+falseCount+"条客户信息导入失败"+falseStrShow);
redirect("/ctm/manage");
}