MVC上传Excel文件文件读取

页面处理:

上传文件
(该处为txt上传文件或者Excl上传文件)
js部分的处理:

后台代码处理:
/**
* 接收上传文件,并解析
*
* @param file
* @throws ServletException
* @throws IOException
*/
@ControllerAuthority
@ResponseBody
@RequestMapping("/路径")
public void receiveFilesInvocation(Model model, @RequestParam(“file”) MultipartFile file,String inprotfile,
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UmUserDto umuesrdtos = (UmUserDto) request.getSession().getAttribute(“userSession”);
LogUtil.saveLog(umuesrdtos.getUserId() + “”, “接收上传文件,并解析”, EidConstants.LOG_TYPE_SELECT);
String respMsg = “”;

	InputStreamReader inputReader = null;
	BufferedReader bufReader = null;
	try {
		// 如果文件不为空,写入上传路径
		if (!file.isEmpty()) {
			//1. 解析上传文件
			// 上传文件名
			String filename = file.getOriginalFilename();
			String suffix = filename.substring(filename.lastIndexOf(".") + 1);
			if(suffix.equals("xls") && inprotfile.equals("01") || suffix.equals("xlsx") && inprotfile.equals("01")){
				request.getRequestDispatcher("/路径e").forward(request, response);
			}else if(suffix.equals("xls") && inprotfile.equals("02") || suffix.equals("xlsx") && inprotfile.equals("02")){
				respMsg = "上传文件格式不正确!";
				logger.error("上传文件格式不正确!");
			}else if(suffix.equals("txt") && inprotfile.equals("01")){
				respMsg = "上传文件格式不正确!";
				logger.error("上传文件格式不正确!");
			}
			//保存的路径可以是本地或者服务器上的路径
			String locaFilePath = EidConstants.tempSavePath + filename;
			File uploadFile = new File(locaFilePath);
			// 判断路径是否存在,如果不存在就创建一个
			if (!uploadFile.getParentFile().exists()) {
				uploadFile.getParentFile().mkdirs();
			}
			// 将上传文件保存到一个目标文件当中
			file.transferTo(uploadFile); 
			inputReader = new InputStreamReader(file.getInputStream(),"GBK");

// inputReader = new InputStreamReader(new FileInputStream(uploadFile), “UTF-8”);
bufReader = new BufferedReader(inputReader);
/boolean isMatch = false;/
/正常的身份信息/
/正常的身份信息/
List sendInfoList = new ArrayList();
/异常的身份信息/
List errInfoList = new ArrayList();
String information = null;
// 姓名|证件号码|号码
while ((information = bufReader.readLine()) != null) {
校验手机号与身份证号码,正确返回true
// 字符分割出请求参数
String[] sphone = information.split("[|]",-1);
int count = sphone.length;
if(count3){
IdentityInformationDto identityInformationDto=new IdentityInformationDto();
identityInformationDto.setName(sphone[0]);
identityInformationDto.setIdnum(sphone[1]);
identityInformationDto.setMobile(sphone[2]);
sendInfoList.add(identityInformationDto);
}
else if(count
4){
IdentityInformationDto identityInformationDto=new IdentityInformationDto();
identityInformationDto.setName(sphone[0]);
identityInformationDto.setIdnum(sphone[1]);
identityInformationDto.setMobile(sphone[2]);
identityInformationDto.setCardno(sphone[3]);
sendInfoList.add(identityInformationDto);
}else if(count==2){
IdentityInformationDto identityInformationDto=new IdentityInformationDto();
identityInformationDto.setName(sphone[0]);
identityInformationDto.setIdnum(sphone[1]);
sendInfoList.add(identityInformationDto);
}else{
respMsg = “上传文件格式不正确!”;
logger.error(“上传文件格式不正确!”);
}
}
/
清理缓存 /
request.getSession().removeAttribute(“sendInfoList”);
/
正常的手机号码存入缓存 */
request.getSession().setAttribute(“sendInfoList”, sendInfoList);
if (errInfoList.size() > 0) {
respMsg = “请求总数:” + sendInfoList.size() + “个,异常总数:” + errInfoList.size() + “个!”;
} else {
respMsg = “发送总数:” + sendInfoList.size() + “个。”;
}
logger.debug(respMsg);
} else {
respMsg = “上传文件为空!”;
logger.debug(respMsg);
}
} catch (Exception e) {
logger.error(“上传文件失败”, (Throwable) e);
} finally {
if(inputReader != null){
inputReader.close();
}
if(bufReader != null){
bufReader.close();
}
OutputStream outs = response.getOutputStream();
outs.write(respMsg.getBytes(“UTF-8”));
outs.close();
}
return;
}

excl处理
/**
*
* @param
* @param request
* @param response
excel方式上传解决方案
/
@ControllerAuthority
@RequestMapping("/路径")
public void exportreconFile(HttpServletRequest request, HttpServletResponse response, @RequestParam(“file”) MultipartFile file)
throws ServletException, IOException {
//判断调用哪个接口
String respMsg = “文件上传成功”;
/正常的身份信息/
List sendInfoList = new ArrayList();
try {
if (!file.isEmpty()) {
//上传文件路径该路径是自己配置
ParamInfoDto paramInfo=iParamInfoService.queryParamInfo("");//可自定义上传路径
if (paramInfonull || StringUtils.isEmpty(paramInfo.getParamValue())) {
logger.error(“您未配置导入产品模板的临时目录”);
return ;
}
String path = paramInfo.getParamValue();
//上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path,filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
//解析Excel文件
//获得Workbook工作薄对象
Workbook workbook = getWorkBook(file);
if (workbook != null) {
//获得当前sheet工作表 默认第一页sheel
Sheet sheet = workbook.getSheetAt(0);
if(sheet == null){
return ;
}
//获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
for(int rowNum = 1;rowNum <= lastRowNum;rowNum++){
//获得当前行
Row row = sheet.getRow(rowNum);
if(row == null){
continue;
}
//获得当前行的列数
int lastCellNum = row.getLastCellNum();
String[] cells = new String[lastCellNum];
for(int cellNum = 0; cellNum < lastCellNum;cellNum++){
Cell cell = row.getCell(cellNum);
cells[cellNum] = getCellValue(cell);
}
if(lastCellNum
3){
//只需要将有用的值提取出来
String name = cells[0];
String idnum = cells[1];
String mobile = cells[2];
IdentityInformationDto identityInformationDto=new IdentityInformationDto();
identityInformationDto.setName(name);
identityInformationDto.setIdnum(idnum);
identityInformationDto.setMobile(mobile);
sendInfoList.add(identityInformationDto);
}else if(lastCellNum4){
String name = cells[0];
String idnum = cells[1];
String mobile = cells[2];
String cardno = cells[3];
IdentityInformationDto identityInformationDto=new IdentityInformationDto();
identityInformationDto.setName(name);
identityInformationDto.setIdnum(idnum);
identityInformationDto.setMobile(mobile);
identityInformationDto.setCardno(cardno);
sendInfoList.add(identityInformationDto);
}else if(lastCellNum
2){
String name = cells[0];
String idnum = cells[1];
IdentityInformationDto identityInformationDto=new IdentityInformationDto();
identityInformationDto.setName(name);
identityInformationDto.setIdnum(idnum);
sendInfoList.add(identityInformationDto);
}
}
}
/
清理缓存 /
request.getSession().removeAttribute(“sendInfoList”);
/
正常的手机号码存入缓存 */
request.getSession().setAttribute(“sendInfoList”, sendInfoList);
ServletOutputStream outs = response.getOutputStream();
outs.write(respMsg.getBytes(“UTF-8”));
outs.close();
} else {
ServletOutputStream outs = response.getOutputStream();
outs.write((“请选择导入文件!”).getBytes(“UTF-8”));
outs.close();
}
} catch (Exception e) {
//打印日志不要使用System.out.println
logger.error(“文件上传异常”, (Throwable) e);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值