POI Cell 의 wrap 설정 (텍스트 개행)

황제낙엽 2011.05.09 13:08 조회 수 : 2966

sitelink1  
sitelink2  
sitelink3  
extra_vars4  
extra_vars5  
extra_vars6  

wrap의 설정

셀로 설정된 값이 셀의 폭에 들어가지 않는 경우에 어떻게 표시하는지를 정의합니다.

warp 설정을 하려면 CellStyle 인터페이스로 준비되어 있는 setWrapText메소드를 사용합니다.

setWrapText
void setWrapText(boolean wrapped)

 

Set whether the text should be wrapped. Setting this flag to true make all content

visible whithin a cell by displaying it on multiple lines 

Parameters:
  wrapped - wrap text or not

 

true(을)로 지정하면 셀내에서 자동적으로 즉시 복수의 행에 엔터값이 표시됩니다.

실제 사용법은 다음과 같습니다.

Workbook wb = new HSSFWorkbook();
DataFormat format = wb.createDataFormat();

CellStyle  style = wb.createCellStyle();
style.setWrapText(true);

 

 

샘플 프로그램

실제로 시험해 봅시다.

Sample10_1.java

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;

public class Sample10_1{
  public static void main(String[] args){
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet();

    Row[] row = new Row[2];
    Cell[] cell = new Cell[2];

    for (int i = 0 ; i < 2 ; i++){
      row[i] = sheet.createRow(i + 1);
      cell[i] = row[i].createCell(2);
      cell[i].setCellValue("Thank you very much.");
    }

    CellStyle style0 = wb.createCellStyle();
    style0.setWrapText(true);
    cell[0].setCellStyle(style0);

    CellStyle style1 = wb.createCellStyle();
    style1.setWrapText(false);
    cell[1].setCellStyle(style1);

    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample10_1.xls");
      wb.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}

 

각 셀에 대해서wrap의 스타일을 설정하고 있습니다. 그러면 작성된 Excel파일을 열어 보겠습니다.

 

uaa.jpg

 

 

wrap(을)를 true(으)로 설정했을 경우, 값이 셀에 다 들어가지 못할 때는, 자동적으로 개행을 하여 전체가 셀안에 표시됩니다.  

번호 제목 글쓴이 날짜 조회 수
57 셀 크기 조정 (자동 크기 조정) 황제낙엽 2011.05.03 7740
» Cell 의 wrap 설정 (텍스트 개행) file 황제낙엽 2011.05.09 2966
55 POI HSSF, XSSF, SXSSF 성능 분석 file 황제낙엽 2013.11.05 1590
54 병합된 셀의 스타일( border) 설정하기 황제낙엽 2011.05.03 1564
53 Parsing and Processing Large XML Documents with Digester Rules (해석중) file 황제낙엽 2008.05.13 1478
52 POI 셀 스타일 설정을 위한 예제 소스 file 황제낙엽 2008.05.16 1379
51 엑셀(Excel)문서 처리 패키지 황제낙엽 2007.01.22 1334
50 POI-HSSF and POI-XSSF - Java API To Access Microsoft Excel Format Files 황제낙엽 2013.11.05 984
49 Comma Separated Values (CSV) - au.com.bytecode.opencsv file 황제낙엽 2007.01.23 626
48 Parsing, indexing, and searching XML with Digester and Lucene 황제낙엽 2008.05.07 429
47 POI HSSF 기능 가이드 -- 퀵·가이드 (한글) 황제낙엽 2008.05.16 373
46 JUnit 3.8에서 JUnit 4, TestNG 활용으로 황제낙엽 2007.09.17 369
45 Junit 을 이용한 효율적인 단위 테스트 전략 황제낙엽 2007.01.30 317
44 Library & Properties 파일 file 황제낙엽 2011.12.23 313
43 XSSF Examples file 황제낙엽 2011.05.04 254
42 사용자 정의 Appender 정의하여 Log4j 확장하기 황제낙엽 2009.05.28 220
41 log4j-1.2.15.jar 와 log4j.properties 예제 file 황제낙엽 2017.08.04 187
40 접속 클라이언트의 아이피별로 로그 화일 기록하기 file 황제낙엽 2009.06.01 183
39 Comma Separated Values (CSV) - com.Ostermiller.util Java Utilities 황제낙엽 2007.01.23 177
38 Apache Log4j 2 Configuration 파일 설정 황제낙엽 2020.04.01 150