if (excel.Checked)
{
ToExcel(dt,"id/t山东/t济南");
}
public void ToExcel(DataTable dt, string colName)
{
StringWriter sw=new StringWriter();
sw.Write(colName);//
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine("");
for(int i = 0; i<dt.Columns.Count; i++)
{
sw.Write(dr[i].ToString().Replace("/n","") + "/t");
}
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
C#导出Excel数据
本文介绍了一种使用C#将DataTable数据导出到Excel的方法。通过简单的代码实现,包括写入表头、遍历数据行并处理特殊字符,最终通过HTTP响应返回Excel文件。
1943

被折叠的 条评论
为什么被折叠?



