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.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值