반응형

apache poi 로 엑셀작업을 하다보면 셀(Cell) 크기 문제로 어려움을 겪는 경우가 많이 있습니다.


Cell 세로 높이에 대해서 알아보겠습니다.


가로 크기를 자동으로 해놓으면 내용에 따라 가로 넓이가 계속 늘어나기 때문에 세로 높이를 신경쓸 필요가 없습니다.

하지만 내용이 많은 경우 자동 줄 바꿈 옵션(셀객체.getCellStyle().setWrapText(true))을 통해 보여주어야 합니다. 


보통은 wrapText를 하면 세로 높이 역시 자동으로 조정 됩니다.

문제는 병합된 셀들의 경우 자동으로 높이가 조절되지 않습니다.


이런 경우 해당 셀의 텍스트를 분석해서 셀의 높이를 계산해 높이를 수동으로 설정하는 방법이 있습니다.

아래 예제 소스를 보시면 특이하게 java.awt.Font 를 사용하고 있습니다.

일종의 편법의 awt Font를 이용해서 세로 높이를 계산해 내는 겁니다.


참고로, 병합된 셀의 세로 높이가 자동으로 잡히지 않는 문제는 poi 의 영역이 아니라 엑셀 자체의 문제라고 합니다.


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
 
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.io.OutputStream;
import java.text.AttributedString;
import java.util.List;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


...........................중략........................

/**
   * ROW 높이 자동 조절
   * 
   * @param rownum
   * @param cellValue
   */
  public void setAutoRowFit(int cellnum, int rownum) {
    XSSFRow row = sheet.getRow(rownum);
    XSSFCell cell = row.getCell(cellnum);
    XSSFFont cellFont = cell.getCellStyle().getFont();
    int fontStyle = java.awt.Font.PLAIN;
    if (cellFont.getBold())
      fontStyle = java.awt.Font.BOLD;
    if (cellFont.getItalic())
      fontStyle = java.awt.Font.ITALIC;
    java.awt.Font currFont = new java.awt.Font(cellFont.getFontName(), fontStyle,
        cellFont.getFontHeightInPoints());
    String cellText = cell.getStringCellValue();
    AttributedString attrStr = new AttributedString(cellText);
    attrStr.addAttribute(TextAttribute.FONT, currFont);
    // Use LineBreakMeasurer to count number of lines needed for the text
    //
    FontRenderContext frc = new FontRenderContext(nulltruetrue);
    LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(), frc);
    int nextPos = 0;
    int lineCnt = 1;
    float columnWidthInPx = sheet.getColumnWidthInPixels(cellnum);
    while (measurer.getPosition() < cellText.length()) {
      nextPos = measurer.nextOffset(columnWidthInPx);
      lineCnt++;
      measurer.setPosition(nextPos);
    }
    System.out.println("lineCnt:" + lineCnt);
    if (lineCnt > 1) {
      row.setHeightInPoints(
          sheet.getDefaultRowHeightInPoints() * lineCnt * /* fudge factor */ 1f);
    }
  }
cs


반응형

+ Recent posts