导出EXCEL

protected void ButtonExcel_Click1(object sender, EventArgs e)
        {
            List
<string> userTypes = new List<string>();
            
if (this.RadioButtonListUserTypes.SelectedValue.Length > 0)
            {
                userTypes.Add(
this.RadioButtonListUserTypes.SelectedValue);
            }

            UserStatus userStatus 
= (UserStatus)Enum.Parse(typeof(UserStatus), this.RadioButtonByUserStatus.SelectedValue);

            
int recordsCount;
            var userElements 
= MembershipApi.FindUsers(this.TextBoxQueryByAgencyName.Text, null, userTypes.ToArray(), this.TextBoxQueryByUserName.Text, userStatus, 020out recordsCount);

            StreamReader sr 
= new StreamReader(Server.MapPath("~/Membership/UserExcel.htm"));
            
string content = sr.ReadToEnd();
            
string data = "";

            
foreach (UserElement dr in userElements)
            {
                data 
+= "<tr><td>'" + dr.CompanyNames.ToString() + "</td><td>" + dr.CompanyNames.ToString() + "</td><td>" + dr.CompanyNames.ToString() + "</td><td> "
               
+ dr.RoleNames.ToString() + "</td><td>" + dr.CompanyNames.ToString() + "</td><td>" + dr.CompanyNames.ToString() + "</td><td>" + dr.CompanyNames.ToString() + "</td><td>'" + dr.CompanyNames.ToString() + "</td></tr>";
            }

            content 
= content.Replace("%data%", data);

            
string fileName = "EMS_" + DateTime.Now.ToString("yyyyMMddhhmmss"+ ".xls";


            
//从服务器上保存到本机上
            if (WriteFile(Server.MapPath(@"~/Reports/"), fileName, content))
            {
                Response.Clear();
                Response.Buffer 
= true;
                Response.AppendHeader(
"Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("UTF-8")));
                Response.WriteFile(Server.MapPath(
@"~/Reports/"+ fileName);
                Response.Flush();
                Response.Close();
            }
            
else
            {
                ClientScript.RegisterStartupScript(
this.GetType(), "aa""<script>alert('导出报表失败!');</script>");
                
return;
            }
        }
posted @ 2008-12-01 10:04  paymob  阅读(275)  评论(1编辑  收藏  举报