查出数据并导出到excel,测试了下,可以生成。
//从DataSet到出到Excel
protected void Button1_Click(object sender, EventArgs e)
{
string sqlStr = "select * from IoApply ";
//string sqlStr = "select id as 序号,(case Gender when 0 then '女' when 1 then '男' end) as 性别 from OpenDay order by RegisterTime desc";
DataTable DtOut = DataAccess.ExecuteSql("OpenDay.ConnectionString", sqlStr).Tables[0];
Response.Clear();
Response.ContentEncoding = Encoding.Default;
Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("OpenDay"+System.DateTime.Now.ToString("MM.dd")+".xls"));
Response.ContentType = "application/octet-stream";
for (int i = 0; i < DtOut.Columns.Count; i++)
{
Response.Write(DtOut.Columns[i].ColumnName.ToString() + "\t");
}
Response.Write(Environment.NewLine);
for (int i = 0; i < DtOut.Rows.Count; i++)
{
StringBuilder sb = new StringBuilder();
for (int j = 0; j < DtOut.Columns.Count; j++)
{
sb.Append(DtOut.Rows[i][j].ToString());
sb.Append("\t");
}
Response.Write(sb.ToString());
Response.Write(Environment.NewLine);
}
Response.End();
}
}
转载于:https://www.cnblogs.com/lcq135/archive/2008/05/12/1194097.html