Excel上传文件带图片

 @RequestMapping("/addempExcel")
    public ETJSONResponse addEmps(MultipartFile pic, HttpServletRequest request, HttpServletResponse respon) throws IOException, InterruptedException, ParseException {
        ETJSONResponse response = new ETJSONResponse();
        ServletContext application = request.getSession().getServletContext();
        //文件名字
        String name = pic.getOriginalFilename();
        //上传的路径
        String path = application.getRealPath("/wd/" + name);
        //文件的类型
        String fEXT= FilenameUtils.getExtension(pic.getOriginalFilename());
        FileInputStream fis =null;
        Workbook workbook = null;
        Sheet sheet =null;
        List<String>paths=null;
        try
        {
            //获取一个绝对地址的流
            fis = (FileInputStream) pic.getInputStream();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        workbook=new XSSFWorkbook(fis);
        Map<String, PictureData> maplist=null;
        sheet = workbook.getSheetAt(0);
        maplist=getPictures2((XSSFSheet) sheet);
        try {
            paths=printImg(maplist,pic,request,respon);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //        pic.transferTo(new File(path));
            //得到一个工作表
        //获得表头
        Row rowHead = sheet.getRow(0);
        //获得数据的总行数
        int totalRowNum = sheet.getLastRowNum();
        //要获得属性
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        List<Emp> emps=new ArrayList<>();
        for(int i = 1 ; i <= totalRowNum ; i++) {
            //获得第i行对象
            Row row = sheet.getRow(i);
            Emp emp=new Emp();
            emp.setEname(row.getCell(1).getStringCellValue());
            emp.setJob(row.getCell(2).getStringCellValue());


            /*double value = cell.getNumericCellValue();
            Date date = org.apache.poi.ss.usermodel.DateUtil
                    .getJavaDate(value);
            cellValue = sdf.format(date);*/
            emp.setHiredate(sdf.parse(row.getCell(3).getStringCellValue()));
            emp.setSal(Double.parseDouble(row.getCell(4).getNumericCellValue()+""));
            emp.setDeptno((int)row.getCell(5).getNumericCellValue());
            emps.add(emp);
        }
        for (Map.Entry<String, PictureData> entry : maplist.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        workbook.close();
        if (fis != null){
            fis.close();
        }
       Map<String,Object>map=new HashMap<>();
        map.put("emps",emps);
        map.put("paths",paths);
        response.setMsg(map);
//        pic.transferTo(new File(path));

        //获取文件流
        return response;
    }

 private List<String>printImg(Map<String, PictureData> maplist,MultipartFile multipartFile,HttpServletRequest request, HttpServletResponse respon) throws IOException {
        Object key[] = maplist.keySet().toArray();
        List<String>paths=new ArrayList<>();
        String filePath = "";
        ServletContext application = request.getSession().getServletContext();
        for (int i = 0; i < maplist.size(); i++) {
            // 获取图片流
            PictureData pic = maplist.get(key[i]);
            // 获取图片索引
            String picName = key[i].toString();
            // 获取图片格式
            String ext = pic.suggestFileExtension();
            byte[] data = pic.getData();
//            filePath = "D:\\img\\pic" + picName + "." + ext;
            //文件上传七牛
//            QiNiuUtils.uploadOneObject(data,"111_"+picName + "." + ext);
            //图片保存路径
            filePath=application.getRealPath("/files/" + picName + ".jpg");
            System.out.println(filePath);
            paths.add(filePath);
            multipartFile.transferTo(new File(filePath));
//            FileOutputStream out = new FileOutputStream(filePath);
            /*out.write(data);
            out.close();*/
        }
        return paths;
    }
//获取图片
    private Map<String, PictureData> getPictures2(XSSFSheet sheet) {
        Map<String, PictureData> map = new HashMap<String, PictureData>();
        List<POIXMLDocumentPart> list = sheet.getRelations();
        for (POIXMLDocumentPart part : list) {
            if (part instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) part;
                List<XSSFShape> shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    XSSFPicture picture = (XSSFPicture) shape;
                    XSSFClientAnchor anchor = picture.getPreferredSize();
                    CTMarker marker = anchor.getFrom();
                    String key = marker.getRow() + "-" + marker.getCol();
                    map.put(key, picture.getPictureData());
                }
            }
        }
        return map;
    }
<dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.17version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>3.17version>
        dependency>

你可能感兴趣的:(Excel上传文件带图片)