ASP.NET---最简单的导出Excel

#region 最简单的导出Excel
publicvoid CreateExcel(DataTable _table, string FileName)
{
//FileName = Server.UrlEncode(FileName);
HttpResponse response = Page.Response;
response.Clear();
response.Buffer
=true;
response.Charset
="GB2312";
response.HeaderEncoding
= System.Text.Encoding.GetEncoding("GB2312");
response.ContentEncoding
= System.Text.Encoding.GetEncoding("GB2312");
response.AddHeader(
"Content-Disposition", "attachment;filename="+ FileName);
response.AddHeader(
"Content-Type", "application/octet-stream;charset=GB2312");
response.ContentType
="application/ms-excel";
string ls_item ="";

ls_item
="编号\t险种\t保单号\t手续费\t手续费率\t佣金\t备注\t分组序号\n";
response.Write(ls_item);
ls_item
="";
int i =1;
foreach (DataRow row in _table.Rows)
{
ls_item
= i.ToString() +"\t"+ row[3] +"\t"+ row[4] +"\t"+ row[7] +"\t"+ row[8] +"\t"+ row[9] +"\t"+ row[10] +"\t"+ i.ToString() +"\n";
response.Write(ls_item);
i
++;
}
//写缓冲区中的数据到HTTP头文件中
response.Flush();
response.Clear();
response.End();
}
#endregion

你可能感兴趣的:(asp.net)