protected void bSave_Click(object sender, EventArgs e) { GridShowColumnWWE gsc = new GridShowColumnWWE(); gsc.HeaderText = TBHeaderText.Text.Trim(); gsc.ColumnData = TBColumnData.Text.Trim(); gsc.ColumnType = ddlColumnType.SelectedValue.Trim(); gsc.ExcelType = ddlExcelType.SelectedValue.Trim(); gsc.ColumnShow = tbColumnShow.Text.Trim(); gsc.ShowDisplay = ddlShowDisplay.SelectedValue.Trim(); gsc.OrderType = ddlOrderType.SelectedValue.Trim(); List<string> listStyle = new List<string>(); if (!string.IsNullOrEmpty(tbStyle_Width.Text.Trim())) listStyle.Add("width:"+tbStyle_Width.Text.Trim()); if (!string.IsNullOrEmpty(ddlStyle_TextAlign.SelectedValue)) listStyle.Add("text-align:" + ddlStyle_TextAlign.SelectedValue.Trim()); gsc.Style = string.Join(";",listStyle.ToArray()); if (string.IsNullOrEmpty(sColumnGuid)) { gsc.Guid = Guid.NewGuid().ToString(); GSEntity.ColumnList.Add(gsc); } else { gsc.Guid = sColumnGuid; GSEntity.ColumnList[GSEntity.ColumnList.IndexOf(GSEntity.ColumnList.Single(t => t.Guid == sColumnGuid))]= gsc; } strFlag = "ok"; }
protected void DataTableToExcel(DataTable dt, string sFileName) { HttpResponse resp; resp = System.Web.HttpContext.Current.Response; resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); resp.AppendHeader("Content-Disposition", "attachment;filename=" + sFileName); StringBuilder sb = new StringBuilder(); int iColumnLength = dt.Columns.Count; List<string> strList = new List<string>(); for (int i = 0; i < iColumnLength; i++) { strList.Add(dt.Columns[i].ColumnName); } sb.Append(string.Join("\t", strList.ToArray()) + "\n"); for (int i = 0, j = dt.Rows.Count; i < j; i++) { strList = new List<string>(); for (int n = 0; n < iColumnLength; n++) { strList.Add(dt.Rows[i][n].ToString()); } sb.Append(string.Join("\t", strList.ToArray()) + "\n"); } resp.Write(sb.ToString()); //写缓冲区中的数据到HTTP头文件中 resp.End(); }