새소식

반응형
Java/Spring

특정 List 정보 .xlsx 파일 다운로드

  • -
반응형

Controller

@PostMapping(value="/list/file/download")

public ResponseEntity<Resource> licenseListFileDownloadController(@RequestBody List<HtmlToEquipmentVO> htlvo) {

Resource resource = es.equipmentListFileDownload(htlvo);

//서비스에서 Resource 만들어 리턴

HttpHeaders header = new HttpHeaders();

String mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

header.setCacheControl("no-cache");

header.setContentType(MediaType.parseMediaType(mimeType));

return new ResponseEntity<Resource>(resource, header, HttpStatus.OK);

}

* ResponseEntity

: 응답할때 헤더, 몸체, 상태 총 3가지를 응답해주기 위한 객체

Service

public Resource equipmentListFileDownload(List<HtmlToEquipmentVO> htevo) {

XSSFWorkbook wb = new XSSFWorkbook();

XSSFSheet sheet = null;

XSSFRow row = null;

XSSFCell cell = null;

XSSFFont header_font = null;

XSSFFont body_font = null;

CellStyle header_style = null;

CellStyle body_style = null;

int equipment_list_row_no = 0;

sheet = wb.createSheet("장비 목록");

for(int i=0;i<9;i++) {

sheet.setColumnWidth(i, (sheet.getColumnWidth(i)+(short)2048));

}

//header font

header_font = wb.createFont();

header_font.setFontHeightInPoints((short) 20);

header_font.setBold(true);

//header cell style

header_style = wb.createCellStyle();

header_style.setFont(header_font);

header_style.setAlignment(HorizontalAlignment.CENTER);

//0행 헤더설정

sheet.addMergedRegion(new CellRangeAddress(0,0,0,8));

row = sheet.createRow(equipment_list_row_no++);

cell = row.createCell(0);

cell.setCellStyle(header_style);

cell.setCellValue("장비 목록");

//본문 컬럼 font 설정

body_font = wb.createFont();

body_font.setFontHeightInPoints((short) 12);

body_font.setBold(true);

//본문 컬럼 cell 스타일

body_style = wb.createCellStyle();

body_style.setFont(body_font);

body_style.setAlignment(HorizontalAlignment.CENTER);

body_style.setBorderTop(BorderStyle.THIN);

body_style.setBorderBottom(BorderStyle.THIN);

body_style.setBorderLeft(BorderStyle.THIN);

body_style.setBorderRight(BorderStyle.THIN);

body_style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

body_style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

//1행 컬럼명 설정

row = sheet.createRow(equipment_list_row_no++);

//1행 0번째 제조사

cell = row.createCell(0);

cell.setCellStyle(body_style);

cell.setCellValue("제조사");

//1행 1번째 매입처

cell = row.createCell(1);

cell.setCellStyle(body_style);

cell.setCellValue("매입처");

//1행 2번째 솔루션

cell = row.createCell(2);

cell.setCellStyle(body_style);

cell.setCellValue("솔루션");

//1행 3번째 모델

cell = row.createCell(3);

cell.setCellStyle(body_style);

cell.setCellValue("모델");

//1행 4번째 S/N

cell = row.createCell(4);

cell.setCellStyle(body_style);

cell.setCellValue("S/N");

//1행 5번째 선적일

cell = row.createCell(5);

cell.setCellStyle(body_style);

cell.setCellValue("선적일");

//1행 6번째 입고일

cell = row.createCell(6);

cell.setCellStyle(body_style);

cell.setCellValue("입고일");

//1행 7번째 보관창구

cell = row.createCell(7);

cell.setCellStyle(body_style);

cell.setCellValue("보관창구");

//1행 8번째 사용유무

cell = row.createCell(8);

cell.setCellStyle(body_style);

cell.setCellValue("사용유무");

for(HtmlToEquipmentVO vo:htevo) {

row = sheet.createRow(equipment_list_row_no++);

//제조사

cell = row.createCell(0);

cell.setCellValue(vo.getEquipment_b());

//매입처

cell = row.createCell(1);

cell.setCellValue(vo.getEquipment_c());

//솔루션

cell = row.createCell(2);

cell.setCellValue(vo.getEquipment_d());

//모델

cell = row.createCell(3);

cell.setCellValue(vo.getEquipment_e());

//S/N

cell = row.createCell(4);

cell.setCellValue(vo.getEquipment_f());

//선적일

cell = row.createCell(5);

cell.setCellValue(vo.getEquipment_g());

//입고일

cell = row.createCell(6);

cell.setCellValue(vo.getEquipment_h());

//보관창구

cell = row.createCell(7);

cell.setCellValue(vo.getEquipment_i());

//사용유무

cell = row.createCell(8);

cell.setCellValue(vo.getEquipment_j());

}

** 핵심

Resource resource = null;

try {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

wb.write(bos);

byte[] barray = bos.toByteArray();

InputStream is = new ByteArrayInputStream(barray);

resource = new InputStreamResource(is);

} catch (Exception e) {

e.printStackTrace();

}

return resource;

}

* ByteArrayOutputStream

: 출력하기 위한 OutputStream을 사용하는데 바이트 배열로 선언

* wb.write(bos)

: 바이트배열 출력스트림에 생성한 XSSFWorkbook을 입력한다.

* byte[] barray

: 입력된 XSSFWorkbook을 toByteArray 메소드를 이용하여 byte 배열에 저장한다.

* InputStream

: 입력하기 위한 입력스트림 생성

* new ByteArrayInputStream(barray);

: 바이트 배열을 입력하기 위한 바이트 배열 입력스트림으로 다시 생성

* resource = new InputStreamResource(is);

: 입력 스트림 리소스

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.