Skip to content

Commit b4ba54e

Browse files
committed
Fix wrong excel cellStyle background color value using IndexedColor
1 parent 8d86329 commit b4ba54e

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/main/java/root/core/usecase/implement/DBCheckUsecaseImpl.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package root.core.usecase.implement;
22

3+
import java.io.File;
34
import java.io.FileInputStream;
4-
import java.io.FileNotFoundException;
55
import java.io.FileOutputStream;
66
import java.io.IOException;
7-
import java.io.InputStream;
87
import java.io.OutputStream;
98

109
import org.apache.poi.ss.usermodel.Sheet;
@@ -104,20 +103,17 @@ public void writeExcelArchiveUsageCheck() throws Exception {
104103
rowIndex = 29;
105104
}
106105

107-
String filePath = "C:\\Users\\aserv\\Documents\\WorkSpace_DBMonitoring_Quartz\\DBMonitoring\\report\\";
106+
String filePath = "./report/";
108107
String fileName = "DB관리대장_종합_" + year + "." + month;
109108
String extension = ".xlsx";
110-
String file = filePath + fileName + extension;
111-
112-
InputStream is = null;
113-
try {
114-
is = new FileInputStream(file);
115-
} catch (FileNotFoundException e) {
109+
File file = new File(filePath + fileName + extension);
110+
111+
if(!file.exists()) {
112+
file.getParentFile().mkdirs();
116113
DBManageExcel.createMonthlyReportInExcel(year, month);
117-
is = new FileInputStream(file);
118114
}
119-
120-
Workbook workbook = ExcelUtils.getWorkbook(is, fileName + extension);
115+
116+
Workbook workbook = ExcelUtils.getWorkbook(new FileInputStream(file), fileName + extension);
121117
Sheet sheet = workbook.getSheetAt(0);
122118
sheet.getRow(rowIndex).getCell(colIndex).setCellValue(archiveUsage + "%");
123119
OutputStream os = new FileOutputStream(file);

src/main/java/root/utils/DBManageExcel.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Locale;
77

88
import org.apache.poi.ss.usermodel.HorizontalAlignment;
9+
import org.apache.poi.ss.usermodel.IndexedColors;
910
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
1011

1112
public class DBManageExcel extends ExcelUtils {
@@ -24,7 +25,7 @@ public static void createMonthlyReportInExcel(int year, int month) {
2425

2526
OutputStream fos;
2627
try {
27-
String filePath = "C:\\Users\\aserv\\Documents\\WorkSpace_DBMonitoring_Quartz\\DBMonitoring\\report\\";
28+
String filePath = "./report/";
2829
String fileName = "DB관리대장_종합_"+year+"."+month;
2930
String extension = ".xlsx";
3031
fos = new FileOutputStream(filePath + fileName + extension);
@@ -33,14 +34,14 @@ public static void createMonthlyReportInExcel(int year, int month) {
3334

3435
// CellStyle 생성
3536
// 1. 회색 배경, 검은색 실선 테두리, 중앙정렬
36-
XSSFCellStyle grayCS = excel.createCellStyle("d0cece", false);
37+
XSSFCellStyle grayCS = excel.createCellStyle(IndexedColors.GREY_25_PERCENT, false); /// "#d0cece"
3738
// 2. 회색 배경, 검은색 실선 테두리, 왼쪽정렬
38-
XSSFCellStyle grayCSLeft = excel.createCellStyle("d0cece", false);
39+
XSSFCellStyle grayCSLeft = excel.createCellStyle(IndexedColors.GREY_25_PERCENT, false);
3940
grayCSLeft.setAlignment(HorizontalAlignment.LEFT);
4041
// 3. 흰색 배경, 검은색 실선 테두리, 중앙정렬
41-
XSSFCellStyle whiteCS = excel.createCellStyle("ffffff", false);
42+
XSSFCellStyle whiteCS = excel.createCellStyle(IndexedColors.WHITE, false);
4243
// 4. 흰색 배경, 검은색 실선 테두리, 왼쪽정렬
43-
XSSFCellStyle whiteCSLeft = excel.createCellStyle("ffffff", false);
44+
XSSFCellStyle whiteCSLeft = excel.createCellStyle(IndexedColors.WHITE, false);
4445
whiteCSLeft.setAlignment(HorizontalAlignment.LEFT);
4546

4647
// Write Header Region

src/main/java/root/utils/ExcelUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void addRow(List<String> rows) {
108108
addRow(null, (short) 0, rows);
109109
}
110110

111-
public void addRow(String backgroundColor, List<String> rows) {
111+
public void addRow(IndexedColors backgroundColor, List<String> rows) {
112112
addRow(backgroundColor, (short) 0, rows);
113113
}
114114

@@ -120,7 +120,7 @@ public void addRow(String backgroundColor, List<String> rows) {
120120
* @param boldweight
121121
* @param cellStrings
122122
*/
123-
public void addRow(String backgroundColor, short boldweight, List<String> cellStrings) {
123+
public void addRow(IndexedColors backgroundColor, short boldweight, List<String> cellStrings) {
124124
Row header = sheet.createRow(rowIndex++);
125125
int cellIndex = colIndex;
126126
for (String value : cellStrings) {
@@ -156,7 +156,7 @@ public void addRow(List<Map<String, String>> style, List<String> cellStrings) {
156156
boldweight = Short.parseShort(styleMap.get("boldweight"));
157157
}
158158
}
159-
cell.setCellStyle(createCellStyle(backgroundColor, boldweight == 0 ? false : true));
159+
cell.setCellStyle(createCellStyle(IndexedColors.valueOf(backgroundColor), boldweight == 0 ? false : true));
160160
}
161161
if (maxCols < cellIndex) {
162162
maxCols = cellIndex;
@@ -170,12 +170,12 @@ public void addRow(List<Map<String, String>> style, List<String> cellStrings) {
170170
* @param boldweight
171171
* @return
172172
*/
173-
public XSSFCellStyle createCellStyle(String backgroundColor, boolean isBold) {
173+
public XSSFCellStyle createCellStyle(IndexedColors backgroundColor, boolean isBold) {
174174
XSSFCellStyle cellStyle = sheet.getWorkbook().createCellStyle();
175175
cellStyle.setAlignment(HorizontalAlignment.CENTER);
176176
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
177177
if (backgroundColor != null) {
178-
cellStyle.setFillForegroundColor(IndexedColors.valueOf(backgroundColor).getIndex());
178+
cellStyle.setFillForegroundColor(backgroundColor.getIndex());
179179
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
180180
}
181181
setSolidBorder(cellStyle);

0 commit comments

Comments
 (0)