ReportToExcel 类
------------方法
public void OutToExcel(string FileName ,DataSet ds,System.Web.HttpResponse response,bool OutputLastCol)
{
try
{
//获得表头
string title = "";
int iColCount = ds.Tables[0].Columns.Count;
if(OutputLastCol == false)
iColCount -= 1;
for(int i=0;i<iColCount;i++)
{
title = title + ds.Tables[0].Columns[i].ColumnName + "/t";
}
FileName=FileName +".xls";
//*************************************************
//一个excel里面创建多过sheet 可以考虑修改此处
//先输出表头
response.Clear();
response.ContentEncoding=System.Text.Encoding.UTF8;
response.AppendHeader("Content-Disposition","attachment;filename=" + Server.UrlEncode(FileName));
response.ContentType="application/ms-excel";
byte[] title_b=System.Text.Encoding.GetEncoding("GB2312").GetBytes(title);
response.BinaryWrite(title_b);
response.Write('/r');
response.Flush();
//再输出所有行
int rowIndex=ds.Tables[0].Rows.Count;
for(int i=0;i<rowIndex;i++)
{
DataRow row=ds.Tables[0].Rows[i];
for(int j=0;j<iColCount;j++)
{
byte[] temp=System.Text.Encoding.GetEncoding("GB2312").GetBytes(row[j].ToString().Trim() + "/t");
response.BinaryWrite(temp);
}
response.Write('/r');
response.Flush();
}
//*********************************************************
}
catch(System.Exception e1)
{
return;
}
}
--调用
ReportToExcel report = new ReportToExcel();
report.OutToExcel(FileName + System.DateTime.Now.ToString("yyyy-MM-dd"),ds,this.Response,false);
Response.End();