forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/swapanpramanick2004/tutor…
…ials into BAEL-4546
- Loading branch information
Showing
136 changed files
with
1,595 additions
and
1,078 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyleHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.baeldung.poi.excel.cellstyle; | ||
|
||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.FillPatternType; | ||
import org.apache.poi.ss.usermodel.IndexedColors; | ||
|
||
public class CellStyleHandler { | ||
|
||
public void changeCellBackgroundColor(Cell cell) { | ||
CellStyle cellStyle = cell.getCellStyle(); | ||
if(cellStyle == null) { | ||
cellStyle = cell.getSheet().getWorkbook().createCellStyle(); | ||
} | ||
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); | ||
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | ||
cell.setCellStyle(cellStyle); | ||
} | ||
|
||
public void changeCellBackgroundColorWithPattern(Cell cell) { | ||
CellStyle cellStyle = cell.getCellStyle(); | ||
if(cellStyle == null) { | ||
cellStyle = cell.getSheet().getWorkbook().createCellStyle(); | ||
} | ||
cellStyle.setFillBackgroundColor(IndexedColors.BLACK.index); | ||
cellStyle.setFillPattern(FillPatternType.BIG_SPOTS); | ||
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); | ||
cell.setCellStyle(cellStyle); | ||
} | ||
} |
Binary file not shown.
56 changes: 56 additions & 0 deletions
56
apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.poi.excel.cellstyle; | ||
|
||
import org.apache.poi.ss.usermodel.*; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class CellStyleHandlerUnitTest { | ||
private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx"; | ||
private static final int SHEET_INDEX = 0; | ||
private static final int ROW_INDEX = 0; | ||
private static final int CELL_INDEX = 0; | ||
|
||
private String fileLocation; | ||
private CellStyleHandler cellStyleHandler; | ||
|
||
@Before | ||
public void setup() throws URISyntaxException { | ||
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); | ||
cellStyleHandler = new CellStyleHandler(); | ||
} | ||
|
||
@Test | ||
public void givenWorkbookCell_whenChangeCellBackgroundColor() throws IOException { | ||
Workbook workbook = new XSSFWorkbook(fileLocation); | ||
Sheet sheet = workbook.getSheetAt(SHEET_INDEX); | ||
Row row = sheet.getRow(ROW_INDEX); | ||
Cell cell = row.getCell(CELL_INDEX); | ||
|
||
cellStyleHandler.changeCellBackgroundColor(cell); | ||
|
||
assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor()); | ||
workbook.close(); | ||
} | ||
|
||
@Test | ||
public void givenWorkbookCell_whenChangeCellBackgroundColorWithPattern() throws IOException { | ||
Workbook workbook = new XSSFWorkbook(fileLocation); | ||
Sheet sheet = workbook.getSheetAt(SHEET_INDEX); | ||
Row row = sheet.getRow(ROW_INDEX); | ||
Cell cell = row.getCell(CELL_INDEX + 1); | ||
|
||
cellStyleHandler.changeCellBackgroundColorWithPattern(cell); | ||
|
||
assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor()); | ||
workbook.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.