Jxl 导入Excel文件到数据库

这两天用jxl简单的实现了Excel文件的导入导出,下面是我的一些总结(当然有很多是参照别人的代码)。
jsp页面代码:

/Excel文件导入到数据库中
function importEmp(){
	//检验导入的文件是否为Excel文件
	var excelPath = document.getElementById("excelPath").value;
	if(excelPath == null || excelPath == ''){
		alert("请选择要上传的Excel文件");
		return;
	}else{
		var fileExtend = excelPath.substring(excelPath.lastIndexOf('.')).toLowerCase(); 
		if(fileExtend == '.xls'){
		}else{
			alert("文件格式需为'.xls'格式");
		    return;
		}
	}
	//提交表单
	document.getElementById("empForm").action="<%=request.getContextPath()%>/EmpExcel.action.EmpExcelAction.do?method=importEmployeeInfos";  
	document.getElementById("empForm").submit();
}

   <input type="file" id="excelPath" name="excelPath"/>&nbsp;&nbsp;
   <input type="button"  value="导入Excel" οnclick="importEmp()"/>

action:

/**
	 * Excel中的数据导入到数据库中(Excel中的字段已限定)
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward importEmployeeInfos(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		logger.debug(">>>importEmployeeInfos()");
		//从页面接收参数:文件的路径
		String excelPath = request.getParameter("excelPath");
		//输入流
		InputStream fis = new FileInputStream(excelPath);
		//得到解析Excel的实体集合
		List<EmployeeInfo> infos = ImportEmployee.importEmployee(fis);
		
		//遍历解析Excel的实体集合
		for(EmployeeInfo info:infos) {
			//判断员工编号是否存在(存在:做修改操作;不存在:做新增操作)
			EmployeeInfo info1 = this.selectEmpByEmpNum(info.getEmployeeNumber());
			if(info1 == null) {
				//把实体新加到数据库中
				this.service.addEmployeeInfo(info);
			}else{
				//把personId封装到实体
				info.setPersonId(info1.getPersonId());
				//更新实体
				this.updatEmployeeInfo(info);
			}
		}
		//关闭流
		fis.close();
		logger.debug("<<<importEmployeeInfos()");		
		return this.findEmployeeInfos(mapping, form, request, response);
	}
	
	/**
	 * 根据员工编号查找一个员工实体
	 * @param employeeNumber 员工编号
	 * @return 实体
	 */
	private EmployeeInfo selectEmpByEmpNum(String employeeNumber) {
		
		logger.debug(">>>selectEmpByEmpNum(String employeeNumber)");
		
		EmployeeInfo employeeInfo = new EmployeeInfo();
		employeeInfo.setEmployeeNumber(employeeNumber);
		
		EmployeeInfo info =this.service.selectEmployeeInfoByEmpNum(employeeInfo);
		
		logger.debug("<<<selectEmpByEmpNum(String employeeNumber)");
		
		return info;
	}
	
	/**
	 * 更新一条员工信息
	 * @param employeeInfo 已封装的实体
	 * 
	 */
	private void updatEmployeeInfo(EmployeeInfo employeeInfo) {
		
		logger.debug(">>>selectEmpByEmpNum(String employeeNumber)");
		
		this.service.updateEmployeeInfo(employeeInfo);
		
		logger.debug(">>>selectEmpByEmpNum(String employeeNumber)");
		
	}

 导入类:

 

public class ImportEmployee {
	/**
	 * 解析Excel文件中的数据并把每行数据封装成一个实体
	 * @param fis 文件输入流
	 * @return List<EmployeeInfo> Excel中数据封装实体的集合
	 */
	public static List<EmployeeInfo> importEmployee(InputStream fis) {
		
		List<EmployeeInfo> infos = new ArrayList<EmployeeInfo>();
		EmployeeInfo employeeInfo = null;
		
		try {
			//打开文件
			Workbook book = Workbook.getWorkbook(fis);
			//得到第一个工作表对象
			Sheet sheet = book.getSheet(0);
			//得到第一个工作表中的总行数
			int rowCount = sheet.getRows();
			//日期格式化
			DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			//循环取出Excel中的内容
			for (int i = 1; i < rowCount; i++) {
				employeeInfo = new EmployeeInfo();
				Cell[] cells = sheet.getRow(i);
				employeeInfo.setOrgId(Long.parseLong(cells[0].getContents()));
				employeeInfo.setEmployeeNumber(cells[1].getContents().toString());
				employeeInfo.setFullName(cells[2].getContents().toString());
				employeeInfo.setSex(cells[3].getContents().toString());
				employeeInfo.setDateOfBirth(new Date());
				employeeInfo.setTownOfBirth(cells[5].getContents().toString());
				employeeInfo.setNationalIdentifier(cells[6].getContents().toString());
				infos.add(employeeInfo);
			}
			
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return infos;
	}
}

 写的比较简单,如有错误,希望讨论和指正。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值