java下载excel(excel含有多张网络图片)

今天整理笔记的时候看到之前的一段代码,是有关于下载含有网络图片的excle,把代码贴出来大家让大家指导一下
下载下来的excel类似这种效果,我这边处理的是网络图片哟
在这里插入图片描述

1.部分代码展示

@RequestMapping(value = "downReadilyPager")
@ResponseBody
public void downReadilyPager(HttpServletRequest request, HttpServletResponse response) {
    try {
        ReadilyRequest readilyRequest = new ReadilyRequest();
        List<Readily> list = new ArrayList<Readily>();
        String ids = request.getParameter("ids");
        if (!StringUtils.isBlank(ids)) {
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                Readily temp = readilyService.get(Integer.parseInt(id));
                Account account = accountService.get(temp.getAccountId());
                temp.setAccount(account);
                list.add(temp);
        }
    }
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
    SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
    HSSFSheet sheet = hssfWorkbook.createSheet("报表管理");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    //设置表格样式
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 4000);
    sheet.setColumnWidth(2, 8000);
    sheet.setColumnWidth(3, 8000);
    sheet.setColumnWidth(4, 8000);
    sheet.setColumnWidth(5, 8000);
    sheet.setColumnWidth(6, 8000);
    sheet.setColumnWidth(7, 8000);
    sheet.setColumnWidth(8, 8000);
    sheet.setColumnWidth(9, 8000);
    sheet.setColumnWidth(10, 8000);
    //一定要放在循环外,只能声明一次
    String fileName = "报表-" + simpledate.format(new Date());
    cell.setCellValue(fileName);
    row = sheet.createRow(1);
    cell = row.createCell(0);
    cell.setCellValue("主题");
    cell = row.createCell(1);
    cell.setCellValue("内容");
    cell = row.createCell(2);
    cell.setCellValue("地址");
    cell = row.createCell(3);
    cell.setCellValue("状态");
    cell = row.createCell(4);
    cell.setCellValue("拍摄时间");
    cell = row.createCell(5);
    cell.setCellValue("处理时间");
    cell = row.createCell(6);
    cell.setCellValue("描述");
    cell = row.createCell(7);
    cell.setCellValue("昵称");
    cell = row.createCell(8);
    cell.setCellValue("电话");
    cell = row.createCell(9);
    cell.setCellValue("图片");

    int line = 2;
    //循环 开始赋值
    if (list != null && list.size() > 0) {
        for (int i = 0; i < list.size(); i++) {
            Readily s = list.get(i);
            row = sheet.createRow(line++);
            if (s.getTitle() != null) {
                cell = row.createCell(0);
                row.setHeight((short) (6 * 200));
                cell.setCellValue(s.getTitle());
            }
            if (s.getContent() != null) {
                cell = row.createCell(1);
                cell.setCellValue(s.getContent());
            }
            if (s.getAddress() != null) {
                cell = row.createCell(2);
                cell.setCellValue(s.getAddress());
            }
            if (s.getState() != null) {
                cell = row.createCell(3);
                if (s.getState() == 0) {
                    cell.setCellValue("未处理");
                } else if (s.getState() == 1) {
                    cell.setCellValue("已经处理");
                }
            }
            if (s.getTime() != null) {
                cell = row.createCell(4);
                cell.setCellValue(simpledate.format(s.getTime()));
            }
            if (s.getUpdateTime() != null) {
                cell = row.createCell(5);
              cell.setCellValue(simpledate.format(s.getUpdateTime()));
            }
            if (s.getRemark() != null) {
                cell = row.createCell(6);
                cell.setCellValue(s.getRemark());
            }
            if (s.getAccount().getNickName() != null) {
                cell = row.createCell(7);
                cell.setCellValue(s.getAccount().getNickName());
            }
            if (s.getAccount().getPhone() != null) {
                cell = row.createCell(8);
                cell.setCellValue(s.getAccount().getPhone());
            }
            if (!StringUtils.isBlank(s.getImgUrl())) {
                String[] imgUrls = s.getImgUrl().split(",");
                int length = imgUrls.length;
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                //for循环中处理图片的关键代码
                for (int j = 0; j < length; j++) {
                    BufferedImage bufferImg = null;
                    //网络图片用下面的方法处理不适合本地图片
                    URL url = new URL(imgUrls[j]);
                    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                    bufferImg = ImageIO.read(url);
                    ImageIO.write(bufferImg, "jpg", byteArrayOut);
//***(short) (9 + j), (2 + i),   图片起点的位置    (short) (10 + j), (3 + i) 图片终点的位置***
                        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) (9 + j), (2 + i), (short) (10 + j), (3 + i));
                        patriarch.createPicture(anchor, hssfWorkbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                    }
                }
            }
        }
        OutputStream fout = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
        response.setContentType("application/msexcel;charset=UTF-8");
        //表格写出
        hssfWorkbook.write(fout);
        fout.flush();
        fout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.jar包下载地址

https://download.csdn.net/download/weixin_38638883/10652650	
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值