EasyExcel导出图片

在excel里导出图片:
在这里插入图片描述

1、数据实体

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import lombok.Data;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.net.URL;

@Data
@HeadRowHeight(25)
@ContentRowHeight(120)
public class ExcelDB {

    @ColumnWidth(35)
    @ExcelProperty(value = "规格")
    @ContentFontStyle(fontName = "Calibri", fontHeightInPoints = 12)
    @ContentStyle(wrapped = true, horizontalAlignment = HorizontalAlignment.CENTER,
            verticalAlignment = VerticalAlignment.CENTER)   // 自动换行、水平、垂直居中
    private String name;

    @ColumnWidth(35)
    @ExcelProperty("图片")
    private URL imgUrl;

    @ColumnWidth(35)
    @ExcelProperty(value = "介绍")
    @ContentFontStyle(fontName = "Calibri", fontHeightInPoints = 12)
    @ContentStyle(wrapped = true, horizontalAlignment = HorizontalAlignment.CENTER,
            verticalAlignment = VerticalAlignment.CENTER)   // 自动换行、水平、垂直居中
    private String introduce;

}

2、web中下载

import com.alibaba.excel.EasyExcel;
import com.example.demo02.entity.ExcelDB;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @PostMapping("exportExcel")
    public void exportExcel(HttpServletResponse response){
        List<ExcelDB> excelDBS = new ArrayList<>();
        // 测试数据
        ExcelDB excelDB = new ExcelDB();
        excelDB.setName("数据1");
        excelDB.setIntroduce("数据介绍");
        try {
            excelDB.setImgUrl(new URL("http://www.baidu.com/img/bdlogo.png"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        excelDBS.add(excelDB);

        // 请求头设置
        response.setHeader("Content-Disposition", "attachment;filename=" + "XXXXX.xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "octets-stream");
        try {
            EasyExcel.write(response.getOutputStream(), ExcelDB.class).sheet("模板").doWrite(excelDBS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值