Commit e758bfde by 徐高华

企微托管导购授权-批量导入

parent 7ee3c6dc
package com.gic.haoban.manage.api.dto;
import java.io.Serializable;
import java.util.List;
public class OpenStaffImportDTO implements Serializable {
private String wxEnterpriseId ;
private String enterpriseId ;
private List<String> clerkCodeList ;
private String loginClerkId ;
private String clerkCode ;
public String getClerkCode() {
return clerkCode;
}
public void setClerkCode(String clerkCode) {
this.clerkCode = clerkCode;
}
public String getLoginClerkId() {
return loginClerkId;
}
public void setLoginClerkId(String loginClerkId) {
this.loginClerkId = loginClerkId;
}
public String getWxEnterpriseId() {
return wxEnterpriseId;
}
public void setWxEnterpriseId(String wxEnterpriseId) {
this.wxEnterpriseId = wxEnterpriseId;
}
public String getEnterpriseId() {
return enterpriseId;
}
public void setEnterpriseId(String enterpriseId) {
this.enterpriseId = enterpriseId;
}
public List<String> getClerkCodeList() {
return clerkCodeList;
}
public void setClerkCodeList(List<String> clerkCodeList) {
this.clerkCodeList = clerkCodeList;
}
}
...@@ -9,6 +9,17 @@ public class OpenStaffInitQDTO implements Serializable { ...@@ -9,6 +9,17 @@ public class OpenStaffInitQDTO implements Serializable {
private String staffId ; private String staffId ;
private int loginAgainFlag ; private int loginAgainFlag ;
// 导入操作 1是0否
private int importFlag ;
public int getImportFlag() {
return importFlag;
}
public void setImportFlag(int importFlag) {
this.importFlag = importFlag;
}
public int getLoginAgainFlag() { public int getLoginAgainFlag() {
return loginAgainFlag; return loginAgainFlag;
} }
......
package com.gic.haoban.manage.api.service;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.dto.OpenStaffImportDTO;
public interface OpenStaffImportTaskApiService {
ServiceResponse<String> createTask(OpenStaffImportDTO dto);
}
\ No newline at end of file
...@@ -5,6 +5,9 @@ import com.gic.api.base.commons.BasePageInfo; ...@@ -5,6 +5,9 @@ import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page; import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse; import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil; import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.EnterpriseUsingPermissionDto;
import com.gic.enterprise.api.dto.WechatWorkServiceSetting;
import com.gic.enterprise.api.service.EnterpriseUseForbidService;
import com.gic.haoban.manage.api.dto.OpenStaffDTO; import com.gic.haoban.manage.api.dto.OpenStaffDTO;
import com.gic.haoban.manage.api.dto.OpenStaffLicenseDTO; import com.gic.haoban.manage.api.dto.OpenStaffLicenseDTO;
import com.gic.haoban.manage.api.dto.OpenStaffListDTO; import com.gic.haoban.manage.api.dto.OpenStaffListDTO;
...@@ -46,6 +49,8 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService { ...@@ -46,6 +49,8 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
private OpenStaffService openStaffService; private OpenStaffService openStaffService;
@Autowired @Autowired
private WxEnterpriseService wxEnterpriseService; private WxEnterpriseService wxEnterpriseService;
@Autowired
private EnterpriseUseForbidService enterpriseUseForbidService ;
@Override @Override
public ServiceResponse<Void> delete(Long id) { public ServiceResponse<Void> delete(Long id) {
...@@ -193,6 +198,9 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService { ...@@ -193,6 +198,9 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
try { try {
RedisUtil.lock(lockKey, 3L); RedisUtil.lock(lockKey, 3L);
TabOpenStaff openStaff = this.openStaffMapper.getByStaffId(staffId); TabOpenStaff openStaff = this.openStaffMapper.getByStaffId(staffId);
if(qdto.getImportFlag()==1 && null != openStaff) {
return ServiceResponse.failure("9999","导购账号已授权") ;
}
Long qwUserId = null; Long qwUserId = null;
if (null != openStaff) { if (null != openStaff) {
if (openStaff.getStatusFlag() == 1 && qdto.getLoginAgainFlag() == 0) { if (openStaff.getStatusFlag() == 1 && qdto.getLoginAgainFlag() == 0) {
...@@ -220,18 +228,27 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService { ...@@ -220,18 +228,27 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
openStaff.setWxEnterpriseId(qdto.getWxEnterpriseId()); openStaff.setWxEnterpriseId(qdto.getWxEnterpriseId());
openStaff.setEnterpriseId(qdto.getEnterpriseId()); openStaff.setEnterpriseId(qdto.getEnterpriseId());
} }
ServiceResponse<QwOpenResultInitBO> resp = OpenUtils.init(qwUserId,true);
log.info("resp={}", JSONObject.toJSONString(resp));
QwOpenResultInitBO init = resp.getResult();
openStaff.setUuid(init.getUuid());
openStaff.setQrCode1(init.getQrcode());
openStaff.setKey1(init.getKey());
openStaff.setStatusFlag(2); openStaff.setStatusFlag(2);
openStaff.setStep(0); openStaff.setStep(0);
openStaff.setValidFlag(0); openStaff.setValidFlag(0);
openStaff.setValidQrcodeFlag(0); openStaff.setValidQrcodeFlag(0);
openStaff.setQrCode2(null); openStaff.setQrCode2(null);
openStaff.setKey2(null); openStaff.setKey2(null);
if(qdto.getImportFlag()==1) {
boolean flag = this.getCheckInfo(qdto.getWxEnterpriseId(), qdto.getEnterpriseId()) ;
if(!flag) {
return ServiceResponse.failure("9999","托管账号数量已达上线") ;
}
openStaff = this.openStaffService.save(openStaff);
return ServiceResponse.success(null);
}
ServiceResponse<QwOpenResultInitBO> resp = OpenUtils.init(qwUserId,true);
log.info("resp={}", JSONObject.toJSONString(resp));
QwOpenResultInitBO init = resp.getResult();
openStaff.setUuid(init.getUuid());
openStaff.setQrCode1(init.getQrcode());
openStaff.setKey1(init.getKey());
openStaff = this.openStaffService.save(openStaff); openStaff = this.openStaffService.save(openStaff);
OpenStaffDTO dto = EntityUtil.changeEntityByJSON(OpenStaffDTO.class, openStaff); OpenStaffDTO dto = EntityUtil.changeEntityByJSON(OpenStaffDTO.class, openStaff);
dto.setQrcodeData(init.getQrcode_data()); dto.setQrcodeData(init.getQrcode_data());
...@@ -308,4 +325,25 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService { ...@@ -308,4 +325,25 @@ public class OpenStaffApiServiceImpl implements OpenStaffApiService {
public void updateStopTime(Long openStaffId, int type, int time) { public void updateStopTime(Long openStaffId, int type, int time) {
this.openStaffMapper.updateStopTime(openStaffId, type, time); this.openStaffMapper.updateStopTime(openStaffId, type, time);
} }
private boolean getCheckInfo(String wxEnterpriseId, String enterpriseId) {
ServiceResponse<EnterpriseUsingPermissionDto> resp = this.enterpriseUseForbidService.getWxEnterpriseService(enterpriseId, wxEnterpriseId);
log.info("托管许可={}", JSONObject.toJSONString(resp));
if (resp.isSuccess()) {
EnterpriseUsingPermissionDto dto = resp.getResult();
if (null == dto) {
return false ;
} else {
if (dto.getServiceEndDate().before(new Date())) {
return false ;
}
String json = dto.getModuleSetting();
WechatWorkServiceSetting bo = JSONObject.parseObject(json, WechatWorkServiceSetting.class);
int total = bo.getAccountNumber() ;
int useNum = this.openStaffMapper.getNum(wxEnterpriseId, enterpriseId, 0);
return total<useNum ;
}
}
return false;
}
} }
package com.gic.haoban.manage.service.service.out.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.util.ToolUtil;
import com.gic.haoban.manage.api.dto.OpenStaffDTO;
import com.gic.haoban.manage.api.dto.OpenStaffImportDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.qdto.OpenStaffInitQDTO;
import com.gic.haoban.manage.api.service.OpenStaffApiService;
import com.gic.haoban.manage.api.service.OpenStaffImportTaskApiService;
import com.gic.haoban.manage.api.service.StaffClerkRelationApiService;
import com.gic.haoban.manage.service.dao.mapper.OpenStaffMapper;
import com.gic.haoban.manage.service.entity.TabOpenStaff;
import com.google.common.collect.Lists;
import com.task.allocation.dto.AuthDTO;
import com.task.allocation.exception.TaskAllocationException;
import com.task.allocation.operation.AbstractTaskCenterManageOperation;
import com.task.allocation.qo.TaskAllocationComputed;
import com.task.allocation.qo.TaskCenterInitTaskQo;
import com.task.allocation.qo.TaskFailInfoQo;
import com.task.allocation.util.TaskAllocationSdkClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service("openStaffImportTaskApiService")
public class OpenStaffImportTaskApiServiceImpl extends AbstractTaskCenterManageOperation<OpenStaffImportDTO> implements OpenStaffImportTaskApiService {
private static final Logger log = LogManager.getLogger(OpenStaffImportTaskApiService.class);
@Autowired
private StaffClerkRelationApiService staffClerkRelationApiService;
@Autowired
private OpenStaffApiService openStaffApiService;
@Autowired
private OpenStaffMapper openStaffMapper;
@Autowired
private ClerkService clerkService ;
@Override
public List<OpenStaffImportDTO> getListTasks(String params) {
OpenStaffImportDTO dto = JSON.parseObject(params, OpenStaffImportDTO.class);
List<OpenStaffImportDTO> tasks = Lists.partition(dto.getClerkCodeList(), 1)
.stream()
.map(item -> {
OpenStaffImportDTO sub = new OpenStaffImportDTO();
sub.setWxEnterpriseId(dto.getWxEnterpriseId());
sub.setEnterpriseId(dto.getEnterpriseId());
sub.setLoginClerkId(dto.getLoginClerkId());
sub.setClerkCode(item.get(0));
return sub;
}).collect(Collectors.toList());
log.info("导入托管总任务数 {}", tasks.size());
return tasks;
}
@Override
public TaskFailInfoQo dealSingle(OpenStaffImportDTO obj) throws TaskAllocationException {
String wxEnterpriseId = obj.getEnterpriseId() ;
String enterpriseId = obj.getEnterpriseId() ;
String clerkCode = obj.getClerkCode() ;
ClerkDTO clerk = this.clerkService.getClerkByClerkCode(enterpriseId,clerkCode) ;
if(null == clerk) {
return fail().setColumns("导购code","失败说明")
.addValues(clerkCode,"该账号在当前企业下不存在");
}
String clerkId = clerk.getClerkId() ;
StaffClerkRelationDTO staffClerkRelationDTO = this.staffClerkRelationApiService.getByClerkId(clerkId) ;
if(null == staffClerkRelationDTO) {
return fail().setColumns("导购code","失败原因")
.addValues(clerkCode,"该账号未绑定好办小程序");
}
String staffId = staffClerkRelationDTO.getStaffId() ;
TabOpenStaff openStaff = this.openStaffMapper.getByStaffId(staffId) ;
if(null != openStaff) {
return fail().setColumns("导购code","失败说明")
.addValues(clerkCode,"导购账号已授权");
}
OpenStaffInitQDTO init = new OpenStaffInitQDTO() ;
init.setWxEnterpriseId(wxEnterpriseId);
init.setEnterpriseId(enterpriseId);
init.setStaffId(staffId);
init.setImportFlag(1);
ServiceResponse<OpenStaffDTO> resp = this.openStaffApiService.init(init) ;
if(!resp.isSuccess()) {
return fail().setColumns("导购code","失败说明")
.addValues(clerkCode,resp.getMessage());
}
return success();
}
@Override
public void computed(TaskAllocationComputed computed) {
log.info("导入托管完成={}", JSONObject.toJSONString(computed));
}
@Override
public OpenStaffImportDTO singleClass() {
return new OpenStaffImportDTO();
}
@Override
public ServiceResponse<String> createTask(OpenStaffImportDTO dto) {
TaskCenterInitTaskQo initTaskQo = new TaskCenterInitTaskQo();
String uuid = ToolUtil.randomUUID();
initTaskQo.setParams(uuid);
initTaskQo.setBusinessParams(uuid);
initTaskQo.setTaskCategoryCode("openStaffImportTask");
initTaskQo.setTaskType(1);
AuthDTO user = new AuthDTO();
user.setOperationUserId(dto.getLoginClerkId()) ;
user.setEnterpriseId(dto.getEnterpriseId()) ;
initTaskQo.setLoginUser(user);
initTaskQo.setTaskModelName("好办企微托管");
initTaskQo.setTaskName("导入托管账号");
initTaskQo.setTaskContent("批量导入托管账号" + dto.getClerkCodeList().size() + "个");
try {
TaskAllocationSdkClient instance = TaskAllocationSdkClient.getInstance();
String taskId = instance.initTask(initTaskQo);
return ServiceResponse.success(taskId);
} catch (Exception e) {
log.info("批量导入托管账号dto任务异常", e);
}
return ServiceResponse.failure("9999", "创建任务异常");
}
}
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
<bean class="com.gic.haoban.common.ext.LoggerLevelListen" id="loggerLevelListen"> <bean class="com.gic.haoban.common.ext.LoggerLevelListen" id="loggerLevelListen">
<constructor-arg index="0" value="haoban-manage-service"/> <constructor-arg index="0" value="haoban-manage-service"/>
</bean> </bean>
<dubbo:service interface="com.gic.haoban.manage.api.service.OpenStaffImportTaskApiService" ref="openStaffImportTaskApiService" timeout="10000"/>
<dubbo:service interface="com.gic.haoban.manage.api.service.WxEnterpriseActiveDataApiService" ref="wxEnterpriseActiveDataApiService" timeout="10000"/> <dubbo:service interface="com.gic.haoban.manage.api.service.WxEnterpriseActiveDataApiService" ref="wxEnterpriseActiveDataApiService" timeout="10000"/>
<dubbo:service interface="com.gic.haoban.manage.api.service.chat.GroupChatDataApiService" ref="groupChatDataApiService" timeout="10000"/> <dubbo:service interface="com.gic.haoban.manage.api.service.chat.GroupChatDataApiService" ref="groupChatDataApiService" timeout="10000"/>
<dubbo:service interface="com.gic.haoban.manage.api.service.chat.GroupChatPlanApiService" ref="groupChatPlanApiService" timeout="10000"/> <dubbo:service interface="com.gic.haoban.manage.api.service.chat.GroupChatPlanApiService" ref="groupChatPlanApiService" timeout="10000"/>
......
...@@ -26,9 +26,11 @@ import com.gic.haoban.manage.api.dto.*; ...@@ -26,9 +26,11 @@ import com.gic.haoban.manage.api.dto.*;
import com.gic.haoban.manage.api.qdto.OpenStaffInitQDTO; import com.gic.haoban.manage.api.qdto.OpenStaffInitQDTO;
import com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO; import com.gic.haoban.manage.api.qdto.OpenStaffPageQDTO;
import com.gic.haoban.manage.api.service.OpenStaffApiService; import com.gic.haoban.manage.api.service.OpenStaffApiService;
import com.gic.haoban.manage.api.service.OpenStaffImportTaskApiService;
import com.gic.haoban.manage.api.service.StaffApiService; import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.StaffClerkRelationApiService; import com.gic.haoban.manage.api.service.StaffClerkRelationApiService;
import com.gic.haoban.manage.web.qo.open.OpenStaffQO; import com.gic.haoban.manage.web.qo.open.OpenStaffQO;
import com.gic.haoban.manage.web.utils.ImportExcelUtil;
import com.gic.haoban.manage.web.vo.open.OpenQwAccountInfoVO; import com.gic.haoban.manage.web.vo.open.OpenQwAccountInfoVO;
import com.gic.haoban.manage.web.vo.open.OpenStaffVO; import com.gic.haoban.manage.web.vo.open.OpenStaffVO;
import com.gic.qcloud.BucketNameEnum; import com.gic.qcloud.BucketNameEnum;
...@@ -44,7 +46,9 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -44,7 +46,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -72,6 +76,8 @@ public class QywxOpenController { ...@@ -72,6 +76,8 @@ public class QywxOpenController {
private EnterpriseUseForbidService enterpriseUseForbidService; private EnterpriseUseForbidService enterpriseUseForbidService;
@Autowired @Autowired
private ClerkService clerkService ; private ClerkService clerkService ;
@Autowired
private OpenStaffImportTaskApiService openStaffImportTaskApiService ;
/** /**
* 托管列表 * 托管列表
...@@ -376,7 +382,7 @@ public class QywxOpenController { ...@@ -376,7 +382,7 @@ public class QywxOpenController {
} }
OpenStaffDTO openStaff = staffResp.getResult() ; OpenStaffDTO openStaff = staffResp.getResult() ;
if(openStaff.getStatusFlag()==1) { if(openStaff.getStatusFlag()==1) {
return RestResponse.failure("9999","成员正托管中") ; return RestResponse.failure("9999","导购账号已授权") ;
} }
String checkMsg = this.check(wxEnterpriseId, enterpriseId , true); String checkMsg = this.check(wxEnterpriseId, enterpriseId , true);
if (null != checkMsg) { if (null != checkMsg) {
...@@ -398,4 +404,70 @@ public class QywxOpenController { ...@@ -398,4 +404,70 @@ public class QywxOpenController {
return RestResponse.successResult(openStaffDTO); return RestResponse.successResult(openStaffDTO);
} }
/**
* 导入账号
* @param file
* @return
* @throws Exception
*/
@RequestMapping(path = "/import-open-staff")
public RestResponse<?> importVirtualStock(@RequestParam("file") MultipartFile file) throws Exception {
WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
String wxEnterpriseId = login.getWxEnterpriseId();
String enterpriseId = login.getEnterpriseId();
String checkMsg = this.check(wxEnterpriseId, enterpriseId , false);
if (null != checkMsg) {
return RestResponse.failure("9999", checkMsg);
}
List<List<Object>> result = new ArrayList<>();
ImportExcelUtil.getBankListByExcelSheet1(file.getInputStream(), file.getOriginalFilename(), result);
if (CollectionUtils.isEmpty(result) || result.size() <= 1) {
return RestResponse.failure("9999", "文件内容为空");
}
// 判断是否超过数量
if (result.size() > 1000) {
return RestResponse.failure("9999", "最多导入 1000 条信息,文件大小不超过 2 M");
}
List<Object> columns = result.get(0) ;
if(CollectionUtils.isEmpty(columns)) {
return RestResponse.failure("9999", "导入文件表头信息错误,请参考模板");
}
log.info("标题={}", JSONObject.toJSONString(columns));
if(null==columns.get(0) || !columns.get(0).toString().toLowerCase().trim().equals("导购code")) {
log.info("{}",columns.get(0));
return RestResponse.failure("9999", "导入文件表头信息错误,请参考模板");
}
List<String> clerkCodeList = new ArrayList<>();
for (int i = 1; i < result.size(); i++) {
List<Object> objects = result.get(i);
if(CollectionUtils.isEmpty(objects)) {
continue;
}
log.info("解析第 {}行数据 {} , {}", (i + 1), JSON.toJSONString(objects) , objects.size());
String content = objects.stream().map(o->o.toString().trim()).collect(Collectors.joining(""));
if(org.apache.commons.lang3.StringUtils.isBlank(content)) {
continue ;
}
String cardNum = (objects.get(0) == null) ? "" : objects.get(0).toString() ;
if(org.apache.commons.lang3.StringUtils.isNotBlank(cardNum)) {
clerkCodeList.add(cardNum) ;
}
}
if (CollectionUtils.isEmpty(clerkCodeList)) {
return RestResponse.failure("9999", "导入文件无数据");
}
OpenStaffImportDTO qdto = new OpenStaffImportDTO() ;
qdto.setWxEnterpriseId(wxEnterpriseId);
qdto.setEnterpriseId(enterpriseId);
qdto.setLoginClerkId(login.getClerkId());
qdto.setClerkCodeList(clerkCodeList);
ServiceResponse<String> serviceResponse = this.openStaffImportTaskApiService.createTask(qdto);
log.info("批量导入导购任务结果 result:{}", JSON.toJSONString(serviceResponse));
if (serviceResponse.isSuccess()) {
return RestResponse.successResult(serviceResponse.getResult());
}
return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
}
} }
package com.gic.haoban.manage.web.utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* Excel操作
*
* @author 徐波 2019-4-18
*/
public class ImportExcelUtil {
private static final Logger log = LoggerFactory.getLogger(ImportExcelUtil.class);
private final static String excel2003L = ".xls"; //2003- 版本的excel
private final static String excel2007U = ".xlsx"; //2007+ 版本的excel
/**
* 描述:获取IO流中的数据,组装成List<List<Object>>对象
*
* @return 获取第一个sheet 中数据
*/
public static void getBankListByExcelSheet1(InputStream in, String fileName, List<List<Object>> sheet1List) throws Exception {
//创建Excel工作薄
Workbook work = getWorkbook(in, fileName);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet1 = null;
Row row1 = null;
Cell cell1 = null;
//遍历Excel中所有的sheet
sheet1 = work.getSheetAt(0);
if (sheet1 != null) {
//遍历当前sheet中的所有行
for (int j = sheet1.getFirstRowNum(); j <= sheet1.getLastRowNum(); j++) {
row1 = sheet1.getRow(j);
if (row1 == null) {
continue;
}
//遍历所有的列
List<Object> li = new ArrayList<Object>();
log.info("单元格数={}", row1.getFirstCellNum());
if(row1.getFirstCellNum() != 0) {
for(int i=0;i<(row1.getFirstCellNum()) ; i++) {
li.add("") ;
}
}
for (int y = row1.getFirstCellNum(); y < row1.getLastCellNum(); y++) {
cell1 = row1.getCell(y);
//1、代表单元格里面类型为String类型
if (cell1 != null) {
li.add(getCellValue(cell1));
} else {
li.add("");
}
}
if (CollectionUtils.isNotEmpty(li)) {
sheet1List.add(li);
}
}
}
}
/**
* 描述:根据文件后缀,自适应上传文件的版本
*
* @param inStr,fileName
* @return
* @throws Exception
*/
public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (excel2003L.equals(fileType)) {
wb = new HSSFWorkbook(inStr); //2003-
} else if (excel2007U.equals(fileType)) {
wb = new XSSFWorkbook(inStr); //2007+
} else {
throw new Exception("解析的文件格式有误!");
}
return wb;
}
/**
* 描述:对表格中数值进行格式化
*
* @param cell
* @return
*/
public static Object getCellValue(Cell cell) {
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if ("General".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
value = sdf.format(cell.getDateCellValue());
} else if (StringUtils.equals("m/d/yy h:mm", cell.getCellStyle().getDataFormatString())) {
value = dateFormat.format(cell.getDateCellValue());
} else {
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
return value;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment