๐ŸŒฟ Spring

[SpringBoot] Apache Poi๋ฅผ ์ด์šฉํ•œ ์—‘์…€ ๋‹ค์šด๋กœ๋“œ ๊ตฌํ˜„

์—ฐ_์šฐ๋ฆฌ 2022. 8. 20. 22:42
๋ฐ˜์‘ํ˜•

๋ชฉ์ฐจ

     

     

     

     

     

    2022.12.03 ๋‚ด์šฉ ์ถ”๊ฐ€


    ์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š” XSSFWorkbook์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์‹ค์ œ ์šด์˜์„œ๋ฒ„์—์„œ ๋‚˜๋Š” Out Of Memory ์—๋Ÿฌ๋ฅผ ๊ฒฝํ—˜ํ–ˆ๋‹ค.
    ํŽธ์•ˆํ•œ ์ฃผ๋ง์„ ๋ณด๋‚ด๊ณ ์‹ถ๋‹ค๋ฉด ๊ผญ ๋ฐ˜๋“œ์‹œ ์•„๋ž˜ ๊ธ€์„ ์ฝ์–ด๋ณด๊ณ  SXSSFWorkbook์„ ์‚ฌ์šฉํ•˜์ž.

    https://lotuus.tistory.com/153

     

    [SpringBoot] Apache Poi๋ฅผ ์ด์šฉํ•œ ์—‘์…€๋‹ค์šด๋กœ๋“œ๋Š” SXSSF๋ฅผ ์“ฐ์ž..!

    ๋ชฉ์ฐจ ์—๋Ÿฌ ๋ฐœ์ƒ!!!!!!!!!!!! https://lotuus.tistory.com/145 [SpringBoot] Apache Poi๋ฅผ ์ด์šฉํ•œ ์—‘์…€ ๋‹ค์šด๋กœ๋“œ ๊ตฌํ˜„ ๋ชฉ์ฐจ ํ†ต๊ณ„ API๋ฅผ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด ํ™”๋ฉด์—๋„ ๊ฐ’์„ ๋ณด์—ฌ์ฃผ์–ด์•ผํ•˜๊ณ , ์—‘์…€ ๋‹ค์šด๋กœ๋“œ๋„ ๊ฐ€๋Šฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค ใ…Ž

    lotuus.tistory.com


     

     

     

     

     

     

     

    ํ†ต๊ณ„ API๋ฅผ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด

    ํ™”๋ฉด์—๋„ ๊ฐ’์„ ๋ณด์—ฌ์ฃผ์–ด์•ผํ•˜๊ณ , ์—‘์…€ ๋‹ค์šด๋กœ๋“œ๋„ ๊ฐ€๋Šฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž

     

    ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Apache Poi๋ฅผ ์ด์šฉํ•˜์—ฌ ์—‘์…€ ๋‹ค์šด๋กœ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹น!

     

     

    ์˜ˆ์ œ

     

    UserPoint Entity

    @Getter
    @Entity
    @NoArgsConstructor(access = AccessLevel.PROTECTED)
    public class UserPoint {
    
        @Id
        @GeneratedValue
        @Column(name = "id")
        private Long id;
    
        @Column(name = "user_code")
        private String userCode;   //์œ ์ € ์ฝ”๋“œ
    
        @Column(name = "user_name")
        private String userName;   //์œ ์ €๋ช…
    
        @Column(name = "pay_cnt")
        private Integer payCnt;    //๊ฒฐ์ œ๊ฑด์ˆ˜
    
        @Column(name = "pay_sum")
        private Long paySum;     //๊ฒฐ์ œ๊ธˆ์•ก
    
    }

     

     

     

    StatsController

    @RestController
    @RequestMapping("/stats")
    @RequiredArgsConstructor
    public class StatsController {
        private final StatsService statsService;
        
        @GetMapping("/user/point")
        public ResponseEntity getUsersPointStats(HttpServletResponse response, boolean excelDownload){
            return ResponseEntity.ok(statsService.getUsersPointStats(response, excelDownload));
        }
    
    }

     

     

     

    StatsService

    @Service
    @Transactional
    @RequiredArgsConstructor
    public class StatsService {
        private final UserPointRepository userPointRepository;
        private final ObjectMapper objectMapper;
    
        public Object getUsersPointStats(HttpServletResponse response, boolean excelDownload) {
    
            List<UserPoint> userPointList = userPointRepository.findAll();
    
            List<Map> userPointMap = userPointList.stream()
                    .map(userPoint -> objectMapper.convertValue(userPoint, Map.class))
                    .collect(Collectors.toList());
    
            return userPointMap;
        }
    }

     

     

     

    API ์‘๋‹ต

     

     

     

     

    ๋ฐ˜์‘ํ˜•

    ์—‘์…€ ๋‹ค์šด๋กœ๋“œ ๊ตฌํ˜„

     

    build.gradle

    //excel download : poi
    implementation 'org.apache.poi:poi:5.2.2'                // .xls ํ™•์žฅ์ž
    implementation 'org.apache.poi:poi-ooxml:5.2.2'          // .xlsx ํ™•์žฅ์ž

     

     

    StatsService

    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.RequiredArgsConstructor;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    import org.springframework.stereotype.Service;
    
    import javax.servlet.http.HttpServletResponse;
    import javax.transaction.Transactional;
    import java.io.IOException;
    import java.net.URLEncoder;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    @Service
    @Transactional
    @RequiredArgsConstructor
    public class StatsService {
        private final UserPointRepository userPointRepository;
        private final ObjectMapper objectMapper;
    
        public Object getUsersPointStats(HttpServletResponse response, boolean excelDownload) {
    
            List<UserPoint> userPointList = userPointRepository.findAll();
    
            if(excelDownload){
                createExcelDownloadResponse(response, userPointList);
                return null; //์—†์œผ๋ฉด ์—๋Ÿฌ!
            }
    
            List<Map> userPointMap = userPointList.stream()
                    .map(userPoint -> objectMapper.convertValue(userPoint, Map.class))
                    .collect(Collectors.toList());
            return userPointMap;
        }
    
    
        /**
         * ์—‘์…€ ๋‹ค์šด๋กœ๋“œ ๊ตฌํ˜„ ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ
         */ 
        private void createExcelDownloadResponse(HttpServletResponse response, List<UserPoint> userPointList) {
    
            try{
                Workbook workbook = new XSSFWorkbook();
                Sheet sheet = workbook.createSheet("์‚ฌ์šฉ์ž ํฌ์ธํŠธ ํ†ต๊ณ„");
    
                //์ˆซ์ž ํฌ๋งท์€ ์•„๋ž˜ numberCellStyle์„ ์ ์šฉ์‹œํ‚ฌ ๊ฒƒ์ด๋‹ค๋‹ค
                CellStyle numberCellStyle = workbook.createCellStyle();
                numberCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));
    
                //ํŒŒ์ผ๋ช…
                final String fileName = "์‚ฌ์šฉ์ž ํฌ์ธํŠธ ํ†ต๊ณ„";
    
                //ํ—ค๋”
                final String[] header = {"๋ฒˆํ˜ธ", "์œ ์ €์ฝ”๋“œ", "์œ ์ €๋ช…", "๊ฒฐ์ œ๊ฑด์ˆ˜", "๊ฒฐ์ œ๊ธˆ์•ก"};
                Row row = sheet.createRow(0);
                for (int i = 0; i < header.length; i++) {
                    Cell cell = row.createCell(i);
                    cell.setCellValue(header[i]);
                }
    
                //๋ฐ”๋””
                for (int i = 0; i < userPointList.size(); i++) {
                    row = sheet.createRow(i + 1);  //ํ—ค๋” ์ดํ›„๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋˜์–ด์•ผํ•˜๋‹ˆ +1
    
                    UserPoint userPoint = userPointList.get(i);
    
                    Cell cell = null;
                    cell = row.createCell(0);
                    cell.setCellValue(userPoint.getId());
    
                    cell = row.createCell(1);
                    cell.setCellValue(userPoint.getUserCode());
    
                    cell = row.createCell(2);
                    cell.setCellValue(userPoint.getUserName());
    
                    cell = row.createCell(3);
                    cell.setCellValue(userPoint.getPayCnt());
    
                    cell = row.createCell(4);
                    cell.setCellStyle(numberCellStyle);      //์ˆซ์žํฌ๋งท ์ ์šฉ
                    cell.setCellValue(userPoint.getPaySum());
                }
    
    
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8")+".xlsx");
                //ํŒŒ์ผ๋ช…์€ URLEncoder๋กœ ๊ฐ์‹ธ์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค!
    
                workbook.write(response.getOutputStream());
                workbook.close();
    
            }catch(IOException e){
                e.printStackTrace();
            }
    
        }
    }

     

     

     

     

    API ์‘๋‹ต

     

    excelDownload = false์ผ๋•Œ๋Š” json ๋ฐ˜ํ™˜

     

     

     

    excelDownload = true์ผ๋•Œ๋Š” ์—‘์…€ ๋‹ค์šด๋กœ๋“œ

    ๊ฒฐ์ œ๊ฑด์ˆ˜, ๊ฒฐ์ œ๊ธˆ์•ก์„ ๋ณด๋ฉด

    ๋ณ„๋‹ค๋ฅธ ์ฒ˜๋ฆฌ ์—†์ด๋„ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ˆซ์ž๋กœ ์ฒ˜๋ฆฌ๋˜์–ด์„œ ๋ฐ”๋กœ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค

    ( setCellValue(userPoint.getPayCnt().toString()) ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์„œ ์—‘์…€ ๋‹ค์šด๋กœ๋“œํ•ด๋ณด๋ฉด ์ˆซ์ž์—ฐ์‚ฐ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค )

     

    ๋˜ํ•œ ๊ฒฐ์ œ๊ธˆ์•ก์— ์ ์šฉํ•œ ์ˆซ์žํฌ๋งท๋„ ์ž˜ ์ ์šฉ๋˜์—ˆ๋‹ค!

     

     

     

     

     

    ํ”„๋ก ํŠธ์ชฝ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœํ•˜์ง€?

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>excel</title>
    </head>
    <body>
    
    <a href="http://localhost:8080/stats/user/point?excelDownload=true">ํฌ์ธํŠธ์ •๋ณด ์—‘์…€๋‹ค์šด๋กœ๋“œ</a>
    
    </body>
    </html>

    aํƒœ๊ทธ href ์†์„ฑ์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด์ฃผ๋ฉด ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•˜๋‹ค!

     

     

     

     

     

    ๋ฐ˜์‘ํ˜•
    • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
    • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
    • ํŠธ์œ„ํ„ฐ ๊ณต์œ ํ•˜๊ธฐ
    • ๊ตฌ๊ธ€ ํ”Œ๋Ÿฌ์Šค ๊ณต์œ ํ•˜๊ธฐ
    • ์นด์นด์˜คํ†ก ๊ณต์œ ํ•˜๊ธฐ