asp.net c# 查询数据库 按钮导出Excel 放在桌面上 代码是怎样的
asp.net c# 查询数据库 按钮导出Excel 放在桌面上 代码是怎样的 --------------------编程问答-------------------- 有很多方式,可以自己写代码实例化EXCEL对象实现,如果用developer express控件里面的GRIDVIEW,就很简单。以下附上常见的几种导出方式连接,仅供参考:
http://blog.csdn.net/jilm168/article/details/1869118
http://www.cnblogs.com/springyangwc/archive/2011/08/12/2136498.html --------------------编程问答-------------------- Refer:
http://www.cnblogs.com/insus/articles/1400266.html --------------------编程问答-------------------- 第一步:前提是你已经用一个方法取得了一张表,假设这个方法为GetTable(string m,string n...)
aspx里这么写<input type="button" class="btn_Excel" onclick="OutPut('Excel');" />这个是按钮
<iframe width="0" height="0" id="export_page" name="export_page"></iframe>这句放在<form>后面
接下来添加JS:<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
//excel导出
function OutPut(t) {
var a = "<%=查询条件1.Trim()%>";
var b = "<%=查询条件2.SelectedValue%>";
...
document.all("export_page").src = "新建导出页面.aspx?Outtype=" + t + "&A=" + a+ "&B=" + b...;
}
</script>
</telerik:RadScriptBlock>
第二步:新建导出页面
页面aspx:
<body>
<meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8"/>//防止乱码
<form id="form1" runat="server">
<div>
<table style="width: 100%;">
<tr>
<td>列名1</td><td>列名2</td>
</tr>
<asp:PlaceHolder ID="pl_Operation" runat="server"></asp:PlaceHolder>
</table>
</div>
</form>
</body>
后台CS:
protected void Page_Load(object sender, EventArgs e)
{
string Outtype = Request["Outtype"] != null ? Request["Outtype"].ToString() : "";
string a= Request["A"] != null ? Request["A"].ToString() : "";
string b = Request["B"] != null ? Request["B"].ToString() : "";
DataTable dt = ?.GetTable(a,b,...);//方法参数为查询条件,GetTable方法并不在同一个命名空间以及类里面,先实例化一下,?处填写实例
if (null != dt && dt.Rows.Count > 0)
{
for (var i = 0; i < dt.Rows.Count; i++)
{
var dr = dt.Rows[i];
if (null != dr && dr.ItemArray.Length > 0)
{
var tr = new System.Web.UI.HtmlControls.HtmlTableRow();
var td = new System.Web.UI.HtmlControls.HtmlTableCell()
{
InnerText = dr["列名1"].ToString()//注意,列名需要和数据库里的列名一致
};
tr.Cells.Add(td);
td = new System.Web.UI.HtmlControls.HtmlTableCell()
{
InnerText = dr["列名2"].ToString()
};
tr.Cells.Add(td);
...
pl_Operation.Controls.Add(tr);
}
}
}
ExportData export = new ExportData();
export.ExportControl(this, Outtype, "table1");//table1即为导出excel名,随便起
}
最后注意引入命名空间:
usiusing System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
... --------------------编程问答-------------------- 不需要额外的控件,如GRIDVIEW等,只需用按钮,通过查询数据库,导出Excel文件在桌面上的
补充:.NET技术 , ASP.NET