DATASET数据保存到EXCEL文件(asp.net)

public void SaveToExcel(System.Data.DataTable objTable)

{
    int CountR=objTable.Rows.Count;
    int CountC=objTable.Columns.Count;
    Response.Clear();
    Response.Buffer= true;

    //设置Http的头信息,编码格式
          Response.AppendHeader("Content-Disposition","attachment;filename=result.xls");
   Response.ContentType = "application/ms-excel";


 //设置编码
 Response.Charset="GB2312"; 
 Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");

 for (int RowNo = 0; RowNo <= CountR-1; RowNo++)
 {
     string RowContent="";
     for (int CloumnNo = 0; CloumnNo <= CountC-1; CloumnNo++)
     {
  RowContent+=Convert.ToString(objTable.Rows[RowNo][CloumnNo])+"\t";        }
  RowContent+="\n";
  Response.Write(RowContent);
 }
 Response.End();
}

posted @ 2006-07-31 17:11  小y  阅读(977)  评论(0编辑  收藏  举报