java使用poi在excel單元格添加超鏈接設(shè)置字體顏色的方法
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)文章
Spring Boot應(yīng)用上傳文件時(shí)報(bào)錯(cuò)的原因及解決方案
這篇文章主要介紹了Spring Boot應(yīng)用上傳文件時(shí)報(bào)錯(cuò)的原因及解決方案,幫助大家更好的理解和學(xué)習(xí)使用spring boot框架,感興趣的朋友可以了解下2021-02-02詳解SpringBoot是如何整合SpringDataRedis的?
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot是如何整合SpringDataRedis展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06深入探究一下Java中不同的線程間數(shù)據(jù)通信方式
這篇文章主要來(lái)和大家一起深入探究一下Java中不同的線程間數(shù)據(jù)通信方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-04-04java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行的方法詳解
這篇文章主要介紹了java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09SpringBoot 編程式事務(wù)使用及兩種實(shí)現(xiàn)方式
編程式事務(wù)管理是通過(guò)編寫代碼來(lái)管理事務(wù),相對(duì)于聲明式事務(wù)(@Transactional注解),它提供了更細(xì)粒度的事務(wù)控制,這篇文章主要介紹了SpringBoot 編程式事務(wù)使用及兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2024-12-12聊聊BeanUtils.copyProperties和clone()方法的區(qū)別
這篇文章主要介紹了聊聊BeanUtils.copyProperties和clone()方法的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09深入理解Netty?FastThreadLocal優(yōu)缺點(diǎn)及實(shí)現(xiàn)邏輯
本文以線上詭異問(wèn)題為切入點(diǎn),通過(guò)對(duì)比JDK ThreadLocal和Netty FastThreadLocal實(shí)現(xiàn)邏輯以及優(yōu)缺點(diǎn),并深入解讀源碼,由淺入深理解Netty FastThreadLocal2023-10-10SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫(kù)分表
本文主要介紹了SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫(kù)分表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Spring Security的過(guò)濾器鏈機(jī)制
過(guò)濾器作為 ?Spring Security? 的重中之重,我們需要了解其中的機(jī)制,這樣我們才能根據(jù)業(yè)務(wù)需求的變化進(jìn)行定制,今天來(lái)探討一下 ?Spring Security? 中的過(guò)濾器鏈機(jī)制2022-08-08spring整合kaptcha驗(yàn)證碼的實(shí)現(xiàn)
這篇文章主要介紹了spring整合kaptcha驗(yàn)證碼的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05