[Java] 엑셀 다수 데이터 출력

BBANG-JUN·2020년 10월 21일
0

Java

목록 보기
4/6
post-thumbnail
public Workbook downloadExcel(QcVO qcVO){
		// 엑셀 구성 및 시트명
		Workbook xlsWb = new HSSFWorkbook();
                // 시트명
		Sheet sheet = xlsWb.createSheet("라벨");  

		// 행, 열 초기화
		Row row = null;
		Cell cell = null;
		int rowIdx = 0;
		int cellIdx = 0;

		// title : title부분을 셋팅해주는 부분
		String[] oligo = {"주문번호","LOT번호","회사","사용자","정제구분","요청일(주문일자)","합성날짜","Oligo명","Sequence(5' → 3')","Synthesis","tm"};
		row = sheet.createRow(rowIdx++);
		for(int i=0; i<oligo.length; i++) {
			cell = row.createCell(i);
	        cell.setCellValue(oligo[i]);
	        cell.setCellStyle(cellStyle(xlsWb, "title"));
		}

		//data : data를 객체에 맞는 List로 가져와 객체반복으로 데이터 형성
		List<QcVO> list = qcMapper.selectOligoQcLabelList(qcVO);
		for(QcVO egov : list) {
		row = sheet.createRow(rowIdx++);  // row : 데이터의 다음 행 생성.
		cell = row.createCell(cellIdx++); // cell : 데이터의 다음 열 생성.
	        cell.setCellValue(egov.getOrderNo()); // value : 값 저장
	        cell.setCellStyle(cellStyle(xlsWb, "data")); // style : 셀 스타일 설정"
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getLotNo());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getInstNm());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getUserNm());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getRefineMthNm());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getRegistDt());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getComptDt());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getOligoNm());
	        cell.setCellStyle(cellStyle(xlsWb, "dataLeft")); // style : 좌측정렬
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getOligoSeq());
	        cell.setCellStyle(cellStyle(xlsWb, "dataLeft"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getSynthesis());
	        cell.setCellStyle(cellStyle(xlsWb, "data"));
	        cell = row.createCell(cellIdx++);
	        cell.setCellValue(egov.getTm());
	        cell.setCellStyle(cellStyle(xlsWb, "dataRight")); // style : 우측정렬
	        cellIdx = 0;
		}

        	// 셀 너비 설정
		for (int i=0; i<=oligo.length; i++){
			sheet.autoSizeColumn(i);
			sheet.setColumnWidth(i, (sheet.getColumnWidth(i))+(short)1024);
		}

        	return xlsWb;
	}

	//셀 스타일 설정하는 함수
	public static CellStyle cellStyle(Workbook xlsWb, String kind) {
		CellStyle cs = xlsWb.createCellStyle();
		Font f = xlsWb.createFont();

		//헤더 스타일
		cs.setAlignment(HorizontalAlignment.CENTER);

		if("title".equals(kind)) {
			// 타이틀
			cs.setFillForegroundColor(IndexedColors.LIME.getIndex());
			cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);
			f.setFontHeightInPoints((short) 11);
			f.setBold(true);
			f.setFontName("맑은 고딕");
			cs.setFont(f);
		}else if("data".equals(kind)) {
			// 데이터
			f.setFontHeightInPoints((short) 11);
			f.setFontName("맑은 고딕");
			cs.setFont(f);
		}else if("dataRight".equals(kind)) {
			// 데이터 우측정렬
			f.setFontHeightInPoints((short) 11);
			f.setFontName("맑은 고딕");
			cs.setFont(f);
			cs.setAlignment(HorizontalAlignment.RIGHT);
		}else if("dataLeft".equals(kind)) {
			// 데이터 좌측정렬
			f.setFontHeightInPoints((short) 11);
			f.setFontName("맑은 고딕");
			cs.setFont(f);
			cs.setAlignment(HorizontalAlignment.LEFT);
		}

		return cs;
	}
profile
🔥 머릿속으로 생각하지만 말고, 행동으로 보여줘

0개의 댓글