引入依赖
compile group: 'cn.afterturn',name:'easypoi-spring-boot-starter',version:'4.1.0'
这个好像不支持用office打开
@Data
public class EmployeeWithFileLinkDTO {
@Excel(name = "员工ID", width = 15)
private Long id;
@Excel(name = "员工姓名", width = 15)
private String name;
@Excel(name = "图片")
private String image;
public EmployeeWithFileLinkDTO() {
}
public EmployeeWithFileLinkDTO(Long id, String name, String image) {
this.id = id;
this.name = name;
this.image = image;
}
}
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import io.swagger.annotations.Api;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController("excelExportController")
@Api(tags = "excel")
@RequestMapping(value = "/excel")
public class ExcelExportController {
@GetMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) throws IOException {
// 创建数据
List<EmployeeWithFileLinkDTO> employees = new ArrayList<>();
List<EmployeeWithFileLinkDTO> employees2 = new ArrayList<>();
EmployeeWithFileLinkDTO employee1 = new EmployeeWithFileLinkDTO(1L, "张三", "a.png");
EmployeeWithFileLinkDTO employee2 = new EmployeeWithFileLinkDTO(2L, "李四", "aa.jpg");
employees.add(employee1);
employees.add(employee2);
employees2.add(employee1);
employees2.add(employee2);
int size = employees.size();
// 创建Excel导出参数
ExportParams exportParams = new ExportParams();
exportParams.setType(ExcelType.XSSF);
// 导出Excel
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EmployeeWithFileLinkDTO.class, employees);
// 在Excel中插入超链接
Sheet sheet = workbook.getSheetAt(0);
int linkColumnIndex = 2; // 指定要插入超链接的列索引(这里假设是第3列)
CreationHelper creationHelper = workbook.getCreationHelper();
for (int rowIndex = 1; rowIndex <= size; rowIndex++) {
Row row = sheet.getRow(rowIndex);
Cell cell = row.createCell(linkColumnIndex);
// 设置超链接
Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.FILE);
hyperlink.setAddress("http://192.168.1.1/" + employees2.get(rowIndex-1).getImage());
//如果是将文件和excel放在同一个文件夹里面可以用下面这个代码
//hyperlink.setAddress("./" + employees2.get(rowIndex-1).getImage());
cell.setHyperlink(hyperlink);
// 设置链接显示文本(可选)
cell.setCellValue("点击打开文件");
}
// 保存Excel文件
FileOutputStream fos = new FileOutputStream("employee_info.xlsx");
workbook.write(fos);
// 设置响应头信息
response.setHeader("Content-Disposition", "attachment;filename=employee_info.xlsx");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// 将Workbook写入响应流
workbook.write(response.getOutputStream());
fos.close();
workbook.close(); // 关闭Workbook
// 关闭文件流
response.getOutputStream().close();
}
}
导出后的效果