using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class _ss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
dt.Columns.Add("col5");
for (int i = 0; i < 5000; i++)
{
DataRow dr = dt.NewRow();
dr.ItemArray = new object[]{System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString()};
dt.Rows.Add(dr);
}
if (dt != null)
{
StringBuilder sr = new StringBuilder();
foreach (DataColumn col in dt.Columns)
{
sr.Append(col.ColumnName + "\t");
}
sr.Append("\r\n");
foreach (DataRow dr in dt.Rows)
{
foreach (object obj in dr.ItemArray)
{
sr.Append(obj.ToString() + "\t");
}
sr.Append("\r\n");
}
//文本输出为csv个数,可用excel直接打开
Response.Clear();
Response.Buffer = true;
Response.Charset = "gb2312";
//定义为为附件下载
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode("统计报表_", System.Text.Encoding.UTF8) + DateTime.Now.ToString("yyyyMMdd-HHmmss-ffffff") + ".xls\"");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
//文本输出
System.IO.StringWriter strWriter = new System.IO.StringWriter();
strWriter.Write(sr.ToString());
strWriter.Flush();
//System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
//this.RenderControl(htmlWriter);
Response.Write(strWriter.ToString());
Response.End();
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class _ss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
dt.Columns.Add("col5");
for (int i = 0; i < 5000; i++)
{
DataRow dr = dt.NewRow();
dr.ItemArray = new object[]{System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString(),
System.Guid.NewGuid().ToString()};
dt.Rows.Add(dr);
}
if (dt != null)
{
StringBuilder sr = new StringBuilder();
foreach (DataColumn col in dt.Columns)
{
sr.Append(col.ColumnName + "\t");
}
sr.Append("\r\n");
foreach (DataRow dr in dt.Rows)
{
foreach (object obj in dr.ItemArray)
{
sr.Append(obj.ToString() + "\t");
}
sr.Append("\r\n");
}
//文本输出为csv个数,可用excel直接打开
Response.Clear();
Response.Buffer = true;
Response.Charset = "gb2312";
//定义为为附件下载
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode("统计报表_", System.Text.Encoding.UTF8) + DateTime.Now.ToString("yyyyMMdd-HHmmss-ffffff") + ".xls\"");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
//文本输出
System.IO.StringWriter strWriter = new System.IO.StringWriter();
strWriter.Write(sr.ToString());
strWriter.Flush();
//System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
//this.RenderControl(htmlWriter);
Response.Write(strWriter.ToString());
Response.End();
}
}
}