개발팁2016. 9. 1. 10:29

엑셀 다운로드 구현시 html 의 table 태그로 구성된 파일로 내려받기를 하는경우,

 

ContentType 을 application/vnd.ms-excel
하고, 출력을 하면, 자동으로 엑셀로 열리게 되는데,

 

이때 엑셀에서는, 본문에 있는 셀 데이터 (table 태그) 값에 따라,
자료형을 자동으로 인식하게 된다.

 

000212 이런값이 있을경우, 엑셀에서는 숫자로 인식하여,
앞에 0을 떼어버리고, 212 로 출력을 하게 된다.

 

이러한 현상을 해결하기 위해서는 해당 컬럼이 문자라는것을 인식시켜줄 필요가 있다.

 

<style>
body {
    font-size:12px;
    font-family: 맑은 고딕;
}

table {
    font-size:12px;
    text-align:center;
}
table th {
    text-align:center;
    height:28px;
    background-color: #f0f0f0;
}
table td {
    text-align:left;
    height:26px;
    mso-number-format:"\@";
}

.number {
    text-align:right !important;
    mso-number-format:General !important;
    white-space:nowrap !important;
}
</style>

 

html 파일의 head 태그 사이에, 위와 같이 css 를 정의해보자.
mso-number-format 은 MS Office 에서 숫자 포맷관련 stylesheet 속성이다.

 

 <table border="1">
    <tr>
        <th>발주일</th>
        <th>C/D 번호</th>
        <th>순번</th>
        <th>부품번호</th>
        <th>부품명</th>
        <th>수량</th>
    </tr>
    <tr>
        <td>2016-08-31</td>
        <td>00212</td>
        <td class="number">1</td>
        <td>HGTG5026</td>
        <td>INLAY CARRIER SET FOR MACH2XS 15G</td>
        <td class="number">11</td>
    </tr>
</table>

 

이제 본문을 table th td 로 구성하면, 자동으로 문자로 인식하도록 한다.
단, 실제 숫자로 인식해야 할 곳에는, class="number" 만 지정해주면 된다.

 

 

 

Posted by 헝개