当前位置: 首页 > article >正文

Java导出图片到excel

1、例如你有这样一个集合,具体结合你的业务场景
 

@Data
public class Student {
    /**
     * 姓名
     */
    private String xh;
    /**
     * 学号
     */
    private String xm;
    /**
     * 照片
     */
    private byte[] zp;
}

2、相关代码

    @RequestMapping(value = "/quereImgByPkid/{pkid}",method = RequestMethod.GET)
    public  void  test(@PathVariable("pkid") String pkid, HttpServletResponse response){
        try {
            // 业务代码
            List<Student> students = xscnsService.queryImgByPkidAndType(pkid);
            // 设置响应头
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=students.xlsx");

            try (OutputStream outputStream = response.getOutputStream();
                 Workbook workbook = new XSSFWorkbook()) {

                Sheet sheet = workbook.createSheet("Student Data");

                // 创建表头
                Row header = sheet.createRow(0);
                header.createCell(0).setCellValue("学号");
                header.createCell(1).setCellValue("姓名");
                header.createCell(2).setCellValue("照片");

                int rowIndex = 1;
                for (Student student : students) {
                    Row row = sheet.createRow(rowIndex++);

                    // 设置行高(行高设置为100点)
                    row.setHeightInPoints(100);

                    row.createCell(0).setCellValue(student.getXh());
                    row.createCell(1).setCellValue(student.getXm());

                    // 插入照片
                    int pictureIdx = workbook.addPicture(student.getZp(), Workbook.PICTURE_TYPE_JPEG);
                    CreationHelper helper = workbook.getCreationHelper();
                    Drawing<?> drawing = sheet.createDrawingPatriarch();
                    ClientAnchor anchor = helper.createClientAnchor();
                    anchor.setCol1(2); // 照片列
                    anchor.setRow1(rowIndex - 1); // 当前行

                    // 将图片大小调整为单元格大小
                    anchor.setCol2(3); // 结束列
                    anchor.setRow2(rowIndex); // 结束行
                    Picture pict = drawing.createPicture(anchor, pictureIdx);
                    // 不调用resize,让图片完全适应单元格
                }

                // 将数据写入到响应输出流中
                workbook.write(outputStream);
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3、访问接口得以下载,如下

4、拓展(实战代码)

    @GetMapping("/exportFjPf")
    public void exportFjPf(@RequestParam String uuid, HttpServletResponse response) {
        wsjcService.exportFjPf(uuid,response);
    }
    @Override
    public void exportFjPf(String uuid, HttpServletResponse response) {
        //1、获取缓存的数据
        Object obj = NHRedisUtils.getRedisResult(uuid);
        if (obj == null) {
            throw new NHWarmingException("导出Excel失败,请稍后再试");
        }
        Map<String, Object> map = (Map<String, Object>) obj;
        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
        // 文件名字,防止乱码
        String fileName = "房间评分信息.xlsx";

        // 针对不同的浏览器进行文件名编码
        String encodedFileName = null;
        try {
            encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        response.setContentType("application/octet-stream");

        // 对于不同的浏览器设置不同的 Content-Disposition 头
        String headerValue = String.format("attachment; filename=\"%s\"; filename*=UTF-8''%s", encodedFileName, encodedFileName);
        response.setHeader("Content-Disposition", headerValue);

        try (OutputStream outputStream = response.getOutputStream(); Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("房间评分信息");
            // 设置统一的列宽(比如统一设置为20个字符宽度)
            int columnWidth = 20 * 256;
            // 创建一个单元格样式,设置居中对齐
            CellStyle cellStyle = workbook.createCellStyle();
            // 水平居中
            cellStyle.setAlignment(HorizontalAlignment.CENTER); 
            // 垂直居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            // 创建一个标题单元格样式,设置居中对齐和加粗字体
            CellStyle headerStyle = workbook.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 设置字体加粗
            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);
            // 创建表头字段
            String[] headers = {"序号", "校区", "楼栋","楼层","房间","学院","专业","班级","所属年级","所属辅导员","评分","评分等级","评分人","评分时间","评语","上传图片"};
            // 对应的字段
            String[] column = {"ROW_ID", "XQMC", "LDMC","LCMC","FJ","BMMC","ZYMC","BJMC","SSNJ","FDY","PF","PFDJMC","PFR","PFSJ","PY"};
            // 创建表头
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                headerRow.setHeightInPoints(30);
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
                cell.setCellStyle(headerStyle);
                // 设置每一列的宽度
                sheet.setColumnWidth(i, columnWidth);
            }

            int rowIndex = 1;
            for (Map<String,Object> item : list) {
                Row row = sheet.createRow(rowIndex++);
                for (int i = 0; i < column.length; i++) {
                    // 设置行高 100点
                    row.setHeightInPoints(100);
                    Cell cell = row.createCell(i);
                    cell.setCellValue(getStrByObject(item.get(column[i])));
                    cell.setCellStyle(cellStyle);
                }
                List<byte []> pictureList =  getPicture(item.get("SCTP"));
                for (int i = 0; i < pictureList.size(); i++) {
                    sheet.setColumnWidth(column.length + i,columnWidth);
                    // 插入照片
                    int pictureIdx = workbook.addPicture(pictureList.get(i), Workbook.PICTURE_TYPE_JPEG);
                    CreationHelper helper = workbook.getCreationHelper();
                    Drawing<?> drawing = sheet.createDrawingPatriarch();
                    ClientAnchor anchor = helper.createClientAnchor();
                    anchor.setCol1(column.length + i); // 照片列
                    anchor.setRow1(rowIndex - 1); // 当前行

                    // 将图片大小调整为单元格大小
                    anchor.setCol2(column.length + i + 1); // 结束列
                    anchor.setRow2(rowIndex); // 结束行
                    Picture pict = drawing.createPicture(anchor, pictureIdx);
                    //不调用resize,让图片完全适应单元格
                }
            }

            // 将数据写入到响应输出流中
            workbook.write(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    private List<byte[]> getPicture(Object sctp) {
        List<byte[]> bytes = new ArrayList<>();
        String strByObject = getStrByObject(sctp);
        if(strByObject == null){
            return bytes;
        }
        // 开始远程调用
        Object fjDetail = dynamicServiceFeignClient.getDetailed(strByObject);
        // 调用日志
        logger.warn("附件主表req:{},rep:{}", strByObject, fjDetail);
        List<Map<String, Object>> maps = new ArrayList<>();
        if (fjDetail != null) {
            Map map = (Map) fjDetail;
            Object data = map.get("data");
            maps = (List<Map<String, Object>>) data;
        }
        List<String> uuids = maps.stream().map(a -> (String) a.get("uuid")).collect(Collectors.toList());
        for (int i = 0; i < uuids.size(); i++) {
            // 开始调用详细附件明细
            Response fileMessage = docrepoServiceFeignClient.getFileInfo(uuids.get(i), "admin", "admin");
            // 打印日志
            logger.warn("根据uuid拿到资源req:{},resp:{}", uuids.get(i), fileMessage);
            if (fileMessage == null || fileMessage.status() != 200) {
                continue;
            }
            // 拿到文件流
            try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()){
                InputStream imageStream = fileMessage.body().asInputStream();
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = imageStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, bytesRead);
                }
                byte[] imageBytes = byteArrayOutputStream.toByteArray();
                bytes.add(imageBytes);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return bytes;
    }

    private String getStrByObject(Object str){
        if(str != null){
            return str.toString();
        }
        return null;
    }

结果:


http://www.kler.cn/a/289085.html

相关文章:

  • 实验5:网络设备发现、管理和维护
  • zabbix搭建钉钉告警流程
  • 软件测试面试大全(含答案+文档)
  • 代码随想录第二十一天| 669. 修剪二叉搜索树 108.将有序数组转换为二叉搜索树 538.把二叉搜索树转换为累加树
  • 【mysql】使用宝塔面板在云服务器上安装MySQL数据库并实现远程连接
  • 某app最新版 vmp算法分析一
  • 【算法】贪心算法解析:基本概念、策略证明与代码例题演示
  • ARM汇编指令
  • Pandas 2-读取文件
  • CSRF 概念及防护机制
  • 3D幻想空间:Scratch中探索虚拟世界的奥秘
  • 【量化分析】Python、JavaScript(Node.js)、Java、C#和Ruby五种主流语言的实例代码给大家演示一下如何获取股票实时交易数据
  • 深入理解MySQL慢查询优化(2) -- SQL的执行流程
  • OCI编程高级篇(十八) OCI连接池概念
  • 如何打造一个成功的直播矩阵
  • 【科研新人必看】什么是期刊等级,SCI、核心期刊、省刊
  • qt6 socket 不使用代理 socket error: The proxy type is invalid for this operation
  • 8.29 C++
  • 常用Pandas操作(笔记整理)
  • 前端学习笔记-Web APIs篇-02
  • 基于机器学习的工业制造缺陷分析预测系统
  • 运动多线激光三维重建
  • 解决bug: RuntimeError: Address already in use,一个linux下pytorch多卡训练tcp端口占用的bug
  • SpringCloudGateway网关技术
  • 笔记整理—uboot番外(2)find_cmd函数
  • Selenium+Python自动化测试环境搭建