๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ

[์Šคํ”„๋ง] ์—‘์…€ ๋‹ค์šด๋กœ๋“œ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ• (์˜์กด์„ฑ/JSP/์ปจํŠธ๋กค๋Ÿฌ)

by OR15A 2024. 1. 4.
์˜์กด์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ
  • ์—‘์…€ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์œ„ํ•œ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค(maven - pom.xml)
        <!-- ์—‘์…€ ๋‹ค์šด๋กœ๋“œ ์˜์กด์„ฑ -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

 

[A]  : ์ž๋ฐ”๋ฅผ ์ด์šฉํ•œ ์—‘์…€ ์ƒ์„ฑ

[B]  : JSP์˜ <Table> ํƒœ๊ทธ๋ฅผ ๋ฐ”๋กœ Excel ๋ณ€ํ™˜

 

 

 

[A] ๊ธฐ์กด์˜ JPS์— ํƒœ๊ทธ ์ถ”๊ฐ€ํ•˜๊ธฐ
  • ์ œ์ถœ์„ ์œ„ํ•œ form ํƒœ๊ทธ์—์„œ action์†์„ฑ ๊ฐ’์€ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์œ„ํ•ด ๋งŒ๋“ค์–ด๋‘” ์ปจํŠธ๋กค๋Ÿฌ์˜ GetMapping ์ฃผ์†Œ๋กœ, method๋Š” Get์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. 
  • ๋‹ค์šด๋กœ๋“œํ•  ํŽ˜์ด์ง€์— ๊ทธ์™ธ์˜ ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ œ์ถœํ•  ํƒœ๊ทธ๋“ค์„ formํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ค€๋‹ค.
  • ์ œ์ถœ์„ ์œ„ํ•œ submit  ํƒ€์ž…์˜ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
<form action="/excel/download" method="get">

    <!--๋‹ค์šด๋ฐ›์„ ์ž๋ฃŒ๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๊ฐ’-->
    <select id="searchType">
        <option value="none">์„ ํƒ</option>
        <option value="writer">์ž‘์„ฑ์ž</option>
        <option value="subject">์ œ๋ชฉ</option>
        <option value="subAndContent">์ œ๋ชฉ+๋‚ด์šฉ</option>
    </select>
    <input type="search" value="" id="keyword">
    <input type="date" id="startDate">
    <input type="date" id="endDate">
    <input type="button" value="๊ฒ€์ƒ‰" id="searchBtn">

    <button type="submit" name="excelBtn" id="excelBtn">Excel</button>

</form>

 

 

 

[A] ์ปจํŠธ๋กค๋Ÿฌ ์ƒ์„ฑ
  • ์ž๋ฐ”์—์„œ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€๋งŒ, ๊ทธ๋งŒํผ ์ปค์Šคํ…€์„ ๋งˆ์Œ๊ป ํ•  ์ˆ˜ ์žˆ์Œ
  • Workbook์„ ์ด์šฉํ•จ (ํ—ค๋” ๋งŒ๋“ค๊ณ  ๊ทธ ์•„๋ž˜๋กœ ๋กœ์šฐ ์ถ”๊ฐ€ํ•˜๊ธฐ)
    
    @GetMapping("/excel/download")
    public void excelDownload(HttpServletResponse response,
                              @RequestParam(value = "searchType", required = false) String searchType,
                              @RequestParam(value = "keyword", required = false) String keyword,
                              @RequestParam(value = "startDate", required = false) LocalDateTime startDate,
                              @RequestParam(value = "endDate", required = false) LocalDateTime endDate)
            throws IOException {
            
            
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("BoardList"); //์‹œํŠธ ์ด๋ฆ„(์—‘์…€ ํ•˜๋‹จ ๋ถ€๋ถ„)
        Row row = null;
        Cell cell = null;
        int rowNum = 0;

        //์—‘์…€ Header ๋งŒ๋“ค๊ธฐ
        row = sheet.createRow(rowNum++);
        
        cell = row.createCell(0);
        cell.setCellValue("๊ธ€๋ฒˆํ˜ธ");
        cell = row.createCell(1);
        cell.setCellValue("์ž‘์„ฑ์ž(ID)");
        cell = row.createCell(2);
        cell.setCellValue("์ œ๋ชฉ");
        cell = row.createCell(3);
        cell.setCellValue("์ž‘์„ฑ์ผ");
        cell = row.createCell(4);
        cell.setCellValue("์ˆ˜์ •์ผ");
        cell = row.createCell(5);
        cell.setCellValue("์กฐํšŒ์ˆ˜");


		/*  ์ถœ๋ ฅํ•  ๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ  */
        //์ž…๋ ฅ๊ฐ’ ์ฒ˜๋ฆฌ
        if (searchType == null) searchType = "";
        if (keyword == null) keyword = "";
        if (startDate == null) startDate = LocalDateTime.of(1981, Month.JANUARY, 1, 0, 0);
        if (endDate == null) endDate = LocalDateTime.now();
		//๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ƒ์„ฑ
        SearchDto searchDto = new SearchDto(searchType, keyword, startDate, endDate);
        //๊ฒŒ์‹œํŒ ๊ธ€ ๋ฆฌ์ŠคํŠธ ๋ฐ›์•„์˜ค๊ธฐ(ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ์—†๋Š” Mapper์ด์šฉ)
        List<BoardDto> boards = boardService.getSearchBoardForExcel(searchDto);

        //LocalDateTime ์ถœ๋ ฅ์„ ์œ„ํ•œ ๋‚ ์งœ ํ˜•์‹ ์ง€์ •(์ง€์ • ์•ˆํ•˜๋ฉด ์ˆซ์ž๋‚˜์˜ด...)
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");


        //์—‘์…€ Header ์•„๋ž˜์— Row ๋งŒ๋“ค๊ธฐ
        for (BoardDto board : boards) {
        	//๋กœ์šฐ ์ถ”๊ฐ€
            row = sheet.createRow(rowNum++);
            //๊ฐ ์…€๋งˆ๋‹ค ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ž…๋ ฅ
            cell = row.createCell(0);
            cell.setCellValue(board.getSeq());
            cell = row.createCell(1);
            cell.setCellValue(board.getMem_name() + "(" + board.getMem_id() + ")");
            cell = row.createCell(2);
            cell.setCellValue(board.getBoard_subject());
            cell = row.createCell(3);
            cell.setCellValue(board.getBoard_content());
            cell = row.createCell(4);
            cell.setCellValue(board.getReg_date().format(formatter));
            cell = row.createCell(5);
            cell.setCellValue((board.getView_cnt() == null) ? 0 : Integer.parseInt(board.getView_cnt()));
        }

        // ์ปจํ…์ธ  ํƒ€์ž… ์ง€์ • 
        response.setContentType("ms-vnd/excel");
		//ํŒŒ์ผ๋ช… ์ง€์ •
        response.setHeader("Content-Disposition", "attachment;filename = boardList.xlsx"); //์›ํ•˜๋Š” filename ์ง€์ •

        // Excel File Output
        wb.write(response.getOutputStream());
    }


}

 

 


 

 

[B] Excel ๋‹ค์šด๋กœ๋“œ์šฉ JSP์ƒ์„ฑ
  • body๋งŒ ์žˆ๋Š” boardListExcel.JSP๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค
  • `<%@ page language="java" contentType="application/vnd.ms-excel;charset=UTF-8" pageEncoding="UTF-8"%>`
    • `language="java"`(Java ์–ธ์–ด๋ฅผ ์‚ฌ์šฉ ์ง€์ •)
    • `contentType="application/vnd.ms-excel"`(์ด ํŽ˜์ด์ง€์˜ ์ฝ˜ํ…์ธ  ์œ ํ˜•์„ ์—‘์…€ ํŒŒ์ผ๋กœ ์ง€์ •, ํŽ˜์ด์ง€ ์ถœ๋ ฅ์„ ์—‘์…€ํŒŒ์ผ๋กœ)
    • `charset=UTF-8`(๋ฌธ์ž ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ UTF-8๋กœ ์„ค์ •)
    • `pageEncoding="UTF-8"`(JSP ํŽ˜์ด์ง€ ์ž์ฒด์˜ ์ธ์ฝ”๋”ฉ์„ UTF-8๋กœ ์„ค์ •)
  • `<% response.setHeader("Content-Disposition","attachment;filename=ExcelDowndload.xls"); %>`
    • HTTP ์‘๋‹ต ํ—ค๋”๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    • `Content-Disposition`(๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์ฝ˜ํ…์ธ  ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ• ์•Œ๋ ค์คŒ)
    • `attachment`์„ค์ •(ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๋ผ๋Š” ์ง€์‹œ)
    • `filename=ExcelDowndload.xls`(๋‹ค์šด๋กœ๋“œ๋  ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์ง€์ •)
  • `<% response.setHeader("Content-Description", "JSP Generated Data"); %>`
    • `Content-Description`(์‘๋‹ต์˜ ๋‚ด์šฉ์„ ์„ค๋ช…, "JSP Generated Data" : ๋ฐ์ดํ„ฐ๊ฐ€ JSP ํŽ˜์ด์ง€์— ์˜ํ•ด ์ƒ์„ฑ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ„)

<%@ page contentType="application/vnd.ms-excel; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.time.format.DateTimeFormatter" %>

<%@ page language="java" contentType="application/vnd.ms-excel;charset=UTF-8" pageEncoding="UTF-8"%>

<%
response.setHeader("Content-Disposition","attachment; filename = ExcelDownload.xls");
%>

<body>

<table border="1">
    <thead>
        <tr>
            <th class="fixed-col-ss">๊ธ€๋ฒˆํ˜ธ</th>
            <th class="fixed-col-m">์ž‘์„ฑ์ž(ID)</th>
            <th class="fixed-col-title">์ œ๋ชฉ</th>
            <th class="fixed-col-s">์ž‘์„ฑ์ผ</th>
            <th class="fixed-col-s">์ˆ˜์ •์ผ</th>
            <th class="fixed-col-ss">์กฐํšŒ์ˆ˜</th>
        </tr>
    </thead>
    <tbody id="tbodyId">
        <!-- List๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ index๋ฅผ ์‚ฌ์šฉ -->

        <c:forEach items="${boardList}" var="board" varStatus="status">
            <tr name="trBoard${status.index}" id="trBoard${status.index}">
                <td>${board.getSeq()}</td>
                <td>${board.getMem_name()}(${board.getMem_id()})</td>
                <td>${board.getBoard_subject()}</td>
                <td>${board.getReg_date().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))}</td>
                <td>${board.getUpt_date().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))}</td>
                <td>${board.getView_cnt()}</td>
            </tr>
        </c:forEach>


    </tbody>
</table>

</body>

 

 

[B] ์ปจํŠธ๋กค๋Ÿฌ ์ƒ์„ฑ
  • A๋ฒˆ ๋ฐฉ๋ฒ•๋ณด๋‹ค ๊ฐ„ํŽธํ•จ
  • ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•˜๋Š” List๋ฅผ Model๋กœ ์ „๋‹ฌ(JSP์—์„œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ…Œ์ด๋ธ” ํ˜•์„ฑ ๊ณ„ํš)
    @GetMapping("/excel/download")
    public String jspExcelDownload(HttpServletResponse response,
                              @RequestParam(value = "searchType", required = false) String searchType,
                              @RequestParam(value = "keyword", required = false) String keyword,
                              @RequestParam(value = "startDate", required = false) LocalDateTime startDate,
                              @RequestParam(value = "endDate", required = false) LocalDateTime endDate,
                              Model model)

            throws IOException {

        //์ž…๋ ฅ๊ฐ’ ์ฒ˜๋ฆฌ
        if (searchType == null) searchType = "";
        if (keyword == null) keyword = "";
        if (startDate == null) startDate = LocalDateTime.of(1981, Month.JANUARY, 1, 0, 0);
        if (endDate == null) endDate = LocalDateTime.now();

        SearchDto searchDto = new SearchDto(searchType, keyword, startDate, endDate);
        List<BoardDto> boardList = boardService.getSearchBoardForExcel(searchDto);
        
        model.addAttribute("boardList",boardList);

        return "boardListExcel";
    }