7,765
社区成员
发帖
与我相关
我的任务
分享
public void CreateExcel(DataTable dt, string fileName)
{
HttpResponse response = HttpContext.Current.Response;
response.ContentEncoding = Encoding.GetEncoding("GB2312");
response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
string s = "";
string str2 = "";
DataRow[] rowArray = dt.Select();
int num = 0;
int count = dt.Columns.Count;
num = 0;
while (num < count)
{
if (num == (count - 1))
{
s = s + dt.Columns[num].Caption.ToString() + "\n";
}
else
{
s = s + dt.Columns[num].Caption.ToString() + "\t";
}
num++;
}
response.Write(s);
foreach (DataRow row in rowArray)
{
for (num = 0; num < count; num++)
{
if (num == (count - 1))
{
str2 = str2 + row[num].ToString() + "\n";
}
else
{
str2 = str2 + row[num].ToString() + "\t";
}
}
response.Write(str2);
str2 = "";
}
response.End();
}