我有一个包含30+列和6500+行的DataTable。我需要将整个DataTable值转储到Excel文件中。任何人都可以使用C#代码进行帮助。我需要将每个列的值都放在一个单元格中。我需要Excel文件中DataTable的精确外观副本。请提供帮助。
谢谢,Vix
使用此代码…
dt = city.GetAllCity();//your datatable string attachment = "attachment; filename=city.xls"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/vnd.ms-excel"; string tab = ""; foreach (DataColumn dc in dt.Columns) { Response.Write(tab + dc.ColumnName); tab = "\t"; } Response.Write("\n"); int i; foreach (DataRow dr in dt.Rows) { tab = ""; for (i = 0; i < dt.Columns.Count; i++) { Response.Write(tab + dr[i].ToString()); tab = "\t"; } Response.Write("\n"); } Response.End();