poi导出Excel+图片

引入依赖

 		<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>

代码实现

Controller

	@GetMapping("/exportData")
    public void exportData(HttpServletResponse res){
        service.exportData(res);
    }

Service

	@Override
    public void exportData(HttpServletResponse res) {
        List<BodySignOut> content = listRestApiResultVo.getContent();
        try (HSSFWorkbook workbook = new HSSFWorkbook()) {
            //设置导出的文件的名字
            String fileName = Constants.EXCEL_SIGN_FILENAME;
            //创建sheet页
            XSSFSheet sheet = workbook.createSheet();
            //创建第一行
            XSSFRow row = sheet.createRow(0);
            //表头
            XSSFCell cell0 = row.createCell(0);
            cell0.setCellValue(Constants.EXCEL_SIGN_ID);
            XSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(Constants.EXCEL_SIGN_TYPE);
            XSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(Constants.EXCEL_SIGN_VALUE);
            XSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(Constants.EXCEL_SIGN_DATE);

            //内容
            int rowNum = 1;
            for (BodySignOut body : content) {
                XSSFRow rowContent = sheet.createRow(rowNum++);
                rowContent.createCell(0).setCellValue(body.getId());
                rowContent.createCell(1).setCellValue(body.getBodySignType());
                rowContent.createCell(2).setCellValue(body.getBodySignValue());
                rowContent.createCell(3).setCellValue(body.getPartitionTime());

                //写入图片
                String filePath = "D:/1/1.jpg"; //图片地址
                String fileType = "jpg"; //图片类型
                if(StringUtils.isEmpty(filePath) || StringUtils.isEmpty(fileType)){
                    //结束此次写出
                }
                File file = new File(filePath);
                if(!file.canRead()){
                    //结束此次写出
                }
                BufferedImage read = ImageIO.read(file);
                if (read ==null){
                    //结束此次写出
                }
                //写入到outputStream
                ImageIO.write(read,fileType,outputStream);
                XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
                XSSFCreationHelper creationHelper = workbook.getCreationHelper();
                XSSFClientAnchor clientAnchor = creationHelper.createClientAnchor();
                clientAnchor.setCol1(4); //列
                clientAnchor.setRow1(i+1); //行
                XSSFPicture picture = drawingPatriarch.createPicture(clientAnchor, workbook.addPicture(outputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                picture.resize(1,1); //x轴,y轴  这里表示图片在一个单元格里面 
            }
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            TdLog.error(Constants.EXCEL_SIGN_ERROR, e);
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值