选择部分记录输出到Excel

ToExcel.aspx

< asp:datagrid id = " DataGrid1 "  runat = " server "  Width = " 595px "  AutoGenerateColumns = " False "  PageSize = " 30 "
                                AllowPaging
= " True "  BorderWidth = " 1px "  BorderColor = " SeaGreen "  Font - Size = " 12px " >
                                
< ItemStyle HorizontalAlign = " Left " ></ ItemStyle >
                                
< HeaderStyle HorizontalAlign = " Center " ></ HeaderStyle >
                                
< Columns >
                                    
< asp:TemplateColumn >
                                        
< HeaderStyle Width = " 40px " ></ HeaderStyle >
                                        
< ItemStyle HorizontalAlign = " Center " ></ ItemStyle >
                                        
< ItemTemplate >
                                            
< asp:CheckBox id = " myselect "  Runat = " server " ></ asp:CheckBox >
                                        
</ ItemTemplate >
                                        
< EditItemTemplate >
                                            
< asp:TextBox id = TextBox1 runat = " server "  Text = ' <%# DataBinder.Eval(Container, "DataItem.id") %> ' >
                                            
</ asp:TextBox >
                                        
</ EditItemTemplate >
                                    
</ asp:TemplateColumn >
                                    
< asp:ButtonColumn DataTextField = " 公司名称 "  HeaderText = " 公司名称 "  CommandName = " Select " ></ asp:ButtonColumn >
                                    
< asp:BoundColumn Visible = " False "  DataField = " id " ></ asp:BoundColumn >
                                
</ Columns >
                                
< PagerStyle HorizontalAlign = " Right "  Position = " Top "  Mode = " NumericPages " ></ PagerStyle >
                            
</ asp:datagrid >

Toexcel.aspx.cs

a.数据绑定

void  Bind()
        
{

            
string CS=this.Application.Get("kehuConnectionString").ToString();
            
            
string myQuery="";
            
                
            myQuery
= "SELECT  * from [data] where (id<>null)  ";
        
            
if(Label1.Text.Trim()!=""
                myQuery
+=Label1.Text.Trim();
        
        
            OleDbConnection myConnection 
= new OleDbConnection(CS);
            myConnection.Open();
            
            OleDbDataAdapter objDataAdapter
=new OleDbDataAdapter(myQuery,myConnection);
        
            DataSet ds 
= new DataSet();
            objDataAdapter.Fill(ds,
"data");

            
///
            DataTable dt=ds.Tables["data"]; 
                

            
//

            
this.DataGrid1.DataSource=dt.DefaultView;
            DataGrid1.DataBind();

            
if(Session["userlist"]!=null)
            
{
                Hashtable ht 
=(Hashtable)Session["userlist"];
                
if(ht!=null)
                
{
                    
for(int i = 0 ;i<DataGrid1.Items.Count ;i++)
                    
{
                        
if (ht.ContainsKey(DataGrid1.Items[i].Cells[2].Text.ToString().Trim()))
                            (DataGrid1.Items[i].Cells[
0].FindControl("myselect"as CheckBox).Checked = true;

                    }

                }

            }



            ds.Clear();
            
if(myConnection!=null)myConnection.Close();
        }

b.选择

private   void  check()
        
{
            Hashtable ht 
= new Hashtable();
            
if(Session["userlist"]!=null)
            
{
                ht 
=(Hashtable) Session["userlist"];
                
if(ht!=null)
                
{
                    
for(int i = 0 ;i<DataGrid1.Items.Count ;i++)
                    
{
                        
if ( (DataGrid1.Items[i].Cells[0].FindControl("myselect"as CheckBox).Checked)
                        
{
                            
if (! ht.ContainsKey(DataGrid1.Items[i].Cells[2].Text.ToString().Trim()))
                            
{
                                ht.Add(DataGrid1.Items[i].Cells[
2].Text.ToString().Trim(),DataGrid1.Items[i].Cells[2].Text.ToString().Trim());
                            }

                        }

                        
else
                        
{
                            
if ( ht.ContainsKey(DataGrid1.Items[i].Cells[2].Text.ToString().Trim()))
                            
{
                                ht.Remove(DataGrid1.Items[i].Cells[
2].Text.ToString().Trim());
                            }

                        }

                    }

                }

            }

            
else
            
{
                
for(int i = 0 ;i<DataGrid1.Items.Count ;i++)
                
{
                    
if ( (DataGrid1.Items[i].Cells[0].FindControl("myselect"as CheckBox).Checked)
                    
{
                        ht.Add(DataGrid1.Items[i].Cells[
2].Text.ToString().Trim(),DataGrid1.Items[i].Cells[2].Text.ToString().Trim());
                    }

                }

            }


            Session[
"userlist"= ht;
        }

c.输出到excel

private   void  LinkButton2_Click( object  sender, System.EventArgs e)
        
{
            
/*
            Hashtable ht = new Hashtable();

            if(Session["userlist"]!=null)
            {
                ht =(Hashtable) Session["userlist"];
                if(ht!=null)
                {
                    ht.Clear();
                }
            }
            
*/

            check();

            Hashtable ht 
=(Hashtable) Session["userlist"];

            
///
            StringWriter sw=new StringWriter(); 

            
string myhead="";

        
            
            myhead
="联系人 买卖家 部门 职务 公司名称 公司中文名 地址 国别 电话 手机 传真 电子邮件 网址 主营商品类 主营商品 收集日期 收集来源 "

        
            sw.WriteLine(myhead); 

            
string mycol="";//int myint=0;


            
///

            
string CS=Application.Get("kehuConnectionString").ToString();
            
string myQuery="";

            OleDbConnection myConnection 
= new OleDbConnection(CS);
            myConnection.Open();

            
foreach (DictionaryEntry objDE in ht)
            
{
                
string myid=objDE.Value.ToString();
      


                myQuery
= "SELECT  联系人,买卖家,部门,职务,公司名称,公司中文名,地址,国别,电话,手机,传真,电子邮件,企业网址,主营商品类,主营商品,收集日期,收集来源 from [data] where id="+myid;

                OleDbCommand myCommand
= new OleDbCommand(myQuery,myConnection);
                OleDbDataReader objDataReader
=myCommand.ExecuteReader();

            
                mycol
="";
                
if(objDataReader.Read())
                
{
                
                    
for(int j=0;j<17;j++)
                        
if(!objDataReader.IsDBNull(j))
                        
{
                            
if(j==15)
                                mycol
+=objDataReader.GetDateTime(j).ToShortDateString().Trim() + " "
                            
else
                                mycol
+=objDataReader.GetString(j).Trim() + " "
                        }

                        
else{
                        
                            mycol
+= " "
                        }

                
            

                    sw.WriteLine(mycol); 
    
                }

    
                objDataReader.Close();
            
            }

    

            
if(myConnection!=null)myConnection.Close();

            Response.AddHeader(
"Content-Disposition""attachment; filename=RESULT.XLS"); 
            Response.ContentType 
= "application/ms-excel"
            Response.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312"); 
            Response.Write(sw); 
            Response.End(); 
            sw.Close();
        }



        
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值