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
相关文章推荐
- C#动态排序
- [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- C#有关日期的使用方法
- C#时间格式化
- Winform(C#)——子窗口调用父窗口
- C#高级编程 (第六版) 学习 第二章:C#基础
- C#高级编程 (第六版) 学习 第一章:.Net体系结构
- C# zip压缩
- C#编程总结(九)字符编码
- C# Winform应用程序占用内存较大解决方法整理
- MPU6050 实时图表上位机 C#
- 【C#】 关于属性
- C#中yield return用法分析
- C#中用RSA算法生成公钥和私钥 openssl RSA密钥的生成与配置
- C#控制台应用程序 “输出”到控制台 Console.WriteLine
- C#操作IE
- 结构体和类的区别
- C# 构建动态树
- .NET(C#):分析IL中的if-else,while和for语句并用Emit实现
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)