Easy Poi 导出文字带可点击的附件链接

展示效果

在这里插入图片描述

依赖

       <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>

导出

	@RequestMapping(value = "/exportData", method = RequestMethod.GET, produces = "application/octet-stream")
    @ApiOperation(value = "导出", httpMethod = "GET")
    public void exportData(HttpServletResponse response) {
    	String excelName= "yyyyy";
    	String sheetNameName = "xxxx";
        String[] headers = new String[]{"列1", "列2", "附件", "列3", "列4", "列5"};  
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet(sheetNameName);

		// 这个记录行号
        AtomicInteger ai = new AtomicInteger();
        Row row = sheet.createRow(ai.getAndIncrement());
        
        // 表头填充
        AtomicInteger aj = new AtomicInteger();
        for (String header : headers) {
            Cell cell = row.createCell(aj.getAndIncrement());
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            Font font = wb.createFont();
            font.setBold(true);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(header);
        }
        
		// 业务数据
		// List<TZpjh> list = targetMapper.list(wrapper);
		// 填充
        list.forEach(tZpjh -> {
            Row row1 = sheet.createRow(ai.getAndIncrement());
            row1.createCell(0).setCellValue(tZpjh.getxx());// 列1
            row1.createCell(1).setCellValue(tZpjh.getxx());// 列2
            row1.createCell(3).setCellValue(tZpjh.getxx());// 列3
            row1.createCell(4).setCellValue(tZpjh.getxx());// 列4
            row1.createCell(5).setCellValue(tZpjh.getxx());// 列5


			 // 附件
            Cell cell2 = row1.createCell(2);
			// 附件地址:String url ="http://xxxxx/file/upload/1689850956010.xls";
            String url = tZpjh.getFj();
            if (ObjectUtil.isNotEmpty(url)) {
            	String fileName = tZpjh.getFjName();
                url = filePrefix.concat(url);
                // 生成的超链接不带蓝色下划线样式
                String formula = MessageFormat.format("HYPERLINK(\"{0}\",\"{1}\")", url, fileName);
                cell2.setCellFormula(formula);

                CellStyle cellStyle = wb.createCellStyle();
                // 不直接使用getCellStyle(),用cloneStyleFrom就能实现保持原有样式
                cellStyle.cloneStyleFrom(cell2.getCellStyle()); 
                // 设置字体
                Font font = wb.createFont();
                font.setColor(IndexedColors.BLUE.getIndex());
                font.setUnderline((byte) 1);
                cellStyle.setFont(font);
                cell2.setCellStyle(cellStyle);
            }

        });
        wb.getSheet(sheetNameName).createFreezePane(0, 1, 0, 1);
        ExcelUtils.buildExcelDocument(excelName, wb, response);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值