OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2021-10-14 d680ff9fa4298ad3c0cd12f5f9d87f6c51110480
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package kr.wisestone.owl.web.view;
 
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import kr.wisestone.owl.annotation.Viewer;
import kr.wisestone.owl.common.MessageAccessor;
import kr.wisestone.owl.constant.Constants;
import kr.wisestone.owl.util.CommonUtil;
import kr.wisestone.owl.vo.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
 
@Viewer
public class ExcelView extends AbstractExcelView {
    private static final Logger log = LoggerFactory.getLogger(ExcelView.class);
 
    public static final Integer EXCEL_EXPORT_MAX_COUNT = 65000;
 
    @Autowired
    protected MessageAccessor messageAccessor;
 
    @SuppressWarnings({"unused", "unchecked"})
    @Override
    protected void buildExcelDocument(Map<String, Object> model, XSSFWorkbook workbook, HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
 
        XSSFSheet sheet;
        XSSFRow mergeRow = null;
        XSSFRow mergeRow2 = null;
        XSSFRow sheetRow = null;
        XSSFRow sheetRow2 = null;
 
        ExportExcelVo excelInfo = (ExportExcelVo) model.get(Constants.EXCEL);
        String localeFileName = this.findLocaleToText(excelInfo.getFileName());
 
        response.setContentType("application/vnd.ms-excel; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + CommonUtil.getDisposition(localeFileName, CommonUtil.getBrowser(request)) + ".xlsx");
 
        // 총 데이터양을 뽑아 낸다.
        int totalCount = excelInfo.getDatas().size();
 
        // 시트 생성
        sheet = workbook.createSheet(excelInfo.getFileName());
        int currentRowNum = 0;
 
        // 셀 스타일을 설정한다.
        XSSFCellStyle sheetTitleCellStyle = this.makeSheetTitleStyle(workbook);
        XSSFCellStyle gridPageCellStyle = this.makeGridPageStyle(workbook);
        XSSFCellStyle gridHeaderCellStyle = this.makeGridHeaderStyle(workbook);
 
        Boolean merge = Boolean.FALSE;  //  merge 여부 확인
 
        int colIndex = 0;
        for (ExportExcelAttrVo attrInfo : excelInfo.getAttrInfos()) {
            if (attrInfo.getMergeCount() == 1) {
                merge = true;
                //셀 병합
                if (totalCount >= 2) {
                    totalCount = totalCount/  2;
                    int rowNum3 = 3 + totalCount + (totalCount - 2);
                    for (int rowNum1 = 3; rowNum1 <= rowNum3; rowNum1 += 2) {
                        int rowNum2 = rowNum1 + 1;
                        sheet.addMergedRegion(new CellRangeAddress(rowNum1, rowNum2, 0, 0));
                    }
                }
            }
            sheet.setColumnWidth(colIndex++, (attrInfo.getWidth() + 1) * 2 * 256);
            attrInfo.setCellStyle(this.makeCellStyle(workbook, this.makeFont(workbook, XSSFFont.BOLDWEIGHT_NORMAL),
                    attrInfo.getAlign(), XSSFCellStyle.VERTICAL_CENTER, XSSFCellStyle.BORDER_THIN));
        }
 
        int size = excelInfo.getDatas().size();
        //  merge 를 사용할 경우
        if (merge) {
            size = excelInfo.getDatas().size() / 2;
        }
 
        this.writeSheetTitle(sheet, sheetTitleCellStyle, currentRowNum++, localeFileName, excelInfo.getAttrInfos()
                .size() - 1, (short) 1024);
 
        //  건수 표시 제어
        if (!excelInfo.getHideCount()) {
            this.writeGridPage(sheet, gridPageCellStyle, currentRowNum++, size, excelInfo
                    .getAttrInfos().size() - 1);
        }
 
        this.writeGridHeader(sheet, gridHeaderCellStyle, currentRowNum++, excelInfo.getAttrInfos());
 
        for (int index = 0; index < excelInfo.getDatas().size(); index++) {
            if (EXCEL_EXPORT_MAX_COUNT <= index) {
                break;
            }
 
            Object data = excelInfo.getDatas().get(index);
            XSSFRow row = sheet.createRow(currentRowNum++);
 
            int currentColNum = 0;
            for (ExportExcelAttrVo attrInfo : excelInfo.getAttrInfos()) {
                this.writeData(workbook, row, currentColNum++, data, attrInfo);
            }
        }
 
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
        } finally {
            if (out != null) {
                out.close();
            }
        }
 
    }
 
    private XSSFCellStyle makeSheetTitleStyle(XSSFWorkbook workbook) {
        XSSFFont font = this.makeFont(workbook, XSSFFont.BOLDWEIGHT_BOLD, (short) 20);
        XSSFCellStyle cellStyle = this.makeCellStyle(workbook, font, XSSFCellStyle.ALIGN_CENTER,
                XSSFCellStyle.VERTICAL_CENTER, XSSFCellStyle.BORDER_NONE);
 
        return cellStyle;
    }
 
    private XSSFCellStyle makeGridPageStyle(XSSFWorkbook workbook) {
        XSSFFont font = this.makeFont(workbook, XSSFFont.BOLDWEIGHT_BOLD);
        XSSFCellStyle cellStyle = this.makeCellStyle(workbook, font, XSSFCellStyle.ALIGN_RIGHT,
                XSSFCellStyle.VERTICAL_CENTER, XSSFCellStyle.BORDER_NONE);
 
        return cellStyle;
    }
 
    private XSSFCellStyle makeGridHeaderStyle(XSSFWorkbook workbook) {
        XSSFFont font = this.makeFont(workbook, XSSFFont.BOLDWEIGHT_BOLD);
        XSSFCellStyle cellStyle = this.makeCellStyle(workbook, font, XSSFCellStyle.ALIGN_CENTER,
                XSSFCellStyle.VERTICAL_CENTER, XSSFCellStyle.BORDER_THIN);
 
        // 셀에 색깔 채우기
        cellStyle.setFillForegroundColor(new HSSFColor.GREY_25_PERCENT().getIndex());
        cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
 
        return cellStyle;
    }
 
    private XSSFCellStyle makeCellStyle(XSSFWorkbook workbook, XSSFFont font, short hAlign, short vAlign, short border) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
 
        cellStyle.setFont(font);
        cellStyle.setAlignment(hAlign);
        cellStyle.setVerticalAlignment(vAlign);
        cellStyle.setBorderTop(border);
        cellStyle.setBorderLeft(border);
        cellStyle.setBorderBottom(border);
        cellStyle.setBorderRight(border);
        cellStyle.setWrapText(true);
 
        return cellStyle;
    }
 
    private XSSFFont makeFont(XSSFWorkbook workbook) {
        XSSFFont font = workbook.createFont();
        font.setFontName("맑은 고딕");
        font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
        font.setColor(HSSFColor.BLACK.index);
 
        return font;
    }
 
    private XSSFFont makeFont(XSSFWorkbook workbook, short bold) {
        XSSFFont font = workbook.createFont();
        font.setFontName("맑은 고딕");
        font.setBoldweight(bold);
        font.setColor(HSSFColor.BLACK.index);
 
        return font;
    }
 
    private XSSFFont makeFont(XSSFWorkbook workbook, short bold, short size) {
        XSSFFont font = workbook.createFont();
        font.setFontName("맑은 고딕");
        font.setBoldweight(bold);
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints(size);
 
        return font;
    }
 
    @SuppressWarnings("unchecked")
    private void writeData(XSSFWorkbook workbook, XSSFRow row, int colnum, Object data, ExportExcelAttrVo attrInfo) {
        XSSFCell cell = row.createCell(colnum);
        cell.setCellStyle(attrInfo.getCellStyle());
 
        String cellValue = "";
 
        if (this.getAttrValue(data, attrInfo.getName()) != null) {
            if (attrInfo.getI18Prefix() != null) {
                if (this.getAttrValue(data, attrInfo.getName()) instanceof List) {
                    StringBuffer sb = new StringBuffer("");
                    List<String> values = (List<String>) this.getAttrValue(data, attrInfo.getName());
                    int idx = 0;
                    for (String value : values) {
                        if (idx != 0) {
                            sb.append("\n");
                        }
                        sb.append(this.messageAccessor.message(attrInfo.getI18Prefix() + value));
                        idx++;
                    }
                    cellValue = sb.toString();
                }
                else {
                    cellValue = this.messageAccessor.message(attrInfo.getI18Prefix() + this.getAttrValue(data, attrInfo.getName()).toString());
                }
            }
            else {
                if (this.getAttrValue(data, attrInfo.getName()) instanceof List) {
                    StringBuffer sb = new StringBuffer("");
                    List values = (List) this.getAttrValue(data, attrInfo.getName());
                    int idx = 0;
                    for (Object value : values) {
                        if (idx != 0) {
                            sb.append("\n");
                        }
 
                        if (value instanceof BaseVo) {
                            sb.append(this.getAttrValue(value, attrInfo.getVoAttrName()));
                        }
                        else {
                            sb.append(value);
                        }
                        idx++;
                    }
                    cellValue = sb.toString();
                }
                else {
                    cellValue = this.getAttrValue(data, attrInfo.getName()).toString();
                }
            }
        }
 
        if (attrInfo.getReplaceWithNewlineYn()) {
            cellValue = CommonUtil.replaceBrWithNewline(cellValue);
        }
 
        cell.setCellValue(cellValue);
    }
 
    private void writeGridHeader(XSSFSheet sheet, XSSFCellStyle cellStyle, int rownum, List<ExportExcelAttrVo> attrInfos) {
        XSSFRow row = sheet.createRow(rownum);
 
        int colnum = 0;
 
        for (ExportExcelAttrVo attrInfo : attrInfos) {
            XSSFCell cell = row.createCell(colnum++);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(attrInfo.getDesc());
        }
 
    }
 
    private void writeSheetTitle(XSSFSheet sheet, XSSFCellStyle cellStyle, int rownum, String title, int mergeCount,
                                 short height) {
        XSSFRow row = sheet.createRow(rownum);
        row.setHeight(height);
 
        XSSFCell cell = row.createCell(0);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(title);
 
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, mergeCount));
    }
 
    private void writeGridPage(XSSFSheet sheet, XSSFCellStyle cellStyle, int rownum, int size, int mergeCount) {
        XSSFRow row = sheet.createRow(rownum);
 
        XSSFCell cell = row.createCell(0);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(size + this.messageAccessor.message("common.few"));   //  건수
 
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, 0, mergeCount));
    }
 
    /**
     * 총 건수를 다국어 메세지로 전환한다.
     *
     * @param string
     */
    private String findLocaleToText(String string) {
        MessageVo message = this.messageAccessor.getMessage(string);
        return message.getMessage();
    }
 
    /**
     * 오브젝트로 부터 attr 속성의 값을 반환한다.
     *
     * @param data
     * @param attr
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private Object getAttrValue(Object data, String attr) {
        Object value = null;
        if (data instanceof Map) {
            value = ((Map) data).get(attr);
        }
        else {
            PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(data.getClass(), attr);
            Method method = descriptor.getReadMethod();
 
            try {
                value = method.invoke(data, null);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                value = "";
            }
        }
 
        return value;
    }
 
}