NET页面导出Excel实例代码。

复制代码代码如下所示:
public static void CreateExcel(DataSet ds)
{
字符串的文件名= datetime.now.tostring(yyyymmddhhmmssff )+.xls;
httpcontext.current.response.contentencoding = system.text.encoding.getencoding(我);
HttpContext.Current.Response.AppendHeader(内容配置
字符串colheaders =

表对象和对象定义,而它的值是用数据集初始化的。
DataTable dt = DS表{ 0 };
DataRow { } = DT。选择(米萝); / /可以类似于DT。选择(ID > 10)形成数据筛选的目的
int = i 0;
int CL = dt.columns.count;


获取数据表列标题,每个标题为t分割,回车后最后一列标题。
对于(i = 0;i <;CL;+ +)
{
如果(i =(CL - 1))/最后一列,加上
{
colheaders = DT。列{我}。caption.tostring(+);
}
其他的
{
colheaders = DT。列{我}。caption.tostring(+) T ;
}

}
httpcontext.current.response.write(colheaders);
以编写输出流获得的数据信息

行数据处理
foreach(DataRow排在米萝)
{
/ /当前行数据转换成HTTP输出流和下行数据的空ls_item
对于(i = 0;i <;CL;+ +)
{
如果(i =(CL - 1))/最后一列,加上
{
ls_item =行{我},ToString(+);
}
其他的
{
ls_item =行{我},ToString(+) T ;
}

}
httpcontext.current.response.write(ls_item);
ls_item = ;

}
HttpContext.Current.Response.End();
}