์์กด์ฑ ์ถ๊ฐํ๊ธฐ
- ์์ ๋ค์ด๋ก๋๋ฅผ ์ํ ์์กด์ฑ์ ์ถ๊ฐํ๋ค(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";
}