SQL数据库的表里面的数据怎么导出到EXCEL里面?
我用的visualstudio2008自带的SQL数据库,用C#,ASP.NET编写的网页,怎么在网页上定义该按钮什么的,能够把数据库里面的信息导出来备份一下。...
我用的visual studio2008自带的SQL数据库,用C#,ASP.NET编写的网页,怎么在网页上定义该按钮什么的,能够把数据库里面的信息导出来备份一下。
展开
4个回答
2011-07-21
展开全部
先将数据绑定到一个控件中,例如gridview:
放一个按钮,代码如下:
protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
{
string FileName = "标题 " + DateTime.Now + ".xls";
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF8;//System.Text
//如果设置GB2312会出现乱码
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.AddHeader("content-type", "application / ms - excel");//设置输出文件的类型
//或Response.ContentType = FileType;
//定义一个输出流
StringWriter tw = new StringWriter();//System.IO
HtmlTextWriter hw = new HtmlTextWriter(tw);
gridview1.RenderControl(hw);//gridview绑定
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//必须写这个;
}
另外,虚机团上产品团购,超级便宜
放一个按钮,代码如下:
protected void ibtnExcel_Click(object sender, ImageClickEventArgs e)
{
string FileName = "标题 " + DateTime.Now + ".xls";
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF8;//System.Text
//如果设置GB2312会出现乱码
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.AddHeader("content-type", "application / ms - excel");//设置输出文件的类型
//或Response.ContentType = FileType;
//定义一个输出流
StringWriter tw = new StringWriter();//System.IO
HtmlTextWriter hw = new HtmlTextWriter(tw);
gridview1.RenderControl(hw);//gridview绑定
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//必须写这个;
}
另外,虚机团上产品团购,超级便宜
展开全部
ASP实例代码,直接将数据库中的数据导出到EXCEL电子表中:
<!--#include file="../conn.asp"-->
<%
dim s,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("users.xls")
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
'Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据查出来
'sql = "select * from Tb_Execl order by id desc"
'rs.Open sql,conn
StartTime = Request("StartTime")
EndTime = Request("EndTime")
StartEndTime = "AddTime between #"& StartTime &" 00:00:00# and #"& EndTime &" 23:59:59#"
strSql = "select * from myeky_myusr "
Set rstData =conn.execute(strSql)
if not rstData.EOF and not rstData.BOF then
dim trLine,responsestr
strLine=""
For each x in rstData.fields
strLine = strLine & x.name & chr(9)
Next
'--将表的列名先写入EXCEL
myfile.writeline strLine
Do while Not rstData.EOF
strLine=""
for each x in rstData.Fields
strLine = strLine & x.value & chr(9)
next
myfile.writeline strLine
rstData.MoveNext
loop
end if
Response.Write "生成EXCEL文件成功,点击<a href=""users.xls"" target=""_blank"">下载</a>!"
rstData.Close
set rstData = nothing
Conn.Close
Set Conn = nothing
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很多方法,给你说一种吧,你可以直接把数据读出来绑定到gridview然后导出,也可以直接用数据源导
BindExportData();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=catiProjectlist.xls");
Response.Charset = "GB2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView2.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
BindExportData();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=catiProjectlist.xls");
Response.Charset = "GB2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView2.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
追问
有没有不用GridView,直接导出的?
追答
///
/// 导出所有的
///
///
///
protected void btnAllExcel_Click(object sender, EventArgs e)
{
List list = diyStoreService.GetAllToExcel();
StringWriter sw = new StringWriter();
sw.WriteLine("code\trgn_nm\tsubrgn_nm\tgrid_cd\tpref_nm\tcity_id\tcity_std_loc_nm\tstor_id\tstor_nm\tstor_tel\tstor_addr\tcat_type_nm\tmall_nm\tmall_addr\trep_nm\trep_tel\trep_email\tfwFeedBacker\tfwFeedBackConter\tfwFeedBackRemark\tfwFeedBackDate\tintelConter\tintelDate\tstatus\tlast_upd_dtm\tisSeccess\tisFWBackFile");
foreach (tb_diy_store diy in list)
{
sw.WriteLine(diy.Code
+ "\t" + diy.Rgn_nm
+ "\t" + diy.Subrgn_nm
+ "\t" + diy.Grid_cd
+ "\t" + diy.Pref_nm
+ "\t" + diy.City_id
+ "\t" + diy.City_std_loc_nm
+ "\t" + diy.Stor_id
+ "\t" + diy.Stor_nm
+ "\t" + diy.Stor_tel
+ "\t" + diy.Stor_addr
+ "\t" + diy.Cat_type_nm
+ "\t" + diy.Mall_nm
+ "\t" + diy.Mall_addr
);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment;filename=DIY-Investigations.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以直接利用sql server 软件,里面就有数据库导出
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询