在javaweb中将excel表格导入存放数据库

在jsp页面

<html>
<body>
<head>
<script type="text/javascript">
function fileChange(input){
	var fileName = input.value;
		if(fileName.length > 1 && fileName ) {       
        var ldot = fileName.lastIndexOf(".");
        var type = fileName.substring(ldot + 1);
        if(type=="xls" || type=="xlsx") {
        	$("#type").val(type);
            var form=document.getElementById("form_query");
			form.method = 'post';
			form.action = 'config/department/importExcle';
			form.submit();  
        }else{
        	alert("文件格式不准确!");
        	input.outerHTML=input.outerHTML.replace(/(value=\").+\"/i,"$1\"")
        }       
	}	
};

</script>
</head>
<table>
	<tr>
		<td>
			<input type="file" id="excleFile" name="excleFile"onchange="fileChange(this);" style="display:none"/>
			<button onclick="$('input[id=excleFile]').click();" class="save-btn">导入</button>
		</td>
		<td>
			<button onclick="exportDepart()" class="save-btn">导出</button>
		</td>
	</tr>
</table>
</body>
</html>

本人用的struts2 的框架,在jsp页面写好以后,数据会通过struts.xml 找到action中对应的方法,在action方法中的具体代码如下:

//导入科室信息 
	public String readXls() throws IOException{
		try {
			Workbook workBook=null;
			InputStream is = new FileInputStream(excleFile);
			String type = request.getParameter("type");
			if(type.toLowerCase().endsWith("xls")){  
				workBook=new HSSFWorkbook(is);  
	        }  
	        if(type.toLowerCase().endsWith("xlsx")){  
	        	workBook= new XSSFWorkbook(is);  
	        }  
	        //调用service层方法,把数据传到service层处理。
			departmentsService.readXls(workBook);
			return SUCCESS;
		} catch (FileNotFoundException e) {
			log.error(e);
			return ERROR;
		}
	}
	

接在在service实现层进行处理,具体代码如下:

@Override
public List<DepartmentVO> readXls(Workbook workBook) {
	try {
			DepartmentVO departVO=null;
			for(int numSheet=0;numSheet<=workBook.getNumberOfSheets();numSheet++){
				Sheet sheet=workBook.getSheetAt(numSheet);
				if(sheet==null){
					continue;
				}
				//处理excel表格中的数据,其实很简单对应好就可以了。
				for(int rowNum=1;rowNum<=sheet.getLastRowNum();rowNum++){
					Row row=sheet.getRow(rowNum);
					if(row!=null){
						departVO=new DepartmentVO();
						Cell dId=row.getCell(1);
						Cell dName=row.getCell(2);
						//Cell pId=row.getCell(2);
						//Cell dStatus=row.getCell(3);
						Cell dInfo=row.getCell(4);
						Cell dAddr=row.getCell(5);
						Cell dPhone=row.getCell(6);
						Cell dNo=row.getCell(0);
						
						departVO.setDepartmentName(ExcleUtils.getValue(dName));
						departVO.setDepartmentId(ExcleUtils.getValue(dId));
						departVO.setDisplay_no(ExcleUtils.getValue(dNo));
						departVO.setParentId("0");
						departVO.setPhone(ExcleUtils.getValue(dPhone));
						departVO.setDepartmentAddr(ExcleUtils.getValue(dAddr));
						departVO.setIntroduction(ExcleUtils.getValue(dInfo));
						departVO.setDepartment_status(1);
						saveOrUpdaeDepartment(departVO);
					}
				}
			}
	} catch (Exception e) {
		log.error(e);
	}
	return null;
}

@Override
public ServiceResult saveOrUpdaeDepartment(DepartmentVO d) {
	try{
		departmentDAO.saveOrUpdaeDepartment(d);
		return ServiceResult.getSucInstance();
	}catch(DataAccessException dacEx){
		ServiceResult sr = ServiceResult.getFailedInstance(""
				, "系统异常,更新科室失败!");
		return sr;
	}catch(Exception ex){
		log.error(null, ex);
		ServiceResult sr = ServiceResult.getFailedInstance(""
				, "参数错误,更新科室失败!");
		return sr;
	}
}

剩下的就不写了,剩下就是封装好的数据传到bean中存到数据库的操作。我代码的例子可能不是很好,是将科室信息导入数据库的。所以各位看官轻喷嘿嘿


欢迎大家关注个人公众号
分享各种学习资料,包含java,linux,大数据等。资料包含视频文档以及源码,仅供学习交流和分享,不涉及任何商业用途。同时分享本人及投递的优质技术博文。
程序员小猪

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员爱酸奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值