C# asp.net 查询 大数据 导出excel!
private void OutPut(string fileType, string strType)
{
this.GridView1.Columns[0].Visible = false;
this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = false;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", fileType);
Response.ContentType = strType;
this.EnableViewState = false;
System.IO.StringWriter swOut = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(swOut);
this.GridView1.RenderControl(htw);
Response.Write(swOut.ToString());
Response.End();
}
protected void Button2_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "save":
switch (this.DropDownList1.SelectedValue)
{
case "excel":
OutPut("attachment;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("attachment;filename=out.doc", "application/ms-word");
break;
}
break;
case "open":
switch (this.DropDownList1.SelectedValue)
{
case "excel":
OutPut("online;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("online;filename=out.doc", "application/ms-word");
break;
}
break;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
目前用的是 显示到 数据控件上 在导出,这种有个弊端,查询出来的数据有上万条,如果显示到gridview上会很慢,即使显示出来,点导出按钮也会出现“无法显示该页”。
有什么方法可以直接选择查询条件,点导出按钮 就直接导出数据,不用显示一次。
还有就是那个 “无法显示该页”怎么解决!
--------------------编程问答-------------------- 可以直接从数据源中导出 --------------------编程问答--------------------
+1
为了避免一次导出过多,也可以使用批量导出,就是类似分页后再导出了 --------------------编程问答-------------------- 要么把页面超时时间设置长一些,或者,还是在数据库直接导吧,毕竟数据量比较大。 --------------------编程问答-------------------- 导出数据和 显示有什么关系??
一个页面 我就放一个textbox 一个查询按钮“导出” 查询数据有5万 导出即可
后台代码是查询 得到一个datetable
跟什么 GridView 显示 一点关系都没有啊??
当然 excel 一个工作薄 最大也就 65536 --------------------编程问答-------------------- datetable 直接导入Excel就可以了。不用绑定GridView了。
如果太大,那么就分页来导入工作薄。 --------------------编程问答-------------------- 多谢大家热心回复,现在都是用2007 如果是 工作表导出数据太多,应该不必担心,我就怕,导出太大是因为页面无法显示而造成。如果能点击按钮 成功弹出 下载 框,那应该就算成功了吧
还有就是重点,数据源导出 代码还是上面 我给的那个代码吗?需要 改哪里?
private void OutPut(string fileType, string strType)
{
this.GridView1.Columns[0].Visible = false;
this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = false;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", fileType);
Response.ContentType = strType;
this.EnableViewState = false;
System.IO.StringWriter swOut = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(swOut);
this.GridView1.RenderControl(htw);
Response.Write(swOut.ToString());
Response.End();
}
protected void Button2_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "save":
switch (this.DropDownList1.SelectedValue)
{
case "excel":
OutPut("attachment;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("attachment;filename=out.doc", "application/ms-word");
break;
}
break;
case "open":
switch (this.DropDownList1.SelectedValue)
{
case "excel":
OutPut("online;filename=out.xls", "application/ms-excel");
break;
case "word":
OutPut("online;filename=out.doc", "application/ms-word");
break;
}
break;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
是这句吗?this.GridView1.RenderControl(htw); --------------------编程问答-------------------- 我都是用apose.cell直接从DataSet直接导到excel的,根本不需要gridview
搜一下apose.cell,下载个试一下就知道了 --------------------编程问答-------------------- 多sheeet操作
分页导出,数组绑定excel
进度条
--------------------编程问答--------------------
还有
<httpRuntime
executionTimeout="800"
maxRequestLength="40960"
useFullyQualifiedRedirectUrl="false"/>
我已经设置超时够长了吧 --------------------编程问答--------------------
如梦哥,能说详细点吗 --------------------编程问答--------------------
#region 导出为Excel--------------------编程问答--------------------
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private void toExcelClk()
{
gvSysUser.AllowPaging = false;
gvSysUser.AllowSorting = false;
gvSysUser.DataBind();
ToExcel(gvSysUser, "OFS_Data.xls");
gvSysUser.AllowPaging = true;
gvSysUser.AllowSorting = true;
gvSysUser.DataBind();
}
#endregion
大哥,你这个还是 绑定了 数据源到 gridview上啊。我就是用的你这个方法
gvSysUser.DataBind();
ToExcel(gvSysUser, "OFS_Data.xls");
--------------------编程问答-------------------- --------------------编程问答-------------------- 学习了。。 --------------------编程问答-------------------- 坐等最快的性能最好的方法 --------------------编程问答-------------------- 真的没人 帮忙了吗????????? --------------------编程问答-------------------- 你用datatable直接輸出Excel就可以了,
例如:
--------------------编程问答-------------------- 导出大数据量要考虑效率问题!看看这里吧!我也在弄!
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=EmailList.xls");
string colHeaders = "", ls_item = "";
DataSet ds = EmailListDS("sql");
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
string[,] head ={
{"a","Bound back date"},
{"b","Bound back email"},
{"c","Campaign Name"},
};
for (i = 0; i < head.GetLength(0); i++)
{
if (i == cl - 1)//最后一列,加n
{
colHeaders += head[i, 1].ToString() + "\n";
}
else
{
colHeaders += head[i, 1].ToString() + "\t";
}
}
resp.Write(colHeaders);
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += (row[i] == System.DBNull.Value ? "" : row[i].ToString()) + "\n";
}
else
{
ls_item += (row[i] == System.DBNull.Value ? "" : row[i].ToString()) + "\t";
}
}
resp.Write(ls_item.Replace("\r\n", ""));
ls_item = "";
}
string FootCount = "";
resp.Write(FootCount);
resp.End();
http://hi.baidu.com/lsf19880820/blog/item/95600d2f0450905c4ec22643.html
补充:.NET技术 , ASP.NET