欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用poi在excel單元格添加超鏈接設(shè)置字體顏色的方法

 更新時(shí)間:2023年09月21日 10:00:32   作者:天河歸來(lái)  
這篇文章主要介紹了java使用poi在excel單元格添加超鏈接,設(shè)置字體顏色,poi功能還是很強(qiáng)大的,基本能想到的功能都能通過(guò)poi實(shí)現(xiàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

java使用poi在excel單元格添加超鏈接,設(shè)置字體顏色

整體描述

產(chǎn)品提的需求,將數(shù)據(jù)添加到excel中,其中有些圖片,需要添加到excel中,這塊兩種方案,一是將圖片直接寫入excel中,二是圖片和excel放在同一個(gè)目錄,excel中使用超鏈接,點(diǎn)擊超鏈接,打開(kāi)對(duì)應(yīng)的圖片。最后使用方案二,方案一poi也可以實(shí)現(xiàn),文中我也會(huì)把實(shí)現(xiàn)方式寫出來(lái),但是方案一中由于圖片全寫到excel中,導(dǎo)致excel會(huì)很大,查看excel和操作不是很靈活,相比來(lái)看方案二就比較靈活,excel只存數(shù)據(jù),圖片單獨(dú)存,操作起來(lái)很方便,excel看著也很整潔。

方案描述

上述方案二,需要將excel和圖片放在同一個(gè)目錄下,使用相對(duì)路徑,這樣在拷貝操作之后,保證excel中的超鏈接還可以正常點(diǎn)擊,打開(kāi)超鏈接的圖片。具體文件目錄結(jié)構(gòu):–整體文件夾:----excel文件----圖片文件夾:----------圖片1----------圖片2拷貝的時(shí)候,直接拷貝整體文件夾,即可實(shí)現(xiàn)在哪都能正常打開(kāi)超鏈接的功能。還有個(gè)地方需要注意:超鏈接的文字需要修改下字體顏色和樣式,設(shè)置成藍(lán)色,加下劃線。要不看不出來(lái)哪個(gè)文字有超鏈接,哪個(gè)沒(méi)有。

java實(shí)現(xiàn)

1. 引入poi的maven

poi功能還是很強(qiáng)大的,基本能想到的功能都能通過(guò)poi實(shí)現(xiàn)。

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.1</version>

2. 創(chuàng)建excel單元格配置類

單元格配置類用于設(shè)置每個(gè)單元格的字體顏色,是否有超鏈接等。具體看代碼和注釋吧。

package com.psim.project.patrol.domain;
import lombok.Data;
/**
 * 用于向?qū)?yīng)的單元格設(shè)置對(duì)應(yīng)的內(nèi)容
 *
 * @author thcb
 */
@Data
public class ExcelVo {
    /**
     * Excel表的行,Excel中的第一行為此處的第0行
     */
    public int row;
    /**
     * Excel表的列,Excel中的第一列為此處的第0列
     */
    public int column;
    /**
     * 單元格內(nèi)容
     */
    public String content;
    /**
     * 單元格批注
     */
    public String comment;
    /**
     * 合并單元格參數(shù),起始行
     */
    public int firstRow;
    /**
     * 合并單元格參數(shù),結(jié)束行
     */
    public int lastRow;
    /**
     * 合并單元格參數(shù),起始列
     */
    public int firstCol;
    /**
     * 合并單元格參數(shù),結(jié)束列
     */
    public int lastCol;
    /**
     * 字體顏色
     */
    public short frontColor;
    /**
     * 超連接
     */
    public String hyperLink = "";
    public ExcelVo(int row, int column, String content) {
        this.row = row;
        this.column = column;
        this.content = content;
        this.comment = "";
        this.firstRow = 0;
        this.lastRow = 0;
        this.firstCol = 0;
        this.lastCol = 0;
        this.frontColor = 0;
        this.hyperLink = "";
    }
    public ExcelVo(int row, int column, String content, short frontColor) {
        this.row = row;
        this.column = column;
        this.content = content;
        this.firstRow = 0;
        this.lastRow = 0;
        this.firstCol = 0;
        this.lastCol = 0;
        this.frontColor = frontColor;
        this.hyperLink = "";
    }
    public ExcelVo(int row, int column, String content, int firstRow, int lastRow, int firstCol, int lastCol) {
        this.row = row;
        this.column = column;
        this.content = content;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
        this.firstCol = firstCol;
        this.lastCol = lastCol;
        this.frontColor = 0;
        this.hyperLink = "";
    }
    public ExcelVo(int row, int column, String content, int firstRow, int lastRow, int firstCol, int lastCol, String hyperLink) {
        this.row = row;
        this.column = column;
        this.content = content;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
        this.firstCol = firstCol;
        this.lastCol = lastCol;
        this.frontColor = 0;
        this.hyperLink = hyperLink;
    }
    public ExcelVo(int row, int column, String content, int firstRow, int lastRow, int firstCol, int lastCol, short frontColor) {
        this.row = row;
        this.column = column;
        this.content = content;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
        this.firstCol = firstCol;
        this.lastCol = lastCol;
        this.frontColor = frontColor;
        this.hyperLink = "";
    }
}

其中字體顏色(frontColor)參數(shù)的取值如下,我沒(méi)找到具體的api描述,各個(gè)值代表什么顏色,這個(gè)是我自己試出來(lái)的,后面應(yīng)該還有其他顏色,我沒(méi)往下試…

    /**
     * Excel字體顏色
     */
    interface ExcelColor {
        /**
         * Excel字體顏色:黑色
         */
        short BLACK = 0;
        /**
         * Excel字體顏色:紅色
         */
        short RED = 10;
        /**
         * Excel字體顏色:綠色
         */
        short GREEN = 11;
        /**
         * Excel字體顏色:藍(lán)色
         */
        short BLUE = 12;
        /**
         * Excel字體顏色:黃色
         */
        short YELLOW = 13;
        /**
         * Excel字體顏色:紫色
         */
        short PURPLE = 14;
        /**
         * Excel字體顏色:青色
         */
        short CYAN = 15;
        /**
         * Excel字體顏色:棕色
         */
        short BROWN = 16;
        /**
         * Excel字體顏色:深綠
         */
        short DARK_GREEN = 17;
        /**
         * Excel字體顏色:深藍(lán)
         */
        short DARK_BLUE = 18;
        /**
         * Excel字體顏色:深黃
         */
        short DARK_YELLOW = 19;
    }

3. 創(chuàng)建excel單元格配置對(duì)象

首先創(chuàng)建ExcelVo對(duì)象,/test/test1.jpg為超鏈接的文件路徑

excelVo = new ExcelVo(4, 2, "這是超鏈接", 4, 6, 2, 2, "/test/test1.jpg");

4. 打開(kāi)excel文件workbook

File file = new File(filePath);
Workbook workbook = getReportTemplate(file.getAbsolutePath());
setCell(workbook, excelVo);
workbook.write(new FileOutputStream(file));

根據(jù)xls和xlsx區(qū)分打開(kāi)的文件,兩個(gè)格式的創(chuàng)建方式有所區(qū)別

/**
     * 獲得巡檢報(bào)表
     *
     * @param filePath 報(bào)表文件路徑
     */
    public Workbook getReportTemplate(String filePath) {
        Workbook workbook;
        FileInputStream fileInputStream;
        FileOutputStream out = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            if (filePath.endsWith(".xlsx")) {
                FileInputStream fileSystem = new FileInputStream(filePath);
                workbook = new XSSFWorkbook(fileSystem);
                fileSystem.close();
                return workbook;
            }
            if (filePath.endsWith(".xls")) {
                POIFSFileSystem fileSystem = new POIFSFileSystem(fileInputStream);
                workbook = new HSSFWorkbook(fileSystem);
                return workbook;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

5. 設(shè)置excel單元格

中間創(chuàng)建excel的步驟略過(guò)了,直接調(diào)用此方法設(shè)置單元格具體樣式即可。其中workbook就是生成的excel

/**
     * 根據(jù)ExcelVo修改Excel中的指定單元格的值
     *
     * @param ExcelVo Excel單元格
     */
    public void setCell(Workbook workbook, ExcelVo excelVo) {
        Cell cell = createCell(workbook, polRecordTemplateVo);
        if (cell != null) {
            // 單元格樣式
            CellStyle cellStyle = workbook.createCellStyle();
            //不直接使用getCellStyle(),用cloneStyleFrom就能實(shí)現(xiàn)保持原有樣式
            cellStyle.cloneStyleFrom(cell.getCellStyle());
            // 設(shè)置單元格字體顏色
            if (excelVo.frontColor != 0) {
                Font font = workbook.createFont();
                font.setColor(excelVo.frontColor);
                cellStyle.setFont(font);
            }
            if (StringUtils.isNotEmpty(excelVo.hyperLink)) {
                // 設(shè)置超鏈接
                CreationHelper createHelper = workbook.getCreationHelper();
                Hyperlink link = createHelper.createHyperlink(HyperlinkType.FILE);
                link.setAddress(excelVo.hyperLink);
                cell.setHyperlink(link);
                // 設(shè)置字體
                Font font = workbook.createFont();
                font.setColor(ExcelColor.BLUE);
                font.setUnderline((byte) 1);
                cellStyle.setFont(font);
            }
            // 設(shè)置單元格顯示內(nèi)容
            cell.setCellValue(excelVo.content);
            // 設(shè)置單元格樣式
            cell.setCellStyle(cellStyle);
        }
    }

創(chuàng)建單元格的方法

   /**
     * 創(chuàng)建單元格excelVo
     */
    public Cell createCell(Workbook workbook, ExcelVo excelVo) {
        if (workbook != null) {
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet != null && excelVo != null) {
                Cell cell = null;
                if (excelVo.getFirstCol() == 0
                        && excelVo.getLastCol() == 0
                        && excelVo.getFirstRow() == 0
                        && excelVo.getLastRow() == 0) {
                    // 合并參數(shù)都為0,正常創(chuàng)建和對(duì)單元格賦值
                    if (sheet.getRow(excelVo.row) != null) {
                        Row row = sheet.getRow(excelVo.row);
                        row.setHeightInPoints(PolPatrolConstants.ROW_HEIGHT);
                        if (row.getCell(excelVo.column) != null) {
                            cell = row.getCell(excelVo.column);
                        } else {
                            cell = row.createCell(excelVo.column);
                        }
                    } else {
                        Row row = sheet.createRow(excelVo.row);
                        row.setHeightInPoints(PolPatrolConstants.ROW_HEIGHT);
                        if (row.getCell(excelVo.column) != null) {
                            cell = row.getCell(excelVo.column);
                        } else {
                            cell = row.createCell(excelVo.column);
                        }
                    }
                } else {
                    // 有不為0的合并參數(shù),需要?jiǎng)?chuàng)建合并單元格
                    CellRangeAddress cellRangeAddress = new CellRangeAddress(
                            excelVo.firstRow,
                            excelVo.lastRow,
                            excelVo.firstCol,
                            excelVo.lastCol);
                    sheet.addMergedRegion(cellRangeAddress);
                    if (sheet.getRow(excelVo.row) != null) {
                        Row row = sheet.getRow(excelVo.row);
                        row.setHeightInPoints(PolPatrolConstants.ROW_HEIGHT);
                        if (row.getCell(excelVo.column) != null) {
                            cell = row.getCell(excelVo.column);
                        } else {
                            cell = row.createCell(excelVo.column);
                        }
                    } else {
                        Row row = sheet.createRow(excelVo.row);
                        row.setHeightInPoints(PolPatrolConstants.ROW_HEIGHT);
                        if (row.getCell(excelVo.column) != null) {
                            cell = row.getCell(excelVo.column);
                        } else {
                            cell = row.createCell(excelVo.column);
                        }
                    }
                }
                return cell;
            } else {
                log.error("setCell:shell is null");
            }
        } else {
            log.error("workbook:workbook is null");
        }
        return null;
    }

6. 總結(jié)

僅寫出了關(guān)鍵性的代碼,需要注意的是合并的時(shí)候,一個(gè)單元格只能有一個(gè)合并的操作,如果合并一個(gè)已經(jīng)合并過(guò)的單元格,程序會(huì)報(bào)錯(cuò)。

到此這篇關(guān)于java使用poi在excel單元格添加超鏈接,設(shè)置字體顏色的文章就介紹到這了,更多相關(guān)java poi添加超鏈接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論