Skip to content

Commit 7a04b14

Browse files
authored
Merge pull request #146 from Dokyeongyun/fx-220209-fixExcelBug
Fx 220209 fix excel bug
2 parents 8d86329 + 9377773 commit 7a04b14

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

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

Lines changed: 22 additions & 22 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;
@@ -23,7 +22,7 @@
2322
import root.utils.CsvUtils;
2423
import root.utils.DBManageExcel;
2524
import root.utils.DateUtils;
26-
import root.utils.ExcelUtils;
25+
import root.utils.ExcelSheet;
2726

2827
public class DBCheckUsecaseImpl implements DBCheckUsecase {
2928
private DBCheckRepository dbCheckRepository;
@@ -38,6 +37,17 @@ public DBCheckUsecaseImpl(DBCheckRepository dbCheckRepository, ReportRepository
3837
public void printArchiveUsageCheck() {
3938
MonitoringResult<ArchiveUsage> result = dbCheckRepository.checkArchiveUsage();
4039
System.out.println("\t▶ Archive Usage Check");
40+
41+
result.getMonitoringResults().forEach(r -> {
42+
if (r.getUsedPercent() >= 90) {
43+
System.out.println("\t" + ConsoleUtils.BACKGROUND_RED + ConsoleUtils.FONT_WHITE
44+
+ "▶ Archive Usage Check : Usage 90% 초과! (" + r.getArchiveName() + ")" + ConsoleUtils.RESET
45+
+ "\n");
46+
} else {
47+
System.out.println("\t▶ Archive Usage Check : SUCCESS\n");
48+
}
49+
});
50+
4151
try {
4252
TextTable tt = new TextTable(
4353
new CsvTableModel(CsvUtils.toCsvString(result.getMonitoringResults(), ArchiveUsage.class)));
@@ -81,14 +91,6 @@ public void printASMDiskCheck() {
8191
public void writeExcelArchiveUsageCheck() throws Exception {
8292
MonitoringResult<ArchiveUsage> result = dbCheckRepository.checkArchiveUsage();
8393
String dbName = dbCheckRepository.getDBName();
84-
double archiveUsage = result.getMonitoringResults().get(0).getUsedPercent();
85-
86-
if (archiveUsage >= 90) {
87-
System.out.println("\t" + ConsoleUtils.BACKGROUND_RED + ConsoleUtils.FONT_WHITE
88-
+ "▶ Archive Usage Check : Usage 90% 초과!" + ConsoleUtils.RESET + "\n");
89-
} else {
90-
System.out.println("\t▶ Archive Usage Check : SUCCESS\n");
91-
}
9294

9395
int year = Integer.parseInt(DateUtils.getToday("yyyy"));
9496
int month = Integer.parseInt(DateUtils.getToday("MM"));
@@ -104,20 +106,18 @@ public void writeExcelArchiveUsageCheck() throws Exception {
104106
rowIndex = 29;
105107
}
106108

107-
String filePath = "C:\\Users\\aserv\\Documents\\WorkSpace_DBMonitoring_Quartz\\DBMonitoring\\report\\";
108-
String fileName = "DB관리대장_종합_" + year + "." + month;
109+
String filePath = "./report/";
110+
String fileName = "DB관리대장_종합_" + year + "." + DateUtils.getTwoDigitDate(month);
109111
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) {
112+
File file = new File(filePath + fileName + extension);
113+
114+
if(!file.exists()) {
115+
file.getParentFile().mkdirs();
116116
DBManageExcel.createMonthlyReportInExcel(year, month);
117-
is = new FileInputStream(file);
118117
}
119-
120-
Workbook workbook = ExcelUtils.getWorkbook(is, fileName + extension);
118+
119+
double archiveUsage = result.getMonitoringResults().get(0).getUsedPercent();
120+
Workbook workbook = ExcelSheet.getWorkbook(new FileInputStream(file), fileName + extension);
121121
Sheet sheet = workbook.getSheetAt(0);
122122
sheet.getRow(rowIndex).getCell(colIndex).setCellValue(archiveUsage + "%");
123123
OutputStream os = new FileOutputStream(file);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import root.utils.CsvUtils;
3333
import root.utils.DBManageExcel;
3434
import root.utils.DateUtils;
35-
import root.utils.ExcelUtils;
35+
import root.utils.ExcelSheet;
3636

3737
public class ServerCheckUsecaseImpl implements ServerCheckUsecase {
3838
private ServerCheckRepository serverCheckRepository;
@@ -212,7 +212,7 @@ public void writeExcelOSDiskUsage() throws Exception {
212212
is = new FileInputStream(file);
213213
}
214214

215-
Workbook workbook = ExcelUtils.getWorkbook(is, fileName+extension);
215+
Workbook workbook = ExcelSheet.getWorkbook(is, fileName+extension);
216216
Sheet sheet = workbook.getSheetAt(0);
217217

218218
for (OSDiskUsage data : result.getMonitoringResults()) {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
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

11-
public class DBManageExcel extends ExcelUtils {
12+
public class DBManageExcel extends ExcelSheet {
1213

1314
public DBManageExcel(String sheetName) {
1415
super(sheetName);
@@ -24,23 +25,23 @@ 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);
3132

32-
ExcelUtils excel = new ExcelUtils("MonthlyReport");
33+
ExcelSheet excel = new ExcelSheet("MonthlyReport");
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 renamed to src/main/java/root/utils/ExcelSheet.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
3939
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
4040

41-
public class ExcelUtils {
41+
public class ExcelSheet {
4242
private XSSFWorkbook workbook;
4343
private XSSFSheet sheet;
4444
private int rowIndex = 0;
@@ -48,7 +48,7 @@ public class ExcelUtils {
4848
// 1. DB 관리대장 포맷을 월별로 작성해주는 메서드 작성 (관리대장 신규 생성)
4949
// 2. 기존 관리대장을 읽어 특정 날짜의 DB 모니터링 결과를 추가하는 메서드 작성 (모니터링 결과 작성)
5050

51-
public ExcelUtils(String sheetName) {
51+
public ExcelSheet(String sheetName) {
5252
workbook = new XSSFWorkbook();
5353
sheet = workbook.createSheet(sheetName);
5454
}
@@ -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)