엄지월드

[Excel POI] 자바 엑셀 다운로드 POI 예제(Maven 미사용) 본문

java

[Excel POI] 자바 엑셀 다운로드 POI 예제(Maven 미사용)

킨글 2017. 5. 5. 20:12
반응형

이 예제를 변형하면 웹에서도 혹은 DB에 있는 값을 뽑아서 다운로드가 가능할 것이다!

 

먼저 POI library를 추가해야 한다. 

아래의 링크에서 다운 받도록 한다(아무거나 받아서 알집을 풀고 이클립스에서 JAR파일 추가)

https://poi.apache.org/download.html

그리고 프로젝트에서 라이브러리를 추가한다.

프로젝트 우클릭 -> properties -> Java Build Path -> Libraries 탭 -> Add External JARS.. 버튼 클릭해서

 

 

package javaTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;



public class ex01 {
    public void writeData() throws Exception {
        try {
            String filePath = "C:\\javalec\\"; // file 생성 위치
            String filename = "aaaaa.xls"; // 생성될 파일 이름
            FileOutputStream fout = setFile(filePath, filename);

            HSSFWorkbook wb = setExcel(filename);
            // write the workbook to the output stream
            wb.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private FileOutputStream setFile(String filePath, String filename) throws FileNotFoundException {
        // 엑셀 파일 생성
        // 디렉토리 없으면 생성.
        File fDir = new File(filePath);
        if (!fDir.exists()) {
            fDir.mkdirs();
        }

        FileOutputStream fout = new FileOutputStream(filePath + filename);
        return fout;
    }



    private HSSFWorkbook setExcel(String filename) throws IOException {
        // 엑셀 파일 생성
        HSSFWorkbook wb = new HSSFWorkbook();

        // 쉬트 및 폰트 지정
        HSSFSheet sht = wb.createSheet(filename);
        sht.setGridsPrinted(true);
        sht.setFitToPage(true);
        sht.setDisplayGuts(true);
        HSSFRow row = null;
        HSSFCell cell = null;

        // 쉬트 이름 주기
        wb.setSheetName(0, filename);

        // 제목 줄 생성
        String[] title1 = { "NO.", "제목", "작성자" };
        String[] title2 = { "NO.", "공지합니다!!", "엄지용" };
        String[] contents = { "1", "공지합니다!!", "엄지용" };

        int[] cellwidth = { 6, 20, 20 };

        // row 1 table start
        row = sht.createRow((short) 1);
        row.setHeight((short) 500); // 칼럼 높이
        short width = 265;

        // ========== title1 - first row ========================

        for (int i = 0; i < title1.length; i++) {
            sht.setColumnWidth(i, (cellwidth[i] * width)); // Column 넓이 설정
            cell = row.createCell(i);
            cell.setCellValue(new HSSFRichTextString(title1[i]));
            if (i == 1) {
                // ====== Cell 합병 ==================
                sht.addMergedRegion(new CellRangeAddress(1, 1, i, i + 1));
                // ==================================
            }

            cell.setCellStyle(getTitleStyle(wb));
        }

        // ===========title2 - Second row ====================
        row = sht.createRow(2);
        row.setHeight((short) 500); // 칼럼 높이

        for (int i = 0; i < title2.length; i++) {
            sht.setColumnWidth(i, (cellwidth[i] * width));
            cell = row.createCell((i));
            cell.setCellValue(new HSSFRichTextString(title2[i]));
            cell.setCellStyle(getTitleStyle(wb));
        }



        // ======================================================
        // =========== Table Contents ===================
        row = sht.createRow(3);
        row.setHeight((short) 500); // 칼럼 높이

        for (int i = 0; i < contents.length; i++) {
            sht.setColumnWidth(i, (cellwidth[i] * width));
            cell = row.createCell((i));

            if (i == 0)
                cell.setCellValue(new HSSFRichTextString(String.valueOf(i)));
            else
                cell.setCellValue(new HSSFRichTextString(contents[i]));

            cell.setCellStyle(getTextStyle(wb));
        }

        // =====================================================
        // 출력설정
        HSSFPrintSetup hps = sht.getPrintSetup();

        // 용지설정
        hps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

        // 출력방향설정
        hps.setLandscape(false);

        // 출력비율설정
        hps.setScale((short) 100);

        // footer에 페이지번호 설정
        HSSFFooter footer = sht.getFooter();
        footer.setCenter(HSSFFooter.page() + "/" + HSSFFooter.numPages());

        // 쉬트 여백 설정
        sht.setMargin(HSSFSheet.TopMargin, 0.6);
        sht.setMargin(HSSFSheet.BottomMargin, 0.6);
        sht.setMargin(HSSFSheet.LeftMargin, 0.6);
        sht.setMargin(HSSFSheet.RightMargin, 0.6);

        // 반복행 설정
        // wb.setRepeatingRowsAndColumns(0, 0, 3, 0, 0); // 뭐하는것이지?

        return wb;
    }



    /*

     * @@@@@ Font 설정 Method @@@@@

     */

    private HSSFCellStyle getTitleStyle(HSSFWorkbook wb) {
        // 제목 폰트
        HSSFFont hf = wb.createFont();
        hf.setFontHeightInPoints((short) 8);
        hf.setColor((short) HSSFColor.BLACK.index);
        hf.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        // Header style setting
        HSSFCellStyle hcs = wb.createCellStyle();
        hcs.setFont(hf);
        hcs.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // set border style
        hcs.setBorderBottom(HSSFCellStyle.BORDER_THICK);
        hcs.setBorderRight(HSSFCellStyle.BORDER_THIN);
        hcs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        hcs.setBorderTop(HSSFCellStyle.BORDER_THIN);
        hcs.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        // set color
        hcs.setFillBackgroundColor((short) HSSFColor.WHITE.index);
        hcs.setFillForegroundColor((short) HSSFColor.VIOLET.index);
        hcs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        hcs.setLocked(true);
        hcs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return hcs;
    }



    /*

     * @@@@@ Font 설정 Method @@@@@

     */

    private HSSFCellStyle getTextStyle(HSSFWorkbook wb) {
        HSSFFont hf = wb.createFont();
        hf.setFontHeightInPoints((short) 8);
        hf.setColor((short) HSSFColor.BLACK.index);

        HSSFCellStyle hcs = wb.createCellStyle();
        hcs.setFont(hf);
        hcs.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // set border style
        hcs.setBorderBottom(HSSFCellStyle.BORDER_THICK);
        hcs.setBorderRight(HSSFCellStyle.BORDER_THIN);
        hcs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        hcs.setBorderTop(HSSFCellStyle.BORDER_THIN);
        hcs.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        // set color
        hcs.setFillBackgroundColor((short) HSSFColor.WHITE.index);
        hcs.setFillForegroundColor((short) HSSFColor.WHITE.index);
        hcs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        hcs.setLocked(true);
        hcs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return hcs;
    }

    public static void main(String args[]) throws IOException {

        ex01 ew = new ex01();

        try {
            ew.writeData();
        } catch (Exception e) {
            System.out.println("error!!");
            e.printStackTrace();
        }
    }
}

 

추가로 참조할만한 사이트 : https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/

 

'java' 카테고리의 다른 글

eclipse module import 및 export 방법  (0) 2022.09.30
메서드(Method) 함수(Function) 차이  (0) 2021.03.14
java8 lambda stream 시작하기  (0) 2018.09.01
[Excel POI] 시간만 저장하기  (2) 2017.09.16
[Excel POI] 예제 모음  (0) 2017.08.24
Comments