지금까지 프로젝트을 진행하면서 엑셀다운로드 기능은 꼭 한번씩은 나온거 같다.

어느 언어로 개발을 하든 엑셀다운로드 라이브러리가 대부분 제공이 된다.

 

지금은 POI 를 통해 엑셀다운로드 기능을 알라여보려고한다.

 

http://poi.apache.org/components/spreadsheet/

 

 

HSSF : EXCEL 2007 이전 버전(.xls)  - 65535 라인까지 사용가능

XSSF : EXCEL 2007 이후 버전(2007포함 .xlsx -  - 65535 라인 이상 사용가능

SXSSF : XSSF Streaming Version으로 메모리를 적게 사용 - 65535 라인 이상 사용가능



public void excelDownLoad() {
	FileOutputStream fos = null;

	// 워크북
	SXSSFWorkbook workbook = null;		
	// 행
	SXSSFRow row = null;
	// 셀
	SXSSFCell cell = null;				
	// 샐 스타일
	CellStyle styleMoneyFormat = null;	
	// 셀 헤더 카운트
	int index = 0;								
	// 행 카운트
	int rowIndex = 1;							

	List<DataDto> dataList = null;

	// 엑셀 헤더 정보 구성
	String[] cellHeader = {"번호", "테스트", "날짜", "시간"};

	try {
		dataList = mapper.getData();


	    // 워크북 생성
	    workbook = new SXSSFWorkbook();
	    workbook.setCompressTempFiles(true);

		// 워크시트 생성
	    SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet("시트이름");
	    sheet.setRandomAccessWindowSize(100); // 메모리 행 100개로 제한, 초과 시 Disk로 flush

		//셀 칼럼 크기 설정
		sheet.setColumnWidth(2, 300);
		// 행 생성
		row = sheet.createRow(0);
		// 셀 스타일 생성
		styleMoneyFormat = workbook.createCellStyle();
		CreationHelper ch = workbook.getCreationHelper();
		styleMoneyFormat.setDataFormat(ch.createDataFormat().getFormat("#,##0"));

		// 헤더 적용
		for(String head : cellHeader ) {
			cell = row.createCell(index++);
			cell.setCellValue(head);
		}

		for(DataDto dataDto : dataList) {
			row = sheet.createRow(rowIndex);

            cell = row.createCell(0);
            cell.setCellValue(rowIndex++);			//번호

            cell = row.createCell(1);
            cell.setCellValue(dataDto.getTest());	//테스트

            cell = row.createCell(2);
            cell.setCellValue(dataDto.getDate());	//날짜

            cell = row.createCell(3);
            cell.setCellValue(dataDto.getTime());	//시간

		}
		
        String filename = "파일명.xlsx";
        String orgFileName = "TEST_TEMP_FILE_01_.xlsx"; // 서버저장파일명
        String fileDownLoadPath = "/home/excelTemp/"

        // 파일생성
        fos = new FileOutputStream(fileDownLoadPath + orgFileName);
        workbook.write(fos);

        paramMap.put("filePath", fileDownLoadPath);
        paramMap.put("realFilNm", orgFileName);
        paramMap.put("viewFileNm", filename);

    } catch (Exception e) {
        if(fos != null) try { fos.close(); } catch(Exception ignore) {}
    } finally {
        try {
            workbook.close();
            workbook.dispose();
            if(fos != null) try { fos.close(); } catch(Exception ignore) {}
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

 


 

 

 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기