써먹는 웹개발

[Spring] 전자정부프레임워크에서 엑셀 다운로드 본문

Server/Spring

[Spring] 전자정부프레임워크에서 엑셀 다운로드

kmhan 2023. 8. 23. 14:19


728x90
반응형

FileOutputStream 영역에서 자꾸 에러나서 파일을 만들어서 처리했더니 해결됨

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
            List<CompanyVO> companyList = companyMapper.getCompanyList(params);
        
            // .xlsx 파일     
            XSSFWorkbook wb = new XSSFWorkbook();
            // sheet 생성
            Sheet sheet = wb.createSheet("sheet1");
            // create top row column head 생성
            String[] columnHeadings = { "대상 기업명""사업자번호""담당자 이름""담당자 전화번호""담당자 이메일" };
            // bold with a foreground color 지정
            Font headerFont = wb.createFont();
            headerFont.setBold(true);
            headerFont.setFontHeightInPoints((short12);
            headerFont.setColor(IndexedColors.BLACK.index);
            // CellStyle with the font 지정
            CellStyle headerStyle = wb.createCellStyle();
            headerStyle.setFont(headerFont);
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
            // Create the header row
            Row headerRow = sheet.createRow(0);
            // Iterate over the column headings to create columns
            for (int i = 0; i < columnHeadings.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(columnHeadings[i]);
                cell.setCellStyle(headerStyle);
            }
            // Header Row 고정
            sheet.createFreezePane(01);
            // Fill data 
            CreationHelper creationHelper = wb.getCreationHelper();
             CellStyle dateStyle = wb.createCellStyle();
 
            for (int i=0; i < companyList.size(); i++) { 
                companyVO vo = companyList.get(i); 
                Row row = sheet.createRow(i+1); 
                row.createCell(0).setCellValue(vo.getCompany()); 
                row.createCell(1).setCellValue(vo.getComCode()); 
                row.createCell(2).setCellValue(vo.getName()); 
                row.createCell(3).setCellValue(vo.getPhone()); 
                row.createCell(4).setCellValue(vo.getEmail()); 
            } 
 
            // 컬럼 너비 조절 
            for (int i = 0; i <  columnHeadings.length; i++){ 
                // Autosize columns 
                sheet.autoSizeColumn(i); 
                sheet.setColumnWidth(i, (sheet.getColumnWidth(i)) + 1000); 
            }
            
            // 윈도우 테스트 기준 파일저장
            File file = new File("C:\\excelDownload\\test.xlsx");
            // Write the output to file 저장되는 경로
            FileOutputStream fileOut = new FileOutputStream(file);
            wb.write(fileOut);
            wb.close();
            fileOut.close();
cs

 

Maven 환경이면 다음 소스 추가

1
2
3
4
5
6
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
cs

 

참고 : https://yunassnn.tistory.com/28

728x90
반응형


Comments