java使用poi在excel單元格添加超鏈接設置字體顏色的方法
java使用poi在excel單元格添加超鏈接,設置字體顏色
整體描述
產(chǎn)品提的需求,將數(shù)據(jù)添加到excel中,其中有些圖片,需要添加到excel中,這塊兩種方案,一是將圖片直接寫入excel中,二是圖片和excel放在同一個目錄,excel中使用超鏈接,點擊超鏈接,打開對應的圖片。最后使用方案二,方案一poi也可以實現(xiàn),文中我也會把實現(xiàn)方式寫出來,但是方案一中由于圖片全寫到excel中,導致excel會很大,查看excel和操作不是很靈活,相比來看方案二就比較靈活,excel只存數(shù)據(jù),圖片單獨存,操作起來很方便,excel看著也很整潔。
方案描述
上述方案二,需要將excel和圖片放在同一個目錄下,使用相對路徑,這樣在拷貝操作之后,保證excel中的超鏈接還可以正常點擊,打開超鏈接的圖片。具體文件目錄結構:–整體文件夾:----excel文件----圖片文件夾:----------圖片1----------圖片2拷貝的時候,直接拷貝整體文件夾,即可實現(xiàn)在哪都能正常打開超鏈接的功能。還有個地方需要注意:超鏈接的文字需要修改下字體顏色和樣式,設置成藍色,加下劃線。要不看不出來哪個文字有超鏈接,哪個沒有。
java實現(xiàn)
1. 引入poi的maven
poi功能還是很強大的,基本能想到的功能都能通過poi實現(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單元格配置類
單元格配置類用于設置每個單元格的字體顏色,是否有超鏈接等。具體看代碼和注釋吧。
package com.psim.project.patrol.domain; import lombok.Data; /** * 用于向對應的單元格設置對應的內容 * * @author thcb */ @Data public class ExcelVo { /** * Excel表的行,Excel中的第一行為此處的第0行 */ public int row; /** * Excel表的列,Excel中的第一列為此處的第0列 */ public int column; /** * 單元格內容 */ public String content; /** * 單元格批注 */ public String comment; /** * 合并單元格參數(shù),起始行 */ public int firstRow; /** * 合并單元格參數(shù),結束行 */ public int lastRow; /** * 合并單元格參數(shù),起始列 */ public int firstCol; /** * 合并單元格參數(shù),結束列 */ 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ù)的取值如下,我沒找到具體的api描述,各個值代表什么顏色,這個是我自己試出來的,后面應該還有其他顏色,我沒往下試…
/** * Excel字體顏色 */ interface ExcelColor { /** * Excel字體顏色:黑色 */ short BLACK = 0; /** * Excel字體顏色:紅色 */ short RED = 10; /** * Excel字體顏色:綠色 */ short GREEN = 11; /** * Excel字體顏色:藍色 */ 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字體顏色:深藍 */ short DARK_BLUE = 18; /** * Excel字體顏色:深黃 */ short DARK_YELLOW = 19; }
3. 創(chuàng)建excel單元格配置對象
首先創(chuàng)建ExcelVo對象,/test/test1.jpg為超鏈接的文件路徑
excelVo = new ExcelVo(4, 2, "這是超鏈接", 4, 6, 2, 2, "/test/test1.jpg");
4. 打開excel文件workbook
File file = new File(filePath); Workbook workbook = getReportTemplate(file.getAbsolutePath()); setCell(workbook, excelVo); workbook.write(new FileOutputStream(file));
根據(jù)xls和xlsx區(qū)分打開的文件,兩個格式的創(chuàng)建方式有所區(qū)別
/** * 獲得巡檢報表 * * @param filePath 報表文件路徑 */ 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. 設置excel單元格
中間創(chuàng)建excel的步驟略過了,直接調用此方法設置單元格具體樣式即可。其中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就能實現(xiàn)保持原有樣式 cellStyle.cloneStyleFrom(cell.getCellStyle()); // 設置單元格字體顏色 if (excelVo.frontColor != 0) { Font font = workbook.createFont(); font.setColor(excelVo.frontColor); cellStyle.setFont(font); } if (StringUtils.isNotEmpty(excelVo.hyperLink)) { // 設置超鏈接 CreationHelper createHelper = workbook.getCreationHelper(); Hyperlink link = createHelper.createHyperlink(HyperlinkType.FILE); link.setAddress(excelVo.hyperLink); cell.setHyperlink(link); // 設置字體 Font font = workbook.createFont(); font.setColor(ExcelColor.BLUE); font.setUnderline((byte) 1); cellStyle.setFont(font); } // 設置單元格顯示內容 cell.setCellValue(excelVo.content); // 設置單元格樣式 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)建和對單元格賦值 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ù),需要創(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. 總結
僅寫出了關鍵性的代碼,需要注意的是合并的時候,一個單元格只能有一個合并的操作,如果合并一個已經(jīng)合并過的單元格,程序會報錯。
到此這篇關于java使用poi在excel單元格添加超鏈接,設置字體顏色的文章就介紹到這了,更多相關java poi添加超鏈接內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解SpringBoot是如何整合SpringDataRedis的?
今天給大家?guī)淼氖顷P于Java的相關知識,文章圍繞著SpringBoot是如何整合SpringDataRedis展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06深入探究一下Java中不同的線程間數(shù)據(jù)通信方式
這篇文章主要來和大家一起深入探究一下Java中不同的線程間數(shù)據(jù)通信方式,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-04-04java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行的方法詳解
這篇文章主要介紹了java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-09-09SpringBoot 編程式事務使用及兩種實現(xiàn)方式
編程式事務管理是通過編寫代碼來管理事務,相對于聲明式事務(@Transactional注解),它提供了更細粒度的事務控制,這篇文章主要介紹了SpringBoot 編程式事務使用及兩種實現(xiàn)方式,需要的朋友可以參考下2024-12-12聊聊BeanUtils.copyProperties和clone()方法的區(qū)別
這篇文章主要介紹了聊聊BeanUtils.copyProperties和clone()方法的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09深入理解Netty?FastThreadLocal優(yōu)缺點及實現(xiàn)邏輯
本文以線上詭異問題為切入點,通過對比JDK ThreadLocal和Netty FastThreadLocal實現(xiàn)邏輯以及優(yōu)缺點,并深入解讀源碼,由淺入深理解Netty FastThreadLocal2023-10-10SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表
本文主要介紹了SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03