用ASP如何实现从数据库中读取数据并导出EXCEL
发布网友
发布时间:2022-04-09 04:42
我来回答
共5个回答
懂视网
时间:2022-04-09 09:04
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace Login
{
class Program
{
static void Main(string[] args)
{
//新建一个数据库连接
using(SqlConnection conn = new SqlConnection(GetConnectString()))
{
conn.Open();//打开数据库
//Console.WriteLine("数据库打开成功!");
//创建数据库命令
SqlCommand cmd = conn.CreateCommand();
//创建查询语句
cmd.CommandText = "SELECT * FROM userinfo";
//从数据库中读取数据流存入reader中
SqlDataReader reader = cmd.ExecuteReader();
//从reader中读取下一行数据,如果没有数据,reader.Read()返回flase
while (reader.Read())
{
//reader.GetOrdinal("id")是得到ID所在列的index,
//reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回
//reader.GetString(int n)这是将第n列的数据以string 格式返回
int id = reader.GetInt32(reader.GetOrdinal("id"));
string name = reader.GetString(reader.GetOrdinal("name"));
string pwd = reader.GetString(reader.GetOrdinal("password"));
int age = reader.GetInt32(reader.GetOrdinal("age"));
string sex = reader.GetString(reader.GetOrdinal("sex"));
string phone = reader.GetString(reader.GetOrdinal("phone"));
string address = reader.GetString(reader.GetOrdinal("Address"));
//格式输出数据
Console.Write("ID:{0},Name:{1},PWD:{2},Age:{3},Sex:{4},Phone{5},Address:{6}
", id, name, pwd, age, sex, phone, address);
}
}
Console.ReadKey();
}
//得到一个数据库连接字符串
static string GetConnectString()
{
return "Data Source=(local);Initial Catalog=db1;Integrated Security=SSPI;";
}
}
}
从数据库中读出数据并输出
标签:
热心网友
时间:2022-04-09 06:12
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
on error resume next
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "inline; filename = 楼盘团购报名表"&cstr(date())&".xls"
dim rs,sql, sCount
sql = "SELECT * FROM [LouPan_Buy] ORDER BY ItemName ASC, ID DESC"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
sCount = rs.recordcount
%>
热心网友
时间:2022-04-09 07:30
可以使用visual studio developer 2005 速成版。
至于代码嘛,找个教程学一下就行
热心网友
时间:2022-04-09 09:04
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="../inc/conn.asp"-->
<%
on error resume next
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "inline; filename = 楼盘团购报名表"&cstr(date())&".xls"
dim rs,sql, sCount
sql = "SELECT * FROM [LouPan_Buy] ORDER BY ItemName ASC, ID DESC"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
sCount = rs.recordcount
%>
<!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>导出团购报名表</title>
</head>
<body>
<h1 align="center">-楼盘团购报名表-</h1>
<h3 align="center">内蒙古房产网 www.nmgfc.cn</h3>
<table width="100%">
<tr>
<td align="left">【报名总人数】:<%=sCount%></td>
<td align="right">【制表时间】:<%=NOW()%></td>
</tr>
</table>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<th width="6%"> 序号</th>
<th width="6%">姓名</th>
<th width="6%">性别</th>
<th width="12%">团购楼盘</th>
<th width="8%">预购价格</th>
<th width="8%">手机</th>
<th width="8%">电话</th>
<th width="12%"> 电子邮件</th>
<th width="9%">QQ号码</th>
<th width="11%">报名时间</th>
<th width="11%">IP地址 </th>
</tr>
<%do while not rs.eof%>
<tr>
<td><%=rs("ID")%></td>
<td><%=rs("Name")%></td>
<td><%=rs("Sex")%></td>
<td><%=rs("ItemName")%></td>
<td><%=rs("Price")%></td>
<td><%=rs("Mobil")%></td>
<td><%=rs("Phone")%></td>
<td><%=rs("Email")%></td>
<td><%=rs("QQ")%></td>
<td><%=rs("Time")%></td>
<td><%=rs("IP")%></td>
</tr>
<%rs.movenext
loop
rs.close
set rs=nothing%>
</table>
</body>
</html>
热心网友
时间:2022-04-09 10:56
简单的,网上有很多源码