我想将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>
然后,当我尝试打开它时,它给了我这个错误:
我认为这是因为文件里面只有HTML代码(我通过记事本打开它来查看)
如果我点击是,数据会显示,但我想生成一个真正的.xls文件或使用empty.xls文件,克隆它,然后将数据插入它
感谢任何帮助!
这就是我的empty.xls文件的外观
答案 0 :(得分:2)
使用准备填充的空文件是最无痛的方法
通过相关数据提供者填充工作表后,您可以提供静态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> </td><td><input type="submit" value="Add & Download" /></td></tr>
</table>
</form>
</body>
</html>
答案 1 :(得分:1)
这只是部分/黑客的答案,因为对于Excel 2007上的所有内容,我都不知道您可以绕过该警告(除非您使用第三方库,而且我不知道&#39;知道任何ASP经典 - ASP.NET的很多选择。这是一篇关于这个主题的MS文章,它导致了黑客攻击:
http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx
您可以做的是稍微改变您的代码并以CSV格式提供数据。这仍然会直接在Excel中下载和打开,但您不会收到警告。所以这就是它的内涵:
<%
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
%>