ASP Classsic - Insert into Excel .XLS columns or Create a Real Excel Format File

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 07:27:09

Using an empty file that ready to fill is the most painless method.
After filling the sheet(s) via related data providers, you can serve the static excel file or stream it.
Here's the demo : http://aspfiddle.com/ymuikl0id4/test.asp (will be deleted a few days later)
Make sure that you have permission to create new files.
Hope it helps.

<%@Language=VBScript CodePage=65001%>
<%
If Request.Form.Count Then 'form handling
    Dim Fso
    Set Fso = Server.CreateObject("Scripting.FileSystemObject")

    Dim emptyXlsFileName
        emptyXlsFileName = Server.Mappath("test.xls") 'empty original file path

    Dim filledXlsFileName
        filledXlsFileName = Replace(Fso.GetTempName, ".tmp", ".xls") '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.Jet.OLEDB.4.0;Data Source=" & Server.Mappath(filledXlsFileName) & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=0"";"
        'Since Jet.OLEDB.4.0 is a 32-Bit only provider
        'if you need to run this application in a 64-bit environment
        'you have to install 64-bit version of Access Database Engine (http://www.microsoft.com/en-us/download/details.aspx?id=13255) to the server
        'then use the following connection string instead
        'Connection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.Mappath(filledXlsFileName) & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"";"

    Dim Recordset
    Set Recordset = Server.CreateObject("Adodb.Recordset")
        Recordset.Open "[Sheet1$]", Connection, , 3
        Recordset.Addnew
        Recordset("DEBNAME").Value = Request.Form("DEBNAME")
        Recordset("INV_ID").Value = Request.Form("INV_ID")
        Recordset("INV_DATE").Value = Request.Form("INV_DATE")
        Recordset("PO_NO").Value = Request.Form("PO_NO")
        Recordset("INVAMT").Value = Request.Form("INVAMT")
        Recordset.Update
        Recordset.Close
    Set Recordset = Nothing

        Connection.Close
    Set Connection = Nothing

    Const BufferSize = 8192

    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment;filename=example.xls"
    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
End If
%><!doctype html>
<html lang="en">
<head>
    <title>Insert Into Excel File</title>
    <meta charset="utf-8">
</head>
<body>
    <form method="post" target="_blank">
        <table>
            <tr><td>DEBNAME</td><td><input type="text" name="DEBNAME" value="John Doe" /></td></tr>
            <tr><td>INV_ID</td><td><input type="text" name="INV_ID" value="123" /></td></tr>
            <tr><td>INV_DATE</td><td><input type="text" name="INV_DATE" value="24 Jun, 2014" /></td></tr>
            <tr><td>PO_NO</td><td><input type="text" name="PO_NO" value="321" /></td></tr>
            <tr><td>INVAMT</td><td><input type="text" name="INVAMT" value="etc" /></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" value="Add & Download" /></td></tr>
        </table>
    </form>
</body>
</html>

This will only be a partial/hack answer, because for everything from Excel 2007 on, I don't know that you can bypass that warning (unless you use a third-party library, and I don't know of any for ASP classic -- plenty of choices for ASP.NET). Here is a MS article on this subject, and it leads to the hack:

http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

What you can do is alter your code slightly and deliver the data in CSV format. This will still download and open directly in Excel, but you won't get the warning. So here's the guts of it:

<% 
Response.Clear
Response.Buffer = False
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;filename=example.csv"
Response.Write "DEBNAME, INV_ID, INV_DATE, PO_NO, INVAMT" & vbCRLF
Do Until objRS.EOF
    Response.Write objRS("DEBNAME") & ", " & objRS("INV_ID") & ", " & objRS("INV_DATE") & ", " & objRS("PO_NO") & ", " & objRS("INVAMT") & vbCRLF
    objRS.MoveNext
Loop
objRS.Close
 %>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!