ASP Classsic - Insert into Excel .XLS columns or C

2019-05-27 10:35发布

问题:

I would like to add my MS SQL data into the Excel .XLS file below.

I tried the following:

    <% 
    Response.Clear
    Response.Buffer = False
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment;filename=example.xls"

        ' --OPEN MS SQL DATABASE CODE--
        ' --OPEN RECORDSET CODE--
     %>
    <table>  
        <tr>
            <th>DEBNAME</th>
            <th>INV_ID</th>
            <th>INV_DATE</th>
            <th>PO_NO</th>
            <th>INVAMT</th>
        </tr>
    <% 'START OF: ROW LOOP 
      Do Until objRS.EOF
     %>
        <tr>
            <td><%=objRS("DEBNAME")%></td>
            <td><%=objRS("INV_ID")%></td>
            <td><%=objRS("INV_DATE")%></td>
            <td><%=objRS("PO_NO")%></td>
            <td><%=objRS("INVAMT")%></td>
        </tr>
    <%
      objRS.MoveNext
      Loop
      objRS.Close
    'END OF: ROW LOOP
     %>
</table>

THEN WHEN I TRY TO OPEN IT, IT GIVES ME THIS ERROR:

I think this is because the file only has HTML code inside it (I opened it via Notepad to check it out)

If I click on Yes the data will show but I would like to generate a real .xls file or use an empty.xls file, clone it, and then insert the data into it.

THANKS FOR ANY HELP!

This is how my empty.xls file looks

回答1:

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>


回答2:

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
 %>


标签: asp-classic