Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

메모장

엑셀 POI 본문

Java

엑셀 POI

doopang 2022. 11. 10. 13:12

Contorller

@GetMapping("/{infoSeq}")
@ApiOperation("엑셀 다운로드")
public void getInfoExcel(HttpServletResponse response, 
    @PathVariable Long infoSeq) throws IOException {
  Workbook workbook = infoService.getInfoExcel(infoSeq);

  try {
    response.setContentType("application/vnd.ms-excel; charset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename=\"info.xlsx\"");
    workbook.write(response.getOutputStream());
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    workbook.close();
  }
}

 

Vue.js
excel.js

import axios from '@/api/axios'

const apiUrlEx = '/info/'

const getInfoExcel = async (infoSeq) => {
  const options = {
    method: 'GET',
    url: apiUrlEx + infoSeq,
    responseType: 'blob'
  }
  await axios(options)
    .then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.data], { type: response.headers['content-type'] }));
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', 'info' + new Date().toJSON().slice(0, 10).replace(/-/g, '.') + '.xlsx');
      document.body.appendChild(link);
      link.click();
    })
    .catch(error => {
      console.log(error);
    })
}

export default {
  getInfoExcel
}

 

Excel Poi

Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet();

// 인쇄 영역 설정
PrintSetup printSetup = sheet.getPrintSetup();
sheet.setFitToPage(true);
printSetup.setFitWidth((short)1);
printSetup.setFitHeight((short)0);

// 전체 열 너비
sheet.setDefaultColumnWidth((short) 10);

// 특정 열 너비
sheet.setColumnWidth((short) 0, (short) 4800);

// 폰트
Font font = workbook.createFont();
font.setFontName("맑은 고딕");
font.setFontHeight((short) 440);
font.setBold(true);

// 표시 형식
XSSFDataFormat dataFormat = (XSSFDataFormat) workbook.createDataFormat();

// 스타일
XSSFCellStyle cetllStyle = (XSSFCellStyle) workbook.createCellStyle();
cellStyle.setWrapText(true); // 자동개행
cellStyle.setIndention((short) 1); // 들여쓰기
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 가운데 맞춤
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 가운데 맞춤
cellStyle.setFont(font); // 폰트 적용
cellStyle.setFillForegroundColor(new XSSFColor(new Color(233, 247, 237))); // 색 적용
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 색 적용
cellStyle.setDataFormat(dataFormat.getFormat("@")); // 표시 형식 적용

// 헤더 생성
Header header = sheet.getHeader();
header.setRight("헤더 생성");

// 행 생성
Row row = sheet.createRow(1);
row.setHeight((short) 530); // 행 높이

// 셀 생성
int idx = 0;
Cell cell = row.createCell(idx);
cell.setCellValue("셀 생성");
cell.setCellStyle(cellStyle);

// 셀 병합 > 병합되는 셀들도 똑같이 생성하고 스타일 지정
for (int i = 1; i < 9; i++) {
  cell = row.createCell(i);
  cell.setCellStyle(style);
}

// 셀 병합
sheet.addMergedRegion(new CellRangeAddress(idx, idx, 0, 8));

// 다음 행
idx++;

'Java' 카테고리의 다른 글

소수점 0 제거  (0) 2022.11.24
JPA @PrePersist, @PreUpdate  (0) 2022.11.22
JPA Entity > Repository  (0) 2022.09.30
JPA Transaction 분리하고 싶을 때  (0) 2022.04.10
JPA delete 다음에 insert 하기  (0) 2022.04.10