POI導出之Excel實現(xiàn)單元格的背景色填充問題
POI導出之Excel實現(xiàn)單元格的背景色填充
隨著業(yè)務需求的擴充,簡簡單單的Excel導出已經(jīng)不能滿足客戶的胃口了。
而POI api這個家伙里面的坑有時候真的是讓你分分鐘沒有脾氣,所以打算記錄下來,分享一下poi的坑及其解決方法。
POI導出Excel設置單元格背景色
使用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);//設置自動換行
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.設置單元格背景色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充單元格
cellStyle.setFillForegroundColor(HSSFColor.RED.index);//設置單元格背景色
//5.創(chuàng)建單元格并為單元格添加樣式對象
/*5.1在創(chuàng)建單元格前需要先創(chuàng)建行 */
HSSFRow row = sheet.createRow(0);//這里就默認創(chuàng)建第一行
HSSFCell cell = row.createCell(0);//默認創(chuàng)建第一個單元格
cell.setCellStyle(cellStyle); //設置單元格樣式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//設置單元格內(nèi)容的類型
cell.setCellValue("Hello World!");//設置單元格內(nèi)容使用自定義的背景色
使用自定義的背景色,需要額外的添加一些操作。
這里以16進制的顏色為例,演示如何從16進制的顏色一步步設為單元格的背景色。
自定義顏色方法
下述方法的作用簡單來說就是:根據(jù)傳入的HSSFPalette 畫板對象和color16進制的顏色字符串,先轉成RGB碼,然后使用HSSFPalette畫板對象和index下標重新設置對應下標的顏色,并對HSSFCellStyle單元格樣式進行顏色的設置。
強調(diào)說明:
(1)在進行顏色重新生成的時候,即如下代碼。需要注意index下標的范圍是在[8,64],設置成其他數(shù)字的下標,無效!
palette.setColorAtIndex((short)(index), (byte) r, (byte) g, (byte) b);
(2)根據(jù)下標和RGB碼重新設置完顏色后,后續(xù)需要使用的話只需要根據(jù)下標設置單元格顏色即可。
hssfCellStyle.setFillForegroundColor((short)(index));
/**
* 設置自定義顏色
* @param palette excel工作空間的繪畫板
* @param hssfCellStyle cell的單元格樣式對象
* @param color 16進制顏色字符串【如:#C1232B】
* @param index 下標,范圍在[8~64]
*/
public static void setCellColor(HSSFPalette palette,HSSFCellStyle hssfCellStyle,String color,int index){
//轉為RGB碼
int r = Integer.parseInt((color.substring(0,2)),16); //轉為16進制
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));
}關于POI這家伙,里邊還有許多的坑存在,以后遇到了會一一記錄下來。
關于自定義顏色這一塊,其實也還沒有做好,但是這里只是做一些簡單的實現(xiàn),類似顏色鎖定等問題就不闡述了,因為基本上夠用了。
POI設置Excel單元格背景色(setFillForegroundColor與setFillPattern使用)
使用Java開發(fā)信息系統(tǒng)項目,項目中往往會涉及到報表管理部分,而Excel表格首當其沖稱為最合適的選擇,但是對單元格操作時對于設置單元格的背景顏色卻很少提及,本文旨在方便單元格背景顏色設計。
操作:
至于冗長的創(chuàng)建表格表格設置的代碼相信大家都已經(jīng)了解。直接進行單元格背景顏色設計。
// 創(chuàng)建一個 workbook 對象?
Workbook workbook = new XSSFWorkbook();
?? ??? ?// 創(chuàng)建一個 sheet
?? ??? ?Sheet sheet = workbook.createSheet();
?? ??? ?//創(chuàng)建一行
?? ??? ?Row row = sheet.createRow((short) 1);
?? ??? ?ellStyle style = workbook.createCellStyle();
?? ??? ?//關鍵點 IndexedColors.AQUA.getIndex() 對應顏色
?? ??? ?style.setFillForegroundColor(***IndexedColors.AQUA.getIndex()***);
?? ??? ?style.setFillPattern(CellStyle.SOLID_FOREGROUND);
?? ??? ?Cell cell = row.createCell((short) 1);
?? ??? ?cell.setCellValue("X1");
?? ??? ?cell.setCellStyle(style);顏色與代碼參考:

上面的單元格顏色對應下面的英語顏色表示,從X1-X49 按順序對應;
將下面對應的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()
這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解Java并發(fā)工具類之CountDownLatch和CyclicBarrier
在JDK的并發(fā)包中,有幾個非常有用的并發(fā)工具類,它們分別是:CountDownLatch、CyclicBarrier、Semaphore和Exchanger,本文主要來講講其中CountDownLatch和CyclicBarrier的使用,感興趣的可以了解一下2023-06-06
SpringBoot項目中的多數(shù)據(jù)源支持的方法
本篇文章主要介紹了SpringBoot項目中的多數(shù)據(jù)源支持的方法,主要介紹在SpringBoot項目中利用SpringDataJpa技術如何支持多個數(shù)據(jù)庫的數(shù)據(jù)源,有興趣的可以了解一下2017-10-10
SpringBoot+MySQL實現(xiàn)讀寫分離的多種具體方案
在高并發(fā)和大數(shù)據(jù)量的場景下,數(shù)據(jù)庫成為了系統(tǒng)的瓶頸。為了提高數(shù)據(jù)庫的處理能力和性能,讀寫分離成為了一種常用的解決方案,本文將介紹在Spring?Boot項目中實現(xiàn)MySQL數(shù)據(jù)庫讀寫分離的多種具體方案,需要的朋友可以參考下2023-06-06

