public static void ProductTeacherReport(DataTable dts)
{
DataTable dt = dts;
if (dt.Rows.Count == 0) return;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
MemoryStream ms = new MemoryStream();
foreach (DataColumn dc in dt.Columns)
{
sb.Append(dc.ColumnName + Convert.ToChar(9));
}
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append("\r");
for (int j = 0; j < dt.Columns.Count; j++)
{
sb.Append( dt.Rows[i][j].ToString() + Convert.ToChar(9));
}
}
StreamWriter sw = new StreamWriter(ms);
sw.Write(sb.ToString());
string fileName ="teacher_" + DateTime.Now.Ticks + ".xls";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
没有时间写注释说明,上班偷偷发的哦
{
DataTable dt = dts;
if (dt.Rows.Count == 0) return;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
MemoryStream ms = new MemoryStream();
foreach (DataColumn dc in dt.Columns)
{
sb.Append(dc.ColumnName + Convert.ToChar(9));
}
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append("\r");
for (int j = 0; j < dt.Columns.Count; j++)
{
sb.Append( dt.Rows[i][j].ToString() + Convert.ToChar(9));
}
}
StreamWriter sw = new StreamWriter(ms);
sw.Write(sb.ToString());
string fileName ="teacher_" + DateTime.Now.Ticks + ".xls";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}