public void CreateExcel(DataTable dt, string FileName)
{
string FileType = "application/ms-excel";
Response.Clear();
Response.Charset = "UTF-8";
Response.Buffer = true;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + FileName + ".xls\"");
Response.ContentType = FileType;
string colHeaders = string.Empty;
colHeaders = "标题";
Response.Output.Write(colHeaders);
colHeaders = string.Empty;
string ls_item = string.Empty;
DataRow[] myRow = dt.Select();
int cl = dt.Columns.Count;
foreach (DataRow row in myRow)
{
int count = 0;
for (int i = 0; i < cl; i++)
{
if (i == (cl - 1))
{
ls_item += row[i].ToString() + "\n";
}
else
{
if (count < 2)
{
ls_item = ls_item + "" + row[i].ToString() + "\t";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
count++;
}
Response.Output.Write(ls_item);
ls_item = string.Empty;
}
Response.Output.Flush();
Response.End();
}