ASP.NET--最も簡単なExcelのエクスポート

12073 ワード

#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
";
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() +"
";
response.Write(ls_item);
i
++;
}
// HTTP
response.Flush();
response.Clear();
response.End();
}
#endregion