美文网首页
设置超链接

设置超链接

作者: 刘小刀tina | 来源:发表于2024-01-06 18:54 被阅读0次
    package org.example.handler;
    
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    import cn.hutool.core.collection.CollUtil;
    import org.apache.poi.common.usermodel.HyperlinkType;
    import org.apache.poi.ss.usermodel.*;
    import org.example.CacheMap;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.alibaba.excel.metadata.CellData;
    import com.alibaba.excel.metadata.Head;
    import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
    import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
    import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
    
    public class CustomCellWriteHandler extends AbstractCellWriteHandler {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(CustomCellWriteHandler.class);
    
        @Override
        public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                     List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
            // 这里可以对cell进行任何操作
            LOGGER.info("fieldName => {}",head.getFieldName());
            LOGGER.info("relativeRowIndex => {}",relativeRowIndex);
            LOGGER.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
    //        if (isHead && cell.getColumnIndex() == 0) { // 对第一行第一列的头超链接
    //            CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
    //            Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL);
    //            hyperlink.setAddress("https://github.com/alibaba/easyexcel");
    //            cell.setHyperlink(hyperlink);
    //        }
            Set<String> cache = CacheMap.getCache();
            if(CollUtil.isEmpty(cache)){
                LOGGER.warn("cache is empty....");
                return;
            }
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            String fieldName = head.getFieldName();
            if (!isHead && cache.contains(fieldName)){ // 头为 filePath 的列设置附件相对地址
                CreationHelper createHelper = workbook.getCreationHelper();
                Font font = workbook.createFont();
                CellStyle cellStyle = workbook.createCellStyle();
                Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.FILE); // 类型设置为 FILE
                String url = "https://lmg.jj20.com/up/allimg/tp10/22022312542M617-0-lp.jpg";
                hyperlink.setAddress(url);  // 附件相对地址,必须使用反斜杠
                cell.setHyperlink(hyperlink);
                font.setColor(IndexedColors.BLUE.getIndex());
                cellStyle.setFont(font);
                cell.setCellStyle(cellStyle);
            }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:设置超链接

          本文链接:https://www.haomeiwen.com/subject/yqbwndtx.html