我正试图将HTML表导出到excel,但不幸的是,我无法执行。
我有一个表,它是根据以前的页面筛选表单的结果从数据库表中填充的。这个表运行良好,筛选良好,在表中显示输出。
现在,当我尝试让它通过ASP下载表时,它会下载整个SQL表。我再次尝试以隐藏的形式存储结果,并通过它进行下载;不起作用。当它移动到下一页时,它会丢失每个字段的数据,而占用整个SQL表。
我一直在使用Response.ContentType和Response.AddHeader,因为我知道这就是导出到excel.csv的方式。
如果有人能帮我,让我在下载开始时点击超链接/按钮,就可以在同一个页面上看到这个,那就太棒了。
编辑:
<tr>
<td name="StationReference"><a href="Inventory_details.asp?Name=<%=statRS("StationReference")%>&InventoryID=<%=statRS("InventoryID")%>"><%=statRS("StationReference")%></a></td>
<td name="RepairStatus"><%=statRS("RepairStatus")%></td>
<td name="SSD"><%=statRS("SSD")%></td>
<td name="Motherboard"><%=statRS("Motherboard")%></td>
<td name="PSU"><%=statRS("PSU")%></td>
<td name="NetworkLogin"><%=rsGlobalUsers("NetworkLogin")%></td>
<td name="RepairDate"><%=statRS("RepairDate")%></td>
<td name="Location"><%=statRS("LocationName")%></td>
<td align="center"><a href="repairsEdit.asp?InventoryID=<%=statRS("InventoryID")%>"><img src="../Images/Icon_edit.gif" align="absmiddle" alt="Edit" /></a></td>
</tr>
上面的代码显示了进行过滤/搜索后的表格
以下代码是隐藏形式
<form id="hiddenDownload" name="hiddenDownload" action="downloadTable.asp" method="post">
<input type="hidden" name="TheSearch" value="<%=statRS("InventoryID")%>">
<input type="hidden" name="StationReference" value="<%=statRS("StationReference")%>">
<input type="hidden" name="RepairStatus" value="<%=statRS("RepairStatus")%>">
<input type="hidden" name="SSD"value="<%=statRS("SSD")%>">
<input type="hidden" name="Motherboard" value="<%=statRS("Motherboard")%>">
<input type="hidden" name="PSU" value="<%=statRS("PSU")%>">
<input type="hidden" name="rtRepairedBy" value="<%=rsGlobalUsers("NetworkLogin")%>">
<input type="hidden" name="rtRepairDate"value="<%=statRS("RepairDate")%>">
<input type="hidden" value="<%=statRS("LocationName")%>">
<input type="submit" value="Download">
</form>
您需要精确地删除Response.ContentType,以便显示表格格式。实际的html表看起来更重要,因为只有IE才能用这个命令以最佳方式工作。如评论7所述:
https://bugzilla.mozilla.org/show_bug.cgi?id=319428#c7
如果您刚刚将列设置为隐藏显示,则它们仍将显示。Response.ContentType仅适用于internet explorer。对于多个浏览器,您应该研究其他方法来生成excel报告。如果你需要支持其他浏览器,你需要确保你的页面上只有表格。任何内容都不应隐藏或以其他方式更改为不显示。
编辑:根据意见更新
第2页和第3页应该彼此相同,而不仅仅是相似。如果你不能在没有更改的情况下直接复制页面,那么就有问题了。下面的例子应该说明这一点:
第1页:
<Form id="form1" action="page2.asp" method="post">
Sell Location: <input type="text" name="sellLocation"><br />
<input type="submit">
</form>
第2页:
<%
sLoc = Request("sellLocation")
Set objCmd = CreateObject("ADODB.Command")
Set objCmd.ActiveConnection = oConn
objCmd.CommandType = 4 'Stored Proc
objCmd.CommandText = "YourStoredProcedure"
objCmd.Parameters.Refresh
objCmd.Parameters("sellLocation") = sLoc
Set obj_rst = objCmd.Execute
%>
<Form id="form1" action="page3.asp" method="post">
<input type="hidden" name="sellLocation"><br />
<input type="submit" value="download">
</form>
<tr>
<td name="StationReference"><a href="Inventory_details.asp?Name=<%=statRS("StationReference")%>&InventoryID=<%=statRS("InventoryID")%>"><%=statRS("StationReference")%></a></td>
<td name="RepairStatus"><%=statRS("RepairStatus")%></td>
<td name="SSD"><%=statRS("SSD")%></td>
<td name="Motherboard"><%=statRS("Motherboard")%></td>
<td name="PSU"><%=statRS("PSU")%></td>
<td name="NetworkLogin"><%=rsGlobalUsers("NetworkLogin")%></td>
<td name="RepairDate"><%=statRS("RepairDate")%></td>
<td name="Location"><%=statRS("LocationName")%></td>
<td align="center"><a href="repairsEdit.asp?InventoryID=<%=statRS("InventoryID")%>"><img src="../Images/Icon_edit.gif" align="absmiddle" alt="Edit" /></a></td>
</tr>
第3页:
<%
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", " filename=yourspreadsheetname.xls"
sLoc = Request("sellLocation")
Set objCmd = CreateObject("ADODB.Command")
Set objCmd.ActiveConnection = oConn
objCmd.CommandType = 4 'Stored Proc
objCmd.CommandText = "YourStoredProcedure"
objCmd.Parameters.Refresh
objCmd.Parameters("sellLocation") = sLoc
Set obj_rst = objCmd.Execute
%>
<tr>
<td name="StationReference"><a href="Inventory_details.asp?Name=<%=statRS("StationReference")%>&InventoryID=<%=statRS("InventoryID")%>"><%=statRS("StationReference")%></a></td>
<td name="RepairStatus"><%=statRS("RepairStatus")%></td>
<td name="SSD"><%=statRS("SSD")%></td>
<td name="Motherboard"><%=statRS("Motherboard")%></td>
<td name="PSU"><%=statRS("PSU")%></td>
<td name="NetworkLogin"><%=rsGlobalUsers("NetworkLogin")%></td>
<td name="RepairDate"><%=statRS("RepairDate")%></td>
<td name="Location"><%=statRS("LocationName")%></td>
<td align="center"><a href="repairsEdit.asp?InventoryID=<%=statRS("InventoryID")%>"><img src="../Images/Icon_edit.gif" align="absmiddle" alt="Edit" /></a></td>
</tr>
请注意,在第2页和第3页之间,我所要做的就是删除表单并添加response.contenttype指令。现在你似乎把值传递到页面的方式,我想这种格式是不可能的。