title: Convert LDAP search results into CSV
body: <p>I'm currently working on a program that takes the results of an LDAP search and converts it into CSV. I've never worked with LDAP before, only SQL, and I find it a bit challenging. </p>
  
  <p>I'm having trouble getting the LDAP data into a form that will translate well to CSV. I've been trying to use what I learned in SQL to help out, but it's proving to be a bit challenging.</p>
  
  <p>Here's the LDAP server I'm working with.</p>
  
  <h1>example.org</h1>
  
  <pre><code>dn: dc=example,dc=org
  objectClass: top
  objectClass: dcObject
  objectClass: organization
  o: Example Inc.
  dc: example
  
  # admin, example.org
  dn: cn=admin,dc=example,dc=org
  objectClass: simpleSecurityObject
  objectClass: organizationalRole
  cn: admin
  description: LDAP administrator
  userPassword: mypassword
  
  # people, example.org
  dn: ou=people,dc=example,dc=org
  objectClass: organizationalUnit
  ou: people
  
  # chris, people, example.org
  dn: uid=jane,ou=people,dc=example,dc=org
  objectClass: inetOrgPerson
  cn: Chris
  sn: smith
  mail: chris@example.org
  uid: chris
  </code></pre>
  
  <p>And here's the Ruby code I'm using to try to get the data.</p>
  
  <pre><code>ldap = Net::LDAP.new  :host =&gt; '127.0.0.1',
    :port =&gt; 1300,
    :auth =&gt; {
      :method =&gt; :simple,
      :username =&gt; 'cn=admin,dc=example,dc=org',
      :password =&gt; 'mypassword'
  }
  filter = Net::LDAP::Filter.eq( "cn", "Chris*")
  treebase = "dc=example,dc=org"
  
  ldap.search( :base =&gt; treebase, :filter =&gt; filter ) do |entry|
    CSV.open("mysearch.csv", "w") do |csv|
    puts "DN: #{entry.dn}"
    entry.each do |attribute, values|
      search_array.push(attribute)
      values.each do |value|
       csv &lt;&lt; [attribute, values]
       data_array.push(values)
      end
      end
    end
  end
  </code></pre>
  
  <p>When I try to load it up in a CSV, here's what I end up getting:
  <a href="https://i.stack.imgur.com/3HUIU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3HUIU.png" alt="wrong"></a></p>
  
  <p>How can I get the headers to line up at the top of the page, not vertically? Any pointers/suggestions from anyone?</p>