본문 바로가기

Spring

[Spring] 게시판 엑셀 다운로드

반응형

목록

1. Controller

2. JSP

3. 엑셀 속성 적용

 

 

 

 

 

 

Controller

@RequestMapping("excel")
    public String excel(@RequestParam Map<String, Object> map, HttpServletResponse response, Model model) throws IOException {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    list = boardService.excel(map);
    model.addAttribute("list", list);
    return "excelView";
}

 

 

 

 

 

JSP

<%@ page language="java" contentType="application/vnd.ms-excel; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    response.setHeader("Content-Disposition", "attachment;filename=" + new  String(("downExcel").getBytes("KSC5601"), "8859_1") + ".xls");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>엑셀 다운로드</title>

// 스타일을 사용하여 엑셀 속성 적용
<style type="text/css">
    .txt {
            mso-number-format:"\@"
    }

    .date {
            mso-number-format:"yyyy\/mm\/dd";
    }
</style>
</head>
<body>
    <table id="table" border="1px">
        <tr>
            <th>순서</th>
            <th>이름(id)</th>
            <th>주제</th>
            <th>내용</th>
            <th>입력날짜</th>
            <th>수정날짜</th>
            <th>방문수</th>
        </tr>
        <c:forEach items="${list}" var="i">
            <tr>
                <td>${i.seq}</td>
                <td class="txt">${i.memName}(${i.memId})</td>
                <td class="txt">${i.boardSubject}</td>
                <td class="txt">${i.boardContent}</td> <td>${i.regDate}</td>
                <td class="date">${i.uptDate}</td>
                <td calss="date">${i.viewCnt}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

 

 

 

 

 

엑셀 속성 적용

사용법

<style type="text/css">
    td {
        mso-number-format:000000;
    }
</style>

or

<td align='center' style='mso-number-format:000000'>

 

000000 : 소수도 여섯자리 정수 (반올림)로 표현된다. 여섯자리 앞의 빈칸은 0으로 채워짐

            1.23 => 000001, 67.67 => 000068

000.000 : 소수자리 세자리까지 (반올림) 표현된다. 앞 뒤 빈칸은 0으로 채워짐

             format은 0.00 인데 숫자가 15.1 인 경우 15.10으로 표현됨

             1.5678 => 001.568

\@ : 셀 형식을 텍스트형으로 표현

        00035.90 인 경우 셀 형식이 숫자형이라면 35.9로 표현되지만 문자형으로 하면 0을 포함하여 보이는 그대로 표현됨

 

 

그 외 mso-number-format 요소들

mso-number-format:"0"                       
        NO Decimals
mso-number-format:"0\.000"                       
        3 Decimals
mso-number-format:"\#\,\#\#0\.000"                       
        Comma with 3 dec
mso-number-format:"mm\/dd\/yy"                       
        Date7
mso-number-format:"mmmm\ d\,\ yyyy"                       
        Date9
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM"                       
        D -T AMPM
mso-number-format:"Short Date"                       
        01/03/1998
mso-number-format:"Medium Date"                       
        01-mar-98
mso-number-format:"d\-mmm\-yyyy"                       
        01-mar-1998
mso-number-format:"Short Time"                       
        5:16
mso-number-format:"Medium Time"                       
        5:16 am
mso-number-format:"Long Time"                       
        5:16:21:00
mso-number-format:"Percent"                       
        Percent - two decimals
mso-number-format:"0%"                         
        Percent - no decimals
mso-number-format:"0\.E+00"                       
        Scientific Notation
mso-number-format:"\@"                         
        Text
mso-number-format:"\#\ ???\/???"                       
        Fractions - up to 3 digits (312/943)
mso-number-format:"\0022£\0022\#\,\#\#0\.00"                       
        £12.76
mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\"                       
        2 decimals, negative numbers in red and signed(1.56   -1.56)

 

 

한 셀 안에서 줄바꿈

<style>   

    .xl24   {mso-number-format:"\@";}   

    br      {mso-data-placement:same-cell;}   

</style> 

 

반응형

'Spring' 카테고리의 다른 글

[Spring] 파일 업로드 & 다운로드  (0) 2021.06.16
[Spring] ajax 사용법  (0) 2021.06.16
[Spring] 게시판 페이징 처리  (2) 2021.06.15
[Spring] 달력 datepicker  (0) 2021.06.15
[Spring] 스프링 게시판 개발을 위한 기본 셋팅  (0) 2021.06.09