EasyExcel export pictures

Export pictures in excel:
Insert image description here

1. Data entity

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. Download from the 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();
        }
    }
    
}

Guess you like

Origin blog.csdn.net/qq_42182034/article/details/122092171