private void btnExport_Click(object sender, EventArgs e) { if (this.exportFieldsCheckBoxList.SelectedItem == null) { this.ShowMsg("请选择需要导出的记录", false); } else { IList <string> fields = new List <string>(); IList <string> list2 = new List <string>(); foreach (ListItem item in this.exportFieldsCheckBoxList.Items) { if (item.Selected) { fields.Add(item.Value); list2.Add(item.Text); } } OrderStatisticsQuery query = new OrderStatisticsQuery { BeginDate = this.BeginDate, EndDate = this.EndDate }; DataTable table = ShopStatisticHelper.Product_GetStatisticReport_NoPage(query, fields); StringBuilder builder = new StringBuilder(); foreach (string str in list2) { builder.Append(str + ","); if (str == list2[list2.Count - 1]) { builder = builder.Remove(builder.Length - 1, 1); builder.Append("\r\n"); } } foreach (DataRow row in table.Rows) { foreach (string str2 in fields) { builder.Append(row[str2]).Append(","); if (str2 == fields[list2.Count - 1]) { builder = builder.Remove(builder.Length - 1, 1); builder.Append("\r\n"); } } } this.Page.Response.Clear(); this.Page.Response.Buffer = false; this.Page.Response.Charset = "GB2312"; if (this.exportFormatRadioButtonList.SelectedValue == "csv") { this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=DataExport.csv"); this.Page.Response.ContentType = "application/octet-stream"; } else { this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=DataExport.txt"); this.Page.Response.ContentType = "application/vnd.ms-word"; } this.Page.Response.ContentEncoding = Encoding.GetEncoding("GB2312"); this.Page.EnableViewState = false; this.Page.Response.Write(builder.ToString()); this.Page.Response.End(); } }
private void btnExport_Click(object sender, System.EventArgs e) { if (this.exportFieldsCheckBoxList.SelectedItem == null) { this.ShowMsg("请选择需要导出的记录", false); return; } System.Collections.Generic.IList <string> list = new System.Collections.Generic.List <string>(); System.Collections.Generic.IList <string> list2 = new System.Collections.Generic.List <string>(); foreach (System.Web.UI.WebControls.ListItem listItem in this.exportFieldsCheckBoxList.Items) { if (listItem.Selected) { list.Add(listItem.Value); list2.Add(listItem.Text); } } System.Data.DataTable dataTable = ShopStatisticHelper.Product_GetStatisticReport_NoPage(new OrderStatisticsQuery { BeginDate = this.BeginDate, EndDate = this.EndDate }, list); System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); foreach (string current in list2) { stringBuilder.Append(current + ","); if (current == list2[list2.Count - 1]) { stringBuilder = stringBuilder.Remove(stringBuilder.Length - 1, 1); stringBuilder.Append("\r\n"); } } foreach (System.Data.DataRow dataRow in dataTable.Rows) { foreach (string current2 in list) { stringBuilder.Append(dataRow[current2]).Append(","); if (current2 == list[list2.Count - 1]) { stringBuilder = stringBuilder.Remove(stringBuilder.Length - 1, 1); stringBuilder.Append("\r\n"); } } } this.Page.Response.Clear(); this.Page.Response.Buffer = false; this.Page.Response.Charset = "GB2312"; if (this.exportFormatRadioButtonList.SelectedValue == "csv") { this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=DataExport.csv"); this.Page.Response.ContentType = "application/octet-stream"; } else { this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=DataExport.txt"); this.Page.Response.ContentType = "application/vnd.ms-word"; } this.Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); this.Page.EnableViewState = false; this.Page.Response.Write(stringBuilder.ToString()); this.Page.Response.End(); }