生成Excel文档(用户选择存放)

public void ExportExcel(DataSet _dsTemp)
  {
   StringWriter sw=new StringWriter();

   string Title = "序号\t";
   
   foreach(DataColumn col in _dsTemp.Tables[0].Columns)
   {
    Title+=col.ColumnName+"\t";               
   }

   //Title = HttpUtility.UrlEncode(Title,System.Text.Encoding.UTF8);
   sw.WriteLine(Title);
   Page.RegisterStartupScript("alertMsg","<script>alert('"+Title+"')</script>");
   
   if (_dsTemp!=null)
   {

    int j=0;
    foreach (DataRow dr in _dsTemp.Tables[0].Rows)
    {
     string Value = "";
     j++;
     Value +=j.ToString()+"\t";
     for(int i=0;i<_dsTemp.Tables[0].Columns.Count;i++)
     {
      Value += "=\"" + dr[i].ToString() + "\"\t";
      //Value +="'"+dr[i].ToString()+"\t";
     }
      sw.WriteLine(Value);
    }
    
    sw.Close();
    Response.Clear();  
    //Response.Buffer = true;
    Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode("Report",System.Text.Encoding.UTF8)+".xls");
    Response.ContentType="application/ms-excel";  
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");
    Response.Write(sw);
    Response.End();
   }
  }

你可能感兴趣的:(Excel)