private void toexcel(DataSet ds, string title)
{
StringBuilder sb = new StringBuilder();
Response.Clear();
Response.Buffer = false;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + title + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
this.EnableViewState = false;
System.Data.DataTable table = ds.Tables[0];
sb.Append("<table border=1>\n");
sb.Append("<tr>\n");
foreach (DataColumn col in table.Columns)
{
sb.Append("<td>");
sb.Append(col.ColumnName);
sb.Append("</td>\n");
}
sb.Append("</tr>\n");
foreach (DataRow row in table.Rows)
{
sb.Append("<tr>\n");
foreach (DataColumn col in table.Columns)
{
sb.Append("<td>");
sb.Append(row[col.ColumnName].ToString());
sb.Append("</td>\n");
}
sb.Append("</tr>\n");
}
sb.Append("</table>\n");
Response.Write(sb.ToString());
Response.End();
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/12/30/1921355.html,如需转载请自行联系原作者