POI本地接口获取图片导出到EXCEL

1.先通过HttpServletRequest获取本地接口http:/ip:xxxx(端口号)

String url = request.getHeader("Origin");

2.再在接口后面加上获取图片的方法

String wholeurl = url+图片接口;

3.将图片插入到EXCEL里

 /*带图片的插入*/
   private static  Integer writeTablePicBodyOrFooterFromMapList(List<Map<String,Object>> list,Integer rowIndex,int maxcols,List<String> fieldList,HSSFSheet sheet,HSSFWorkbook workbook,TableType type) throws Exception{ 
        //正式的数据
        for (int i = 0; i < list.size(); i++) { 
            HSSFRow row = sheet.createRow(rowIndex);
            Map<String,Object> vo = list.get(i); // 得到导出对象.
            for (int j = 0; j < maxcols; j++) { 
                HSSFCell cell = row.createCell(j);
                if(type==TableType.FOOTER)  { cell.setCellStyle(getFooterCellStyler(workbook));}else { 
                    cell.setCellStyle(getBoodyCellStyler(workbook));
                }
                // 取得类型,并根据对象类型设置值.
                if (vo.get(fieldList.get(j)) == null) { 
                    cell.setCellValue("");// 如果数据是null,就填入空格.
                }else { 
                    Class<?> fieldType=vo.get(fieldList.get(j)).getClass();
                    if(String.class == fieldType) { 
                        if ((String)vo.get(fieldList.get(j))!=null&&((String)vo.get(fieldList.get(j))).indexOf("http://")!=-1){ 
                            row.setHeightInPoints(50);
                            sheet.setColumnWidth(j, 256*10+184);
                            HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
                            setpic(patriarch,workbook,i+rowIndex,j,1,1,(String)vo.get(fieldList.get(j)));
                            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                            cell.setCellValue(((String) vo.get(fieldList.get(j))).substring(47));
                        } else { 
                            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                            cell.setCellValue((String) vo.get(fieldList.get(j)));// 如果数据存在就填入,不存在填入空格.
                        }
                    } else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) { 
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        Integer value=(Integer) vo.get(fieldList.get(j));
                        cell.setCellValue(value);
                    } else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) { 
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue((Long)vo.get(fieldList.get(j)));
                    } else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) { 
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue((Float)vo.get(fieldList.get(j)));
                    } else if (Double.TYPE == fieldType||Double.class == fieldType) { 
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        Double value=(Double) vo.get(fieldList.get(j));
                        cell.setCellValue(value);
                    } else if ((BigDecimal.class == fieldType)) {   //一般数据库传过来的map数字都是这种类型
                        Double value = ((BigDecimal) vo.get(fieldList.get(j))).doubleValue();
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(value);
                    } else if (Character.TYPE == fieldType) { 

                    }
                }
            }
            rowIndex++;
        }
        return rowIndex;
    }
    private  static void setpic(HSSFPatriarch patriarch,HSSFWorkbook workbook,int rowNum,int colNum,int rowspan,int colspan,String picurl) throws Exception{ 
        // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        URL url = new URL(picurl);
        HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
        httpUrl.connect();
        //将图片读到BufferedImage
        BufferedImage bufferImg = ImageIO.read(httpUrl.getInputStream());
        if (bufferImg!=null)
        ImageIO.write(bufferImg, "jpg", byteArrayOut);
        httpUrl.disconnect();
        /** * 该构造函数有8个参数 * 前四个参数是控制图片在单元格的位置,分别是图片距离单元格left,top,right,bottom的像素距离 * 后四个参数,前两个表示图片左上角所在的cellNum和 rowNum,后两个参数对应的表示图片右下角所在的cellNum和 rowNum, * excel中的cellNum和rowNum的index都是从0开始的 */
        //图片一导出到单元格B2中
        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,(short)colNum , rowNum, (short)(colNum+colspan), rowNum+rowspan);
        // 插入图片
        patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
    }
    原文作者:小手cool
    原文地址: https://blog.csdn.net/qq_38030465/article/details/122186720
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞