HTML Table 태그에 내용 넣고 헤더를 고쳐서 엑셀파일인 척해서 다운로드 받기

최상단에 아래 

<%

Response.AddHeader "Content-Disposition","attachment;filename=다운로드 받을 디폴트 파일명.xls"

Response.ContentType = "application/octet-stream"

%>

<table>

<tr>

     <th>컬럼타이틀</th>

</tr>

<tr>

<td>내용</td>

</tr>

</table>



진짜 엑셀파일에 값만 채워넣고 다운로드 받기 - VBScript를 이용해야 한다.

출처: http://stackoverflow.com/questions/24517978/asp-classsic-insert-into-excel-xls-columns-or-create-a-real-excel-format-file

<%@Language=VBScript CodePage=65001%>

<%

    Dim Fso

    Set Fso = Server.CreateObject("Scripting.FileSystemObject")


    Dim emptyXlsFileName

        emptyXlsFileName = Server.Mappath("원본엑셀파일.xlsx") 'empty original file path


    Dim filledXlsFileName

        filledXlsFileName = Replace(Fso.GetTempName, ".tmp", ".xlsx") 'temp file will be created and filled


    Fso.CopyFile emptyXlsFileName, Server.Mappath(filledXlsFileName)

    Dim Connection

    Set Connection = Server.CreateObject("Adodb.Connection") ' 이걸로 해야 오피스가 깔리지 않은 서버에서도 사용 가능하다.

        Connection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.Mappath(filledXlsFileName) & ";Extended Properties=""Excel 12.0;HDR=YES;IMEX=0"";" ' xlsx라 12.0이고 xls면 8.0, 첫열을 타이틀로 할거면 HDR=YES, 아니면 HDR=NO



sql = "select * from [Sheet1$]" ' sheet1 다 불러오기

set objRs = Server.CreateObject("ADODB.Recordset")

objRs.open sql, Connection, 1, 3


rows = 0

do while Not objRs.EOF

if rows = 0 then

objRs.Fields(0).Value = "제목을 제외한 첫번째 줄, 첫번째 컬럼 그러니까 A2"

elseif rows = 1 then 

objRs.Fields(1).Value = "B3"

   objRs.Fields("첫열이 타이틀이었다면 그 컬럼명").Value = "넣을 값"

end if

rows = rows + 1

objRs.MoveNext

loop


objRs.Close

set objRs = Nothing


        Connection.Close

    Set Connection = Nothing


    Const BufferSize = 8192


    Response.ContentType = "application/vnd.ms-excel"

    Response.AddHeader "Content-Disposition", "attachment;filename=다운로드할 디폴트 파일명.xlsx"

    Dim Stm

    Set Stm = Server.CreateObject("Adodb.Stream")

        Stm.Type = 1 'adTypeBinary

        Stm.Open

        Stm.LoadFromFile Server.Mappath(filledXlsFileName)

        Do While Not Stm.EOS

            Response.BinaryWrite Stm.Read(BufferSize)

            Response.Flush

            If Not Response.IsClientConnected Then Exit Do

        Loop

        Stm.Close

    Set Stm = Nothing

    Fso.DeleteFile Server.Mappath(filledXlsFileName)

    Response.End


    Set Fso = Nothing

%>


'Classic ASP' 카테고리의 다른 글

스케쥴러  (0) 2016.09.23
숫자 관련 추가 함수  (0) 2016.06.27
사이트 긁어오기  (0) 2015.08.20
ASP에서 JSON을 쓰려면  (0) 2015.03.06
sleep / delay  (0) 2014.09.12

+ Recent posts