asp.net关于倒出excel文件

 导出代码:(导出所有的数据)

 SqlConnection con  =   new  SqlConnection();
       SqlDataAdapter sda 
=   new  SqlDataAdapter( " select * from Users " , con);   // 获得表的数据填充ds
        DataSet ds  =   new  DataSet();
        sda.Fill(ds);

        Response.ClearContent();        
        Response.AddHeader(
" content-disposition " " attachment; filename=MyExcelFile.xls " );
        Response.ContentEncoding 
=  System.Text.Encoding.GetEncoding( " GB2312 " );
        Response.ContentType 
=   " application/excel " ;
        
for  ( int  s  =   0 ; s  <  ds.Tables[ 0 ].Columns.Count; s ++ )
        
{
            Response.Write(ds.Tables[
0].Columns[s].Caption.ToString() + "\t");
        }

        Response.Write(
" \n " );
        
foreach  (DataRow row  in  ds.Tables[ 0 ].Rows)
        
{
            
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            
{
                Response.Write((
char)127+row[i].ToString()+ "\t"); ///(char)127 此处避免长数字的字符串导出时被科学法替代
            }

            Response.Write(
'\n');
        }

        Response.Flush();
        Response.End();

 

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