poi导出excel中含有超链接并且头部样式问题

多余的代码网上都有就不多贴了 我这边的sheet来自于writer 各位看官可自行改成自己的

path是地址,name是那一列存的附件名称,i是行数 j是列数

   private void createSuperLink(ExcelWriter writer,String path,String name,int i,int j){     
        Workbook workbook = writer.getWorkbook();
        Sheet sheet = writer.getSheet();
        // 获取行
        Row row = sheet.getRow(i+1);
        if(row==null){
            row = sheet.createRow(i+1);
        }
        // 获取列
        Cell cell = row.getCell(j+6);
        if(cell==null){
            cell = row.createCell(j+6);
        }
        CreationHelper createHelper = workbook.getCreationHelper();
        CellStyle linkStyle = workbook.createCellStyle();
        Font cellFont = workbook.createFont();
        cellFont.setColor(IndexedColors.BLUE.index);
        linkStyle.setFont(cellFont);

        //底部边框
        linkStyle.setBorderTop(BorderStyle.THIN);
        linkStyle.setTopBorderColor(IndexedColors.BLACK.index);

        //底部边框
        linkStyle.setBorderBottom(BorderStyle.THIN);
        linkStyle.setBottomBorderColor(IndexedColors.BLACK.index);

        //左边框颜色
        linkStyle.setBorderRight(BorderStyle.THIN);
        linkStyle.setRightBorderColor(IndexedColors.BLACK.index);

        cell.setCellStyle(linkStyle);
        // 地址存在时才是超链接
        if(path!=null){
            Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
            link.setAddress(path);
            cell.setHyperlink(link);
        }
        cell.setCellValue(name);
}

实现效果

还有由于我的附件头部是后期合并  因为要计算附件的最大数目进行合并,导致了右边框没有边线

代码如下max为附件最大个数 其中本来我只用了获取列  结果发现设置边线失败  后面用获取尾列才ok的

    
    private void createAppendHead(ExcelWriter writer,int max){
        Sheet sheet = writer.getSheet();
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(0,0,6,max+5));
        // 获取行
        Cell headCell = sheet.getRow(0).getCell(0);
        // 获取列
        Cell targetCell = sheet.getRow(0).createCell(6);
        // 获取合并列尾
        Cell targetEndCell = sheet.getRow(0).createCell(5+max);
        // 设置样式
        targetCell.setCellStyle(headCell.getCellStyle());
        targetEndCell.setCellStyle(headCell.getCellStyle());
        // 设置内容
        targetCell.setCellValue("附件");

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章