팡이네

POI 사용 Excel Cell 생성 및 한 Cell에 여러 줄 표시

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
private void createCell(XSSFRow row, int nCell, Font font, XSSFColor bgColor, String cellValue)
{
    short hAlign = CellStyle.ALIGN_LEFT;
    short vAlign = CellStyle.VERTICAL_CENTER;
    short border = CellStyle.BORDER_THIN;
     
    XSSFSheet sheet = row.getSheet();
    XSSFWorkbook wb = sheet.getWorkbook();
     
    int nCount = 0;
    String[] remark = cellValue.split("\\|");   //분리자 |
    String description = "";
     
    //줄 높이 계산
    for (int k = 0; k < remark.length; k++)
    {
        if (remark[k].length() > 0)
        {
            if (nCount == 0)
                description += remark[k];
            else
                description += "\r\n"+ remark[k];
             
            nCount++;
        }
    }
     
    //줄 높이 설정
    if (nCount > 1)
        row.setHeightInPoints((nCount * sheet.getDefaultRowHeightInPoints()));
     
    //스타일 설정
    XSSFCellStyle cs = wb.createCellStyle();
    cs.setFont(font);
     
    //배경색 설정
    cs.setFillForegroundColor(bgColor); //주의: 반드시 ForegroundColor를 사용(BackgroundColor가 아님)
    cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
     
    //정렬 설정
    cs.setAlignment(hAlign);
    cs.setVerticalAlignment(vAlign);
     
    //테두리 설정
    cs.setBorderTop(border);
    cs.setBorderRight(border);
    cs.setBorderBottom(border);
    cs.setBorderLeft(border);
     
    //여러 줄 표시할 경우 꼭 true
    cs.setWrapText(true);
     
    //Cell 생성
    XSSFCell cell = row.createCell(nCell);
    cell.setCellStyle(cs);
    cell.setCellValue(new XSSFRichTextString(description));
     
    sheet.autoSizeColumn(nCell);    //너비를 자동으로 다시 설정
}


'Java' 카테고리의 다른 글

JDK 8 인스톨 없이 설치하기  (0) 2018.10.20
사진 이미지 크기 변경 후 Base64 문자열 변환  (0) 2018.04.26
POI, Excel 셀 생성  (0) 2015.06.30
POI, Excel 숫자 셀 생성  (0) 2015.06.30
POI, Excel 병합 셀 생성  (0) 2015.06.30