美文网首页
导出导入excel

导出导入excel

作者: 打死你的小白兔 | 来源:发表于2018-04-02 13:01 被阅读0次
<input type="button" value="导出Excel" id="exportexcel">
<input type="button" value="导入Excel" id="importexcel">
    
    $("#exportexcel").click(function(){
    location="person/exportExcel.do";
    })
    
    $("#importexcel").click(function(){
     if($("#myfile").val()==""){
     alert("请先选择要导入的Excel文件!!!");
     }else{
     excel.submit();
     }
    })
    })
    private List<Dept> listDept = new ArrayList<Dept>();
    private List<Person> listPerson = new ArrayList<Person>();
    @Autowired
    private DeptServiceImpl deptdb;
    @Autowired
    private PersonServiceImpl perdb;
    private File myfile;//名必须为myfile ,同时有set get方法
    private String myfileFileName;//名必须 为:myfile+FileName有set get方法
    // 有set get方法,开发struts多选框的回显
    @RequestMapping("exportExcel")
    public String exportExcel( HttpServletResponse response) throws Exception{
        // 初始化HttpServletResponse对象
        // 定义表的标题
        String title = "开发部员工信息";

        //定义表的列名
        String[] rowsName = new String[] { "用户编号", "姓名", "性别", "年龄", "入职时间"
                , "照片", "技能","学历","简历","部门"};

        //定义表的内容
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        List<Person> listPerson =perdb.list();
        for (int i = 0; i < listPerson.size(); i++) {
            Person per = listPerson.get(i);
            objs = new Object[rowsName.length];
            objs[0] = per.getId();
            objs[1] = per.getName();
            objs[2] = per.getSex();
            objs[3] = per.getAge();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = df.format(per.getJobtime());
            objs[4] = date;
            objs[5] = per.getFilepath();
            objs[6] = per.getSkill();
            objs[7] = per.getDegree();
            objs[8] = per.getResume();
            objs[9] = per.getDept().getDname();
            dataList.add(objs);
        }

        // 创建ExportExcel对象
        ExportExcel ex = new ExportExcel(title, rowsName, dataList);

        // 输出Excel文件
        try {
            OutputStream output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition",
                    "attachment; filename=PersonList.xls");//保存人excel文件名
            response.setContentType("application/msexcel");
            ex.export(output);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }
    @RequestMapping("importExcel")
    public String importExcel(MultipartFile file,HttpServletRequest request) throws Exception{
         String realPath = request.getSession().getServletContext().getRealPath("upload");
         String fileName = file.getOriginalFilename();//取文件名
         //解决同名问题
         //fileName = UUID.randomUUID().toString().replace("-", "")+fileName.substring(fileName.lastIndexOf("."));
         File f1=new File(realPath,fileName);
         if(!f1.exists()){
             f1.mkdirs();//如果不存在则创建其目录
         }
        // 上传文件到服务器中
        file.transferTo(f1);

        Person user = new Person();// 新建一个user对象
        Dept dept = new Dept();// 新建一个dept对象

        // 获取服务器中文件的路径
        String path = request.getSession().getServletContext().getRealPath("")
                + "/upload/" + fileName;

        try {
            InputStream is = new FileInputStream(path);
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

            // 循环工作表Sheet
            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
                HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
                if (hssfSheet == null) {
                    continue;
                }

                // 循环行Row
                for (int rowNum = 3; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                    HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                    if (hssfRow == null) {
                        continue;
                    }

                    // 循环列Cell
                    // 姓名   性别  年龄  入职时间    照片  技能  学历  简历  部门  
                    user.setName(getValue(hssfRow.getCell(1)));
                    user.setSex(getValue(hssfRow.getCell(2)));
                    user.setAge(Integer.parseInt(getValue(hssfRow.getCell(3))));
                    //处量时间
                    String da=getValue(hssfRow.getCell(4));
                    SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    user.setJobtime(sd.parse(da));//把字符转为日期
                    user.setFilepath(getValue(hssfRow.getCell(5)));
                    user.setSkill(getValue(hssfRow.getCell(6)));
                    user.setDegree(getValue(hssfRow.getCell(7)));
                    user.setResume(getValue(hssfRow.getCell(8)));
                    //这里很重要,通过部门列表然后与excel中的部门字段进行对比,匹配后获取对应的did
                    String dname = getValue(hssfRow.getCell(9));//获取excel中的部门字段
                    listDept = deptdb.listDept();//得到数据库中的部门列表
                    for (Dept dd : listDept) {//增强for循环
                        if (dd.getDname().equals(dname)) {//如果两者匹配
                            dept.setDid(dd.getDid());//则得到对应的did,并设置dept对象的did
                            user.setDept(dept);//再把dept对象设置到user对象中
                        }
                    }
                   perdb.addPerson(user);//写入到数据中
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return "redirect:list.do";//返回列表展示
    }

相关文章

  • DataGrip 导入 Excel 数据

    目前DataGrip不支持导入Excel,可以将Excel导出为csv文件进行导入:文件=>导出=》更改文件类型=...

  • SpringBoot解析Excel

    现在很多web应用中,导入excel导出excel很常见,这篇文章就讲讲导入excel文件。 以批量导入课程为例 ...

  • 2018-04-09 需求整理

    文件下载导出Excel模板导出Excel数据 文件上传导入Excel照片上传 数据管理 学生管理CURD搜索 教师...

  • VUE框架下搭建在线Excel应用

    本项目实现功能:VUE框架中 嵌入表格控件SpreadJS,实现导入Excel、导出Excel、导出PDF、打印表...

  • Excel表格导入数据库及其下载

    Excel表格的导入导出 1.读取指定Excel到数据库 springMvc上传获取文件 //导入excel表格p...

  • 数据导入、导出

    导入csv文件 导入文本文件 导入Excel文件(xls,xlsx) 中文路径问题 数据导出

  • 导出导入excel

  • excel导入导出

    // 导入excelfunction import_excel($file){// 判断文件是什么格式$type ...

  • Excel导入导出

    在后台管理系统中,常遇到Excel导入导出的需求,整理了如下两个工具类。 一、导入 导入工具类 使用方式 二、导出...

  • 基于C++的灵活操作Excel导入导出数据

    VC++ 灵活操作Excel导入导出数据,不但可以生成Excel文件,还可以从Excel文件导入数据到VC的Lis...

网友评论

      本文标题:导出导入excel

      本文链接:https://www.haomeiwen.com/subject/sneocftx.html