-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1314 from egovernments/download-estimation-1578
HCMPRE-1576,1577,1578: Removed readme sheet, localised estimation sheet and added facility name
- Loading branch information
Showing
6 changed files
with
450 additions
and
42 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
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
104 changes: 104 additions & 0 deletions
104
...h-services/resource-generator/src/main/java/org/egov/processor/util/ExcelStylingUtil.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,104 @@ | ||
package org.egov.processor.util; | ||
|
||
import org.apache.poi.ss.usermodel.*; | ||
import org.apache.poi.xssf.usermodel.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.awt.Color; | ||
|
||
import static org.egov.processor.config.ServiceConstants.*; | ||
|
||
@Component | ||
public class ExcelStylingUtil { | ||
|
||
public void styleCell(Cell cell) { | ||
Sheet sheet = cell.getSheet(); | ||
Workbook workbook = sheet.getWorkbook(); | ||
XSSFWorkbook xssfWorkbook = (XSSFWorkbook) workbook; | ||
|
||
// Create a cell style | ||
XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle(); | ||
|
||
// Set background color | ||
XSSFColor backgroundColor = hexToXSSFColor(HEX_BACKGROUND_COLOR, xssfWorkbook); | ||
cellStyle.setFillForegroundColor(backgroundColor); | ||
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | ||
|
||
// Set font style (bold) | ||
XSSFFont font = (XSSFFont) workbook.createFont(); | ||
font.setBold(true); | ||
cellStyle.setFont(font); | ||
|
||
// Set alignment and wrap text | ||
cellStyle.setAlignment(HorizontalAlignment.LEFT); | ||
cellStyle.setVerticalAlignment(VerticalAlignment.TOP); | ||
cellStyle.setWrapText(true); | ||
|
||
// Lock the cell if FREEZE_CELL is true | ||
if (FREEZE_CELL) { | ||
cellStyle.setLocked(true); | ||
} | ||
|
||
// Apply the style to the cell | ||
cell.setCellStyle(cellStyle); | ||
|
||
} | ||
|
||
/** | ||
* Adjusts the column width to fit the content of the given cell, adding padding for readability. | ||
* | ||
* @param cell the cell whose column width is to be adjusted; does nothing if null. | ||
*/ | ||
public void adjustColumnWidthForCell(Cell cell) { | ||
if (cell == null) { | ||
return; | ||
} | ||
|
||
Sheet sheet = cell.getSheet(); | ||
int columnIndex = cell.getColumnIndex(); | ||
int maxWidth = sheet.getColumnWidth(columnIndex); | ||
|
||
// Calculate the width needed for the current cell content | ||
String cellValue = cell.toString(); // Convert cell content to string | ||
int cellWidth = cellValue.length() * 256; // Approximate width (1/256th of character width) | ||
|
||
// Use the maximum width seen so far, including padding for readability | ||
int padding = 512; // Adjust padding as needed | ||
int newWidth = Math.max(maxWidth, cellWidth + padding); | ||
|
||
sheet.setColumnWidth(columnIndex, newWidth); | ||
} | ||
|
||
/** | ||
* Converts a HEX color string to XSSFColor using the XSSFWorkbook context. | ||
* | ||
* @param hexColor The HEX color string (e.g., "93C47D"). | ||
* @param xssfWorkbook The XSSFWorkbook context for styles. | ||
* @return XSSFColor The corresponding XSSFColor object. | ||
*/ | ||
public static XSSFColor hexToXSSFColor(String hexColor, XSSFWorkbook xssfWorkbook) { | ||
|
||
if (hexColor == null || hexColor.length() < 6) | ||
throw new IllegalArgumentException(INVALID_HEX + hexColor); | ||
|
||
// Convert HEX to RGB | ||
int red = Integer.valueOf(hexColor.substring(0, 2), 16); | ||
int green = Integer.valueOf(hexColor.substring(2, 4), 16); | ||
int blue = Integer.valueOf(hexColor.substring(4, 6), 16); | ||
|
||
red = (int) (red * BRIGHTEN_FACTOR); // increase red component by 10% | ||
green = (int) (green * BRIGHTEN_FACTOR); // increase green component by 10% | ||
blue = (int) (blue * BRIGHTEN_FACTOR); // increase blue component by 10% | ||
|
||
// Clamp the values to be between 0 and 255 | ||
red = Math.min(255, Math.max(0, red)); | ||
green = Math.min(255, Math.max(0, green)); | ||
blue = Math.min(255, Math.max(0, blue)); | ||
|
||
// Create IndexedColorMap from the workbook's styles source | ||
IndexedColorMap colorMap = xssfWorkbook.getStylesSource().getIndexedColors(); | ||
|
||
// Create XSSFColor using the XSSFWorkbook context and colorMap | ||
return new XSSFColor(new Color(red, green, blue), colorMap); | ||
} | ||
} |
Oops, something went wrong.