首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > ASP >

怎么将SQL数据库里面循环出的内容导出

2012-04-17 
如何将SQL数据库里面循环出的内容导出。!--#include fileinclude/function.asp--%response.charset

如何将SQL数据库里面循环出的内容导出。
<!--#include file="include/function.asp"-->
<%
  response.charset = "GBK"  
 
if request.querystring("action")="toExcel" then  
  Response.ContentType="application/vnd.ms-excel"  
  Response.AddHeader "content-disposition","attachment;filename=aaa.xls"  
end if  
if request.querystring("action")="toWord" then  
  Response.ContentType="application/msword"  
  Response.AddHeader "content-disposition","attachment;filename=aaa.doc"  
end if  
%>
<!--#include file="include/conn.asp"-->

<%
  dim rs,sql
  if request.form("send") = "查询" then
  danwei = trim(request("danwei"))
  nian = trim(request("nian"))
  yue = trim(request("yue"))
  '查询该单位和年月有没有话费。
  set rs = server.createobject("adodb.recordset")
sql = "select * from Bill_hftj where danwei_sid ='"&trim(request("danwei"))&"' and hftj_year='"&trim(request("nian"))&"' and hftj_month='"&trim(request("yue"))&"' "
rs.open sql,conn,1,1
  if rs.eof then
  call sussLocationHref("没有查询的话费!","danwei_user_hfcx.asp")
  call close_rs
  call close_conn
  end if
  end if
  '开始分页
set rs = server.createobject("adodb.recordset")
sql = "select * from Bill_hftj where danwei_sid ='"&trim(request("danwei"))&"' and hftj_year='"&trim(request("nian"))&"' and hftj_month='"&trim(request("yue"))&"' "
rs.open sql,conn,1,1
dim allpage,page
'设置每页多少条
rs.pagesize = 15

'总页数
allpage = rs.pagecount

'总条数
allcount = rs.recordcount

'接收传过来的page页码
page = request.querystring("page")

'一开始进入,没有传page值,那么page就是空值,所以可以设置为第一页1
if page = "" then page = 1

'判断如果不是数字,那么page = 1
if isnumeric(page) then
'如果是0或者负数,那么page还是1 cint是整型,clng是长整形
if clng(page) <1 then page = 1
'如果page大于最大的分页数,那么就是最后一页
if clng(page) > rs.pagecount then page = rs.pagecount
else
page = 1
end if

'当前页码
if not rs.eof then
rs.absolutepage = page
end if
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>V1.0版</title>

</head>
<body>
<table border="1" id="userjfcx">
<tr><th>电话号码</th><th>国内</th><th>区内</th><th>区间</th><th>总金额</th><th>话费年月</th></tr>
<%
  for i = 1 to rs.pagesize
if rs.eof then exit for '如果最后一页不足每面的显示条数,则要退出循环。  
%>
<tr><td><%=rs("Cent_Short")%></td><td><%=rs("Total_guonei")%></td><td><%=rs("Total_qunei")%></td><td><%=rs("Total_qujian")%></td><td><%=rs("Total_money")%></td><td><%=rs("hftj_year")%>-<%=rs("hftj_month")%></td></tr>


<%
  rs.movenext
  next
%>
</table>
<input type="button" name="out_excel" onClick="location.href='?action=toExcel'" value="导出到excel">  
<input type="button" name="out_word" onClick="location.href='?action=toWord'" value="导出到word">  
<!--页码-->
<p style="text-align:center;text-indent:40px;">
共 <strong><%=allcount%></strong> 条话费记录 | 
<%=page%>/<%=allpage%>页 | 
<% if page = 1 then %>
首页 | 
上一页 | 
<% else %>
<a href="danwei_user_hfcx.asp?danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">首页</a> | 
<a href="danwei_user_hfcx.asp?page=<%=page-1 %>&danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">上一页</a> | 
<% end if %>
<%
'比较的时候,传过来的是字符串,所以,要转换成整形
if clng(page) = allpage then
%>
下一页 | 
尾页
<% else %>
<a href="danwei_user_hfcx.asp?page=<%=page+1 %>&danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">下一页</a> | 
<a href="danwei_user_hfcx.asp?page=<%=allpage%>&danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">尾页</a>
<% end if %>
</p>
</body>
</html>


数据库里面循环出的内容没有,只有标题内容。为什么?请高手指点一下。

[解决办法]
那是因为你的action没有引用到下面的需要导出的代码
<!--#include file="include/function.asp"-->
<!--#include file="include/conn.asp"-->
<%
response.charset = "GBK"

if request.querystring("action")="toExcel" then
Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment;filename=aaa.xls"
Call Main()
elseif request.querystring("action")="toWord" then
Response.ContentType="application/msword"
Response.AddHeader "content-disposition","attachment;filename=aaa.doc"
Call Main()
else
Call Main()
end if
%>


<%
Sub Main()
dim rs,sql
if request.form("send") = "查询" then
danwei = trim(request("danwei"))
nian = trim(request("nian"))
yue = trim(request("yue"))
'查询该单位和年月有没有话费。
set rs = server.createobject("adodb.recordset")
sql = "select * from Bill_hftj where danwei_sid ='"&trim(request("danwei"))&"' and hftj_year='"&trim(request("nian"))&"' and hftj_month='"&trim(request("yue"))&"' "
rs.open sql,conn,1,1
if rs.eof then
call sussLocationHref("没有查询的话费!","danwei_user_hfcx.asp")
call close_rs
call close_conn
end if 
end if
'开始分页
set rs = server.createobject("adodb.recordset")
sql = "select * from Bill_hftj where danwei_sid ='"&trim(request("danwei"))&"' and hftj_year='"&trim(request("nian"))&"' and hftj_month='"&trim(request("yue"))&"' "


rs.open sql,conn,1,1
dim allpage,page
'设置每页多少条
rs.pagesize = 15

'总页数
allpage = rs.pagecount

'总条数
allcount = rs.recordcount

'接收传过来的page页码
page = request.querystring("page")

'一开始进入,没有传page值,那么page就是空值,所以可以设置为第一页1
if page = "" then page = 1

'判断如果不是数字,那么page = 1
if isnumeric(page) then
'如果是0或者负数,那么page还是1 cint是整型,clng是长整形
if clng(page) <1 then page = 1
'如果page大于最大的分页数,那么就是最后一页
if clng(page) > rs.pagecount then page = rs.pagecount
else
page = 1
end if

'当前页码
if not rs.eof then
rs.absolutepage = page
end if
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>V1.0版</title>

</head>
<body>
<table border="1" id="userjfcx">
<tr><th>电话号码</th><th>国内</th><th>区内</th><th>区间</th><th>总金额</th><th>话费年月</th></tr>
<%
for i = 1 to rs.pagesize
if rs.eof then exit for '如果最后一页不足每面的显示条数,则要退出循环。
%>
<tr><td><%=rs("Cent_Short")%></td><td><%=rs("Total_guonei")%></td><td><%=rs("Total_qunei")%></td><td><%=rs("Total_qujian")%></td><td><%=rs("Total_money")%></td><td><%=rs("hftj_year")%>-<%=rs("hftj_month")%></td></tr>
<%
rs.movenext
next
%>
</table>
<input type="button" name="out_excel" onClick="location.href='?action=toExcel'" value="导出到excel">
<input type="button" name="out_word" onClick="location.href='?action=toWord'" value="导出到word">
<!--页码-->
<p style="text-align:center;text-indent:40px;">
共 <strong><%=allcount%></strong> 条话费记录 |
<%=page%>/<%=allpage%>页 |
<% if page = 1 then %>
首页 |
上一页 |
<% else %>
<a href="danwei_user_hfcx.asp?danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">首页</a> |
<a href="danwei_user_hfcx.asp?page=<%=page-1 %>&danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">上一页</a> |
<% end if %>
<%
'比较的时候,传过来的是字符串,所以,要转换成整形
if clng(page) = allpage then
%>
下一页 |
尾页
<% else %>
<a href="danwei_user_hfcx.asp?page=<%=page+1 %>&danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">下一页</a> |
<a href="danwei_user_hfcx.asp?page=<%=allpage%>&danwei=<%=trim(request("danwei"))%>&nian=<%=trim(request("nian"))%>&yue=<%=trim(request("yue"))%>">尾页</a>
<% end if %>
</p>
</body>
</html>
<%End Sub%>

热点排行
Bad Request.