티스토리 뷰

5.0 기준 - mvnrepository.com/artifact/org.apache.poi/poi/5.0.0

 

둘중 하나 추가 후 개발

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>
// https://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '5.0.0'

 

 

package tset;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class main {
	
	public static void main(String[] args) throws Exception {
		
		Workbook xlsxWB = new XSSFWorkbook();
		
		//Sheet 이름 설정하기 
        Sheet sheet1 = xlsxWB.createSheet("sheet");
 
        // 컬럼의 너비 설정 
        sheet1.autoSizeColumn(1); //행 번호 
        //sheet.setColumnWidth(i, (sheet.getColumnWidth(i))+512 ) 이것도 방법 
        
        // Cell 스타일 생성
        CellStyle cellStyle = xlsxWB.createCellStyle();
        
        //색상 지정
        cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); // 노란색
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 이걸 안쓰면 안채워 진다
        
        
        // Cell 스타일 생성2
        CellStyle cellStyle2 = xlsxWB.createCellStyle();
        // 셀 크기에 따른 줄 바꿈
        cellStyle2.setWrapText(true);

        Row row = null;
        Cell cell = null;
         
        // 첫 행 만들기
        row = sheet1.createRow(0);
         
        //첫행 : 1열 
        cell = row.createCell(0);
        cell.setCellValue("1행 1열");
        cell.setCellStyle(cellStyle); 
        
        //첫행 : 2열 
        cell = row.createCell(1);
        cell.setCellValue("1행 2열");
        cell.setCellStyle(cellStyle);
         
        //첫행 : 3열 
        cell = row.createCell(2);
        cell.setCellValue("1행 3열");
        cell.setCellStyle(cellStyle);

         
        // 두번째 행
        row = sheet1.createRow(1);
         
        // 2행 1열 
        cell = row.createCell(0);
        cell.setCellValue("안녕하세요");
         
        // 2행 2열
        cell = row.createCell(1);
        cell.setCellValue("엑셀 다루는 방법입니다. 글을 길게 써서 줄바꿈 테스트 ");
        cell.setCellStyle(cellStyle2);
        
        // 2행 3열 : 숫자타입
        cell = row.createCell(2);
        cell.setCellValue("1234"); //   숫자타입 입력 
        cell.setCellType(CellType.NUMERIC);
        
        // 2행 4열 : 텍스트
        cell = row.createCell(3);
        cell.setCellValue("1234"); //  텍스트 타입 입력 
        cell.setCellType(CellType.STRING);
        
       
        // 파일저장 
        String filename = "test_" + Calendar.getInstance().getTimeInMillis()+".xlsx";
        try {
            File xlsFile = new File("C:\\Users\\Noah\\Documents\\" + filename);
            FileOutputStream fileOut = new FileOutputStream(xlsFile);
            xlsxWB.write(fileOut);
        }  catch (Exception e) {
            e.printStackTrace();
        }
	}

}

대충 파악하게 수정.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함