日期:2014-05-17 浏览次数:20940 次
protected void down_datacsv(DataTable dt, string filename) {
StringWriter sw = new StringWriter();
string Headsting = "";
//输出表头
foreach (DataColumn col in dt.Columns)
{
Headsting += col.ColumnName + ",";
}
sw.WriteLine(Headsting);
foreach (DataRow dr in dt.Rows)
{
sw.WriteLine(string.Join(",", dr.ItemArray));
} sw.Close();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(sw);
HttpContext.Current.Response.End();