easyexcel 添加超链接

x33g5p2x  于2021-12-28 转载在 其他  
字(4.4k)|赞(0)|评价(0)|浏览(1771)

easyexcel 添加超链接

***********************

相关类与接口

AbstractCellWriteHandler

public abstract class AbstractCellWriteHandler implements CellWriteHandler {

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
        Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
        Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
        CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
        List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }
}

CellWriteHandler

public interface CellWriteHandler extends WriteHandler {

    void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head,
        Integer columnIndex, Integer relativeRowIndex, Boolean isHead);

    void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head,
        Integer relativeRowIndex, Boolean isHead);

    void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData,
        Cell cell, Head head, Integer relativeRowIndex, Boolean isHead);

    void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
        List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead);
}

WriteHandler

public interface WriteHandler extends Handler {
}

Handler

public interface Handler {
}

Cell

public interface Cell {

    int getColumnIndex();
    int getRowIndex();

    Sheet getSheet();
    Row getRow();

    void setCellType(CellType var1);
    void setCellStyle(CellStyle var1);

    @Removal(version = "4.2")
    CellType getCellTypeEnum();

    CellStyle getCellStyle();

    void setCellValue(double var1);
    void setCellValue(RichTextString var1);
    void setCellValue(String var1);
    void setCellValue(boolean var1);
    void setCellValue(Date var1);
    void setCellValue(Calendar var1);
    void setCellErrorValue(byte var1);
    void setCellFormula(String var1) throws FormulaParseException;

    double getNumericCellValue();
    RichTextString getRichStringCellValue();
    String getStringCellValue();
    boolean getBooleanCellValue();
    Date getDateCellValue();
    byte getErrorCellValue();
    String getCellFormula();

    void setAsActiveCell();
    CellAddress getAddress();

    void setCellComment(Comment var1);
    Comment getCellComment();
    void removeCellComment();

    Hyperlink getHyperlink();
    void setHyperlink(Hyperlink var1);
    void removeHyperlink();

    CellRangeAddress getArrayFormulaRange();
    boolean isPartOfArrayFormulaGroup();

    CellType getCachedFormulaResultTypeEnum();
}

CreationHelper

public interface CreationHelper {

    RichTextString createRichTextString(String var1);
    DataFormat createDataFormat();

    Hyperlink createHyperlink(HyperlinkType var1);    //创建超链接

    FormulaEvaluator createFormulaEvaluator();

    ExtendedColor createExtendedColor();

    ClientAnchor createClientAnchor();

    AreaReference createAreaReference(String var1);
    AreaReference createAreaReference(CellReference var1, CellReference var2);
}

HyperLinkType

public enum HyperlinkType {
    @Internal
    NONE(-1),
    URL(1),
    DOCUMENT(2),
    EMAIL(3),
    FILE(4);

***********************

示例

Test

@Data
class Student{

    private Integer id;
    private String name;
    private Integer age;
}

class CustomCellWriteHandler extends AbstractCellWriteHandler{

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (cell.getRowIndex()==2&&cell.getColumnIndex()==1){
                             //第2行、第1列添加超链接(从0开始)

            CreationHelper helper=writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
            Hyperlink hyperlink=helper.createHyperlink(HyperlinkType.URL);
            hyperlink.setAddress("http://www.baidu.com");
            cell.setHyperlink(hyperlink);
        }
    }
}

public class Test8 {

    private static final String write_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"write3.xlsx";

    public static void main(String[] args){
        List<Student> list=new ArrayList<>();

        for (int i=0;i<5;i++){
            Student student=new Student();
            student.setId(i);
            student.setName("瓜田李下 "+i);
            student.setAge(20+i);

            list.add(student);
        }

        EasyExcel.write(write_path,Student.class)
                .registerWriteHandler(new CustomCellWriteHandler())
                .sheet().doWrite(list);
    }
}

使用测试

                       

点击超链接、跳转到百度首页

             

相关文章