您的位置:首页 > 编程语言 > C#

c#大圣之路笔记——c# 从DataGrid中导出数据 Session

2016-03-14 17:36 627 查看
///前端代码
<tr>
<td align="right">
<asp:Button ID="btnExport" runat="server" Text="导出错误数据" CssClass="fieldButton" OnClick="btnExport_Click"   style="  height:24px; width:100px"></asp:Button>
</td>
</tr>


///前端代码
<tr>
<td colspan="4">
<asp:DataGrid ID="GridMain" runat="server" Width="100%" AutoGenerateColumns="False"
AllowPaging="True" PageSize="20" AllowSorting="True" DataMember="" OnPageIndexChanged="GridMain_PageIndexChanged"
AlternatingItemStyle-BackColor="#EBE9E9">
<AlternatingItemStyle CssClass="datagridAlternating" ></AlternatingItemStyle>
<HeaderStyle CssClass="tableHead" Height="22" ></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="errorMessage" HeaderText="错误原因">
<ItemStyle HorizontalAlign="Center" Width="8%" ></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="GID" HeaderText="GID">
<ItemStyle HorizontalAlign="Center" Width="4%" ></ItemStyle>
</asp:BoundColumn>

</Columns>
<PagerStyle Position="Top" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>

</td>
</tr>


//后台代码
private DataTable Err_dt;
private string tableName;

//通过把table中的值放在 Session中,绑定到datagrid中
Session[tableName] = Err_dt;

private void bindNextPage(int p)
{
tableName = ViewState["TableName"].ToString();
Err_dt = (DataTable)Session[tableName];
if (ViewState["Sort"].ToString() != "")
{
DataView dv = new DataView(Err_dt);
dv.Sort = ViewState["Sort"].ToString();
GridMain.DataSource = dv;
}
else
{
GridMain.DataSource = Err_dt;
}

GridMain.CurrentPageIndex = p;
GridMain.DataBind();
return;
}

protected void btnExport_Click(object sender, EventArgs e)
{
GridMain.AllowPaging = false;
GridMain.AllowSorting = false;

bindNextPage(0);

Export(GridMain, "ChannelStoreErrorList.xls", "ChannelStoreErrorList", "application/ms-excel");

GridMain.AllowPaging = true;
GridMain.AllowSorting = false;
}
protected void GridMain_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
currentPageIndex = e.NewPageIndex;
this.GridMain.CurrentPageIndex = currentPageIndex;
//DataTable Err_dt = new DataTable();
//Err_dt = BindERData();
GridMain.DataSource = Err_dt;
GridMain.DataBind();
}

#region  Export to the excel

// Export to the excel
private void Export(System.Web.UI.WebControls.DataGrid dg, string fileName, string fn, string typeName)
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType = typeName;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
dg.RenderControl(hw);

//httpResponse.Write(tw.ToString());
//httpResponse.End();
string filePath = Server.MapPath("..") + fn + DateTime.Now.Ticks.ToString() + new Random().Next(100).ToString();
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();
DownFile(httpResponse, fileName, filePath);

httpResponse.End();
}

private bool DownFile(System.Web.HttpResponse Response, string fileName, string fullPath)
{
try
{
Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs = System.IO.File.OpenRead(fullPath);
long fLen = fs.Length;
int size = 102400;//每100K同时下载数据
byte[] readData = new byte[size];//指定缓冲区的大小
if (size > fLen) size = Convert.ToInt32(fLen);
long fPos = 0;
bool isEnd = false;
while (!isEnd)
{
if ((fPos + size) > fLen)
{
size = Convert.ToInt32(fLen - fPos);
readData = new byte[size];
isEnd = true;
}
fs.Read(readData, 0, size);//读入一个压缩块
Response.BinaryWrite(readData);
fPos += size;
}
fs.Close();
FileInfo FI = new FileInfo(fullPath);
if (FI.Exists)
{
FI.Delete();
}
return true;
}
catch
{
return false;
}
}

#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: