当前位置:   article > 正文

java使用easypoi导出excel文件,并支持在excel文件里面插入文件_easyexcel hyperlinktype.file

easyexcel hyperlinktype.file

引入依赖

compile group: 'cn.afterturn',name:'easypoi-spring-boot-starter',version:'4.1.0'
  1. @Data
  2. public class EmployeeWithFileLinkDTO {
  3. @Excel(name = "员工ID", width = 15)
  4. private Long id;
  5. @Excel(name = "员工姓名", width = 15)
  6. private String name;
  7. @Excel(name = "图片")
  8. private String image;
  9. public EmployeeWithFileLinkDTO() {
  10. }
  11. public EmployeeWithFileLinkDTO(Long id, String name, String image) {
  12. this.id = id;
  13. this.name = name;
  14. this.image = image;
  15. }
  16. }
  1. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  2. import cn.afterturn.easypoi.excel.entity.ExportParams;
  3. import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
  4. import io.swagger.annotations.Api;
  5. import org.apache.poi.common.usermodel.HyperlinkType;
  6. import org.apache.poi.ss.usermodel.*;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. @RestController("excelExportController")
  16. @Api(tags = "excel")
  17. @RequestMapping(value = "/excel")
  18. public class ExcelExportController {
  19. @GetMapping("/exportExcel")
  20. public void exportExcel(HttpServletResponse response) throws IOException {
  21. // 创建数据
  22. List<EmployeeWithFileLinkDTO> employees = new ArrayList<>();
  23. List<EmployeeWithFileLinkDTO> employees2 = new ArrayList<>();
  24. EmployeeWithFileLinkDTO employee1 = new EmployeeWithFileLinkDTO(1L, "张三", "a.png");
  25. EmployeeWithFileLinkDTO employee2 = new EmployeeWithFileLinkDTO(2L, "李四", "aa.jpg");
  26. employees.add(employee1);
  27. employees.add(employee2);
  28. employees2.add(employee1);
  29. employees2.add(employee2);
  30. int size = employees.size();
  31. // 创建Excel导出参数
  32. ExportParams exportParams = new ExportParams();
  33. exportParams.setType(ExcelType.XSSF);
  34. // 导出Excel
  35. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EmployeeWithFileLinkDTO.class, employees);
  36. // 在Excel中插入超链接
  37. Sheet sheet = workbook.getSheetAt(0);
  38. int linkColumnIndex = 2; // 指定要插入超链接的列索引(这里假设是第3列)
  39. CreationHelper creationHelper = workbook.getCreationHelper();
  40. for (int rowIndex = 1; rowIndex <= size; rowIndex++) {
  41. Row row = sheet.getRow(rowIndex);
  42. Cell cell = row.createCell(linkColumnIndex);
  43. // 设置超链接
  44. Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.FILE);
  45. hyperlink.setAddress("http://192.168.1.1/" + employees2.get(rowIndex-1).getImage());
  46. //如果是将文件和excel放在同一个文件夹里面可以用下面这个代码
  47. //hyperlink.setAddress("./" + employees2.get(rowIndex-1).getImage());
  48. cell.setHyperlink(hyperlink);
  49. // 设置链接显示文本(可选)
  50. cell.setCellValue("点击打开文件");
  51. }
  52. // 保存Excel文件
  53. FileOutputStream fos = new FileOutputStream("employee_info.xlsx");
  54. workbook.write(fos);
  55. // 设置响应头信息
  56. response.setHeader("Content-Disposition", "attachment;filename=employee_info.xlsx");
  57. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  58. // 将Workbook写入响应流
  59. workbook.write(response.getOutputStream());
  60. fos.close();
  61. workbook.close(); // 关闭Workbook
  62. // 关闭文件流
  63. response.getOutputStream().close();
  64. }
  65. }

导出后的效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/540289
推荐阅读
相关标签
  

闽ICP备14008679号