POI導(dǎo)出之Excel實(shí)現(xiàn)單元格的背景色填充問題
POI導(dǎo)出之Excel實(shí)現(xiàn)單元格的背景色填充
隨著業(yè)務(wù)需求的擴(kuò)充,簡簡單單的Excel導(dǎo)出已經(jīng)不能滿足客戶的胃口了。
而POI api這個(gè)家伙里面的坑有時(shí)候真的是讓你分分鐘沒有脾氣,所以打算記錄下來,分享一下poi的坑及其解決方法。
POI導(dǎo)出Excel設(shè)置單元格背景色
使用poi提供的背景色
//1.獲取Excel工作簿對象 HSSFWorkbook wb = new HSSFWorkbook(); //2.獲取sheet對象 HSSFSheet sheet = wb.createSheet("sheet名"); //2.創(chuàng)建單元格樣式對象 HSSFCellStyle cellStyle = wb.createCellStyle(); //3.添加常用樣式 cellStyle.setWrapText(true);//設(shè)置自動(dòng)換行 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中顯示 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框 //4.設(shè)置單元格背景色 cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充單元格 cellStyle.setFillForegroundColor(HSSFColor.RED.index);//設(shè)置單元格背景色 //5.創(chuàng)建單元格并為單元格添加樣式對象 /*5.1在創(chuàng)建單元格前需要先創(chuàng)建行 */ HSSFRow row = sheet.createRow(0);//這里就默認(rèn)創(chuàng)建第一行 HSSFCell cell = row.createCell(0);//默認(rèn)創(chuàng)建第一個(gè)單元格 cell.setCellStyle(cellStyle); //設(shè)置單元格樣式 cell.setCellType(HSSFCell.CELL_TYPE_STRING);//設(shè)置單元格內(nèi)容的類型 cell.setCellValue("Hello World!");//設(shè)置單元格內(nèi)容
使用自定義的背景色
使用自定義的背景色,需要額外的添加一些操作。
這里以16進(jìn)制的顏色為例,演示如何從16進(jìn)制的顏色一步步設(shè)為單元格的背景色。
自定義顏色方法
下述方法的作用簡單來說就是:根據(jù)傳入的HSSFPalette 畫板對象和color16進(jìn)制的顏色字符串,先轉(zhuǎn)成RGB碼,然后使用HSSFPalette畫板對象和index下標(biāo)重新設(shè)置對應(yīng)下標(biāo)的顏色,并對HSSFCellStyle單元格樣式進(jìn)行顏色的設(shè)置。
強(qiáng)調(diào)說明:
(1)在進(jìn)行顏色重新生成的時(shí)候,即如下代碼。需要注意index下標(biāo)的范圍是在[8,64],設(shè)置成其他數(shù)字的下標(biāo),無效!
palette.setColorAtIndex((short)(index), (byte) r, (byte) g, (byte) b);
(2)根據(jù)下標(biāo)和RGB碼重新設(shè)置完顏色后,后續(xù)需要使用的話只需要根據(jù)下標(biāo)設(shè)置單元格顏色即可。
hssfCellStyle.setFillForegroundColor((short)(index));
/** * 設(shè)置自定義顏色 * @param palette excel工作空間的繪畫板 * @param hssfCellStyle cell的單元格樣式對象 * @param color 16進(jìn)制顏色字符串【如:#C1232B】 * @param index 下標(biāo),范圍在[8~64] */ public static void setCellColor(HSSFPalette palette,HSSFCellStyle hssfCellStyle,String color,int index){ //轉(zhuǎn)為RGB碼 int r = Integer.parseInt((color.substring(0,2)),16); //轉(zhuǎn)為16進(jìn)制 int g = Integer.parseInt((color.substring(2,4)),16); int b = Integer.parseInt((color.substring(4,6)),16); //這里index是索引 palette.setColorAtIndex((short)(index), (byte) r, (byte) g, (byte) b); hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); hssfCellStyle.setFillForegroundColor((short)(index)); }
關(guān)于POI這家伙,里邊還有許多的坑存在,以后遇到了會一一記錄下來。
關(guān)于自定義顏色這一塊,其實(shí)也還沒有做好,但是這里只是做一些簡單的實(shí)現(xiàn),類似顏色鎖定等問題就不闡述了,因?yàn)榛旧蠅蛴昧恕?/p>
POI設(shè)置Excel單元格背景色(setFillForegroundColor與setFillPattern使用)
使用Java開發(fā)信息系統(tǒng)項(xiàng)目,項(xiàng)目中往往會涉及到報(bào)表管理部分,而Excel表格首當(dāng)其沖稱為最合適的選擇,但是對單元格操作時(shí)對于設(shè)置單元格的背景顏色卻很少提及,本文旨在方便單元格背景顏色設(shè)計(jì)。
操作:
至于冗長的創(chuàng)建表格表格設(shè)置的代碼相信大家都已經(jīng)了解。直接進(jìn)行單元格背景顏色設(shè)計(jì)。
// 創(chuàng)建一個(gè) workbook 對象? Workbook workbook = new XSSFWorkbook(); ?? ??? ?// 創(chuàng)建一個(gè) sheet ?? ??? ?Sheet sheet = workbook.createSheet(); ?? ??? ?//創(chuàng)建一行 ?? ??? ?Row row = sheet.createRow((short) 1); ?? ??? ?ellStyle style = workbook.createCellStyle(); ?? ??? ?//關(guān)鍵點(diǎn) IndexedColors.AQUA.getIndex() 對應(yīng)顏色 ?? ??? ?style.setFillForegroundColor(***IndexedColors.AQUA.getIndex()***); ?? ??? ?style.setFillPattern(CellStyle.SOLID_FOREGROUND); ?? ??? ?Cell cell = row.createCell((short) 1); ?? ??? ?cell.setCellValue("X1"); ?? ??? ?cell.setCellStyle(style);
顏色與代碼參考:
上面的單元格顏色對應(yīng)下面的英語顏色表示,從X1-X49 按順序?qū)?yīng);
將下面對應(yīng)的code填入上述代碼加粗斜體位置即可。
IndexedColors.AQUA.getIndex() IndexedColors.AUTOMATIC.getIndex() IndexedColors.BLUE.getIndex() IndexedColors.BLUE_GREY.getIndex() IndexedColors.BRIGHT_GREEN.getIndex() IndexedColors.BROWN.getIndex() IndexedColors.CORAL.getIndex() IndexedColors.CORNFLOWER_BLUE.getIndex() IndexedColors.DARK_BLUE.getIndex() IndexedColors.DARK_GREEN.getIndex() IndexedColors.DARK_RED.getIndex() IndexedColors.DARK_TEAL.getIndex() IndexedColors.DARK_YELLOW.getIndex() IndexedColors.GOLD.getIndex() IndexedColors.GREEN.getIndex() IndexedColors.GREY_25_PERCENT.getIndex() IndexedColors.GREY_40_PERCENT.getIndex() IndexedColors.GREY_50_PERCENT.getIndex() IndexedColors.GREY_80_PERCENT.getIndex() IndexedColors.INDIGO.getIndex() IndexedColors.LAVENDER.getIndex() IndexedColors.LEMON_CHIFFON.getIndex() IndexedColors.LIGHT_BLUE.getIndex() IndexedColors.LEMON_CHIFFON.getIndex() IndexedColors.LIGHT_BLUE.getIndex() IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex() IndexedColors.LIGHT_GREEN.getIndex() IndexedColors.LIGHT_ORANGE.getIndex() IndexedColors.LIGHT_TURQUOISE.getIndex() IndexedColors.LIGHT_YELLOW.getIndex() IndexedColors.LIME.getIndex() IndexedColors.MAROON.getIndex() IndexedColors.OLIVE_GREEN.getIndex() IndexedColors.ORANGE.getIndex() IndexedColors.ORCHID.getIndex() IndexedColors.PALE_BLUE.getIndex() IndexedColors.PINK.getIndex() IndexedColors.PLUM.getIndex() IndexedColors.RED.getIndex() IndexedColors.ROSE.getIndex() IndexedColors.ROYAL_BLUE.getIndex() IndexedColors.SEA_GREEN.getIndex() IndexedColors.SKY_BLUE.getIndex() IndexedColors.TAN.getIndex() IndexedColors.TEAL.getIndex() IndexedColors.TURQUOISE.getIndex() IndexedColors.VIOLET.getIndex() IndexedColors.WHITE.getIndex() IndexedColors.YELLOW.getIndex()
這些僅為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot中JdbcTemplate的事務(wù)控制
JdbcTemplate是spring-jdbc提供的數(shù)據(jù)庫核心操作類,那對JdbcTemplate進(jìn)行事務(wù)控制呢,本文就詳細(xì)的介紹一下2021-09-09springboot整合企微webhook機(jī)器人發(fā)送消息提醒
這篇文章主要為大家介紹了springboot整合企微webhook機(jī)器人發(fā)送消息提醒,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Maven中pom.xml文件報(bào)錯(cuò)的原因解決
創(chuàng)建Maven項(xiàng)目的時(shí)候,如果你選擇的Packaging為war,那么就會報(bào)錯(cuò),本文主要介紹了Maven中pom.xml文件報(bào)錯(cuò)的原因解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java報(bào)錯(cuò)sun.misc.Unsafe.park(Native Method)問題
這篇文章主要介紹了Java報(bào)錯(cuò)sun.misc.Unsafe.park(Native Method)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟
這篇文章主要介紹了Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟的相關(guān)資料,需要的朋友可以參考下2016-10-10使用springmvc運(yùn)行流程分析,手寫spring框架嘗試
這篇文章主要介紹了使用springmvc運(yùn)行流程分析,手寫spring框架嘗試,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10java實(shí)現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)據(jù)庫的數(shù)據(jù)寫入到txt的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07struts2 validation.xml 驗(yàn)證規(guī)則代碼解析
這篇文章主要介紹了struts2 validation.xml 驗(yàn)證規(guī)則代碼解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01