asp-classic - ASP Classic - 插入 Excel .XLS 列或创建真正的 Excel 格式文件

标签 asp-classic

我想将我的 MS SQL 数据添加到下面的 Excel .XLS 文件中。

我尝试了以下方法:

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

然后当我尝试打开它时,它给出了这个错误: ERROR MESSAGE

我认为这是因为该文件中只有 HTML 代码(我通过记事本打开它来查看)

如果我点击,数据将会显示,但我想生成一个真正的 .xls 文件或使用一个空的 .xls 文件,克隆它,然后将数据插入到它。

感谢您的帮助!

这就是我的空.xls 文件的样子 Example of XLS File

最佳答案

使用准备填充的空文件是最轻松的方法。
通过相关数据提供者填写工作表后,您可以提供静态 Excel 文件或流式传输。
这是演示:http://aspfiddle.com/ymuikl0id4/test.asp (几天后将被删除)
确保您有权创建新文件。
希望能帮助到你。

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

关于asp-classic - ASP Classic - 插入 Excel .XLS 列或创建真正的 Excel 格式文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24517978/

相关文章:

session - 如何在经典 ASP 中获取当前 session 的身份验证 cookie 的名称

mysql - MediumText 和 ASP/MySQL 的数据损坏

unit-testing - 自动测试经典 ASP

c# - 使用 C# 如何从经典 ASP 内部 Request.QueryString 转换为 NameValueCollection?

jquery - HighCharts 饼图图例值对齐

asp-classic - 经典 ASP #include 虚拟不工作

iphone - ASP 页面和 Cocoa/ObjC 之间的安全通信

xml - xpath中有split函数吗?

javascript - 使用 VBScript Classic ASP 页面中的 JavaScriptalert()

sql-server-2008 - 无法使用 SQL Native Client (Windows 7 - IIS7) 从经典 ASP 连接到 SQL Server 2008 R2