poi导出excel中附件超链接

记录一下 参考了一些博客

https://blog.csdn.net/oTianKongLan123/article/details/106903234
https://blog.csdn.net/syzajax/article/details/109649305

            int beginSuperLinkIndex=cellIndex + 5;
            if(CollUtil.isNotEmpty(unitFillFileList)){
                for (int i = 0; i < unitFillFileList.size(); i++) {
                    if(ObjectUtil.isNull(row.getCell(beginSuperLinkIndex+i))){
                        row.createCell(beginSuperLinkIndex+i);
                    }
                    //处理文件超链接
                    CreationHelper creationHelper = book.getCreationHelper();
                    //指定超链接的类型为url
                    Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
                    //指定超链接的 目标地址  例如 :http://www.baidu.com
                    hyperlink.setAddress(unitFillFileList.get(i).getFileUrl());
                    row.getCell(beginSuperLinkIndex+i).setHyperlink(hyperlink);
                    //设置单元格字体样式
                    row.getCell(beginSuperLinkIndex+i).setCellStyle(createHyperlinkStyle(book));
                    //为超链接设置要显示的值  不设置 导出的单元格为空
                    row.getCell(beginSuperLinkIndex+i).setCellValue(unitFillFileList.get(i).getFileName());
                }
            }
    private CellStyle createHyperlinkStyle(HSSFWorkbook hssfWorkbook){
        CellStyle hLinkStyle = hssfWorkbook.createCellStyle();
        Font hLinkFont = hssfWorkbook.createFont();
        hLinkFont.setUnderline(Font.U_SINGLE);
        hLinkFont.setColor(IndexedColors.BLUE.getIndex());
        hLinkStyle.setFont(hLinkFont);

        //顶部边框
        hLinkStyle.setBorderTop(BorderStyle.THIN);
        hLinkStyle.setTopBorderColor(IndexedColors.BLACK.index);
        //底部边框
        hLinkStyle.setBorderBottom(BorderStyle.THIN);
        hLinkStyle.setBottomBorderColor(IndexedColors.BLACK.index);
        //右边框颜色
        hLinkStyle.setBorderRight(BorderStyle.THIN);
        hLinkStyle.setRightBorderColor(IndexedColors.BLACK.index);
        return hLinkStyle;
    }

poi导出excel中附件超链接_第1张图片
最终效果

你可能感兴趣的:(java,java)