乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > asp.net(C#)如何将查询到的数据导出Excel表格?-asp导出为excel,asp导出数据到excel文件

asp.net(C#)如何将查询到的数据导出Excel表格?-asp导出为excel,asp导出数据到excel文件

作者:乔山办公网日期:

返回目录:excel表格制作


asp导出到excel


在开头加上这一句
Response.ContentType = "application/vnd.ms-excel"
如果导出结果为空白的,那就去掉它
一下是导出Excel代码e5a48de588b6e799bee5baa6e79fa5e98193363
--------------------------------------------------------------------------------
<%
set rs=server.createobject("adodb.recordset")
sql="select * from provinceinfo where 1=1"
rs.open sql,objconn,1,1
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="用户表"  
ExcelBook.WorkSheets(1).cells(2,1).value = "用户编号" 
ExcelBook.WorkSheets(1).cells(2,2).value = "登陆名" 
ExcelBook.WorkSheets(1).cells(2,3).value = "真实姓名" 
ExcelBook.WorkSheets(1).cells(2,4).value = "密码" 
cnt =3 
do while not rs.eof 
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("provinceid") 
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("province") 
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("flag") 
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("id")
rs.movenext 
cnt = cint(cnt) + 1 
loop 
Excelbook.SaveAs "d:\yourfile.xls"    '这个是数据导出完毕以后在D盘存成文件
ExcelApp.Application.Quit     '导出以后退出Excel
Set ExcelApp = Nothing     '注销Excel对象
%>

因你数据量大 另提示EXCEL相关信息

Excel 07-2003一个工作表最多可有65536行,行用数字1—65536表示;最多可有256列,列用英文字母A—Z,AA—AZ,BA—BZ,……,IA—IV表示;一个工作簿中最多含有255个工作表,默认情况下是三个工作表;
Excel 2007及以后版本,一个工作表最多可有1048576行,16384列;

所以建议用2007以上版本的excel



你好,可以根据DataGrid将数据导出到excel的e799bee5baa6e997aee7ad94e58685e5aeb9332方法,原理都是一样的
public void ExportToExcel(Page myPage,DataGrid ctl,string filename)
{
HttpResponse Response;
Response=myPage.Response;

bool CurrCtlVisible=ctl.Visible;
ctl.Visible=true;
Response.AppendHeader("Content-Disposition","attachment;filename="+filename+".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

ctl.Page.EnableViewState = true;
ctl.Visible=CurrCtlVisible;
}
在你显示记录zd集的页面前面加上
<%
nowfilename=replace(replace(replace(now,":","")," ",""),"/","")
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "inline; filename = "&nowfilename&".xls"
%>

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
if request("action")=1 then
Response.ContentType="application/ms-excel"
Response.AddHeader "content-disposition","attachment;filename=/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档7a686964616fe78988e69d83333</title>
<style type="text/css">
table {
border-top:1px solid #003399;
border-left:1px solid #003399;
}
td {
border-right:1px solid #003399;
border-bottom:1px solid #003399;
}
thead {
background-color:#000066;
font-weight:bold;
padding:5px;
color:#FFFFFF;
}
</style>
<script language="javascript">
function tableToExcel(){
location.href='?action=1';
}
</script>
</head>

<body>
<input type="button" value="导出数据" onclick="tableToExcel()" />
<%
ConnStr="..."
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstr

set rs = server.CreateObject("adodb.recordset")
rs.open "select top 10 * from [你的表名]",conn,1,1
if not (rs.eof and rs.bof) then
column = rs.fields.count
response.Write("<table cellpadding='0' cellspacing='0'>")
response.Write("<thead><td>序号</td>")
for each f in rs.fields
response.Write("<td>" & f.name & "</td>")
next
response.Write("</thead>")
for j = 1 to rs.recordcount
if j > 5 then '在第五条的时候隐藏数据,经过测试如果是display为none的数据是不会导出来的
response.Write("<tr style='display:none'>")
else
response.Write("<tr>")
end if
response.Write("<td>" & j & "</td>")
for i = 0 to column - 1
response.Write("<td>" & rs(i) & "</td>")
next
response.Write("</tr>")
rs.movenext
next
response.Write("</table>")
end if
rs.close
set rs = nothing
conn.close
set conn = nothing
%>
</body>
</html>

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网