文章标题

                                                      springmvc框架项目将execl格式的数据导入数据库
  1. 导入步骤
    首先将execl文件上传至服务器端,然后通过Jxl解析execl数据并通过java类组织数据,最后将数据存入数据库。
    2.具体代码实现
    页面显示代码:
    将文件标签伪装成button 当选择好上传文件后将文件提交至服务器端

     `
`


`
/css/  
 #file {
position:absolute;
margin:0px;
opacity:0;
filter:alpha(opacity:0);
z-index:999;
width:100px;
height:20px;
}

服务器端处理  

public ModelAndView plansToDBController (HttpServletRequest request,
            HttpServletResponse response) throws Exception{
        Workbook rwb = null;
        String flag="";
        boolean success=false;
        int n=0;//记录导入记录条数
        VOPlanout vop=new VOPlanout();
        Dict dict=null;
        PlanoutService planoutService=null;
        ServletContext ctx = null;
        BeanFactory factory = null;
        Logger log = Logger.getLogger(this.getClass());
        try {
            ctx = this.getServletContext();
            factory = (BeanFactory) WebApplicationContextUtils
                    .getWebApplicationContext(ctx);
            planoutService = (PlanoutService) factory.getBean("planoutService");
            dict=(Dict)factory.getBean("dict");
        } catch (Exception e) {
            log.error("取得 spring PlanoutService 类时失败", e);
        }
        // 上传文件
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        for (Iterator it = multipartRequest.getFileNames(); it
                .hasNext();) {
            String key = it.next();
            MultipartFile multipartFile = multipartRequest.getFile(key);
            if ((multipartFile != null) && !multipartFile.isEmpty()) {
                InputStream input = multipartFile.getInputStream();
                rwb = Workbook.getWorkbook(input);
                // 获取第一张Sheet表
                 Sheet rs = rwb.getSheet(0);
                 String year=rs.getCell(0,0).getContents()==null?"":rs.getCell(0, 0).getContents().substring(0,4);
                 String dwfullnames=rs.getCell(1, 1).getContents()==null?"":rs.getCell(1, 1).getContents();
                 String dwid=dict.getdeptID(dwfullnames);//根据单位全称得到单位id
                 if("单位名称有误!".equals(dwid)){
                     flag="execl表单,申报单位输入不正确!";
                 }else{
                     //System.out.println("----------开始执行2----------------------");
                    for (int k = 3; k 

你可能感兴趣的:(EXECL数据文件导,spring,mvc,java)