Link to home
Start Free TrialLog in
Avatar of minichicken
minichicken

asked on

Export From MySQL to Excel/CSV

Hi

How can I do a query from a MySQL database and export to Excel/CSV?
Is there a class or function that can do that?

Thanks.
SOLUTION
Avatar of sajuks
sajuks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of minichicken
minichicken

ASKER

I need to do this in PHP.

Do I, just execute the query using mysql_query(); ???

Is that all?
I just tested the code, and it works perfectly.
That easy so far...

- Please can you tell where I can read about the available feature for this MySQL csv query.
- How do I add column names into the CSV file?
- How do I specify a relative path of the outputed CSV file?

Thanks so much!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

> matt_mcswain - Unfortunately I am not using ADODB.

> ldbkutty - The code works nicely.

One last question - How do I make the column headers BOLD, is it even possible?

Its not possible with the above class, you might have to do a COM object modelling or have an existing excel file with first row as BOLD and write the db content into that file.

too bad u want this in PHP, there are a couple of modules in Perl that let you write customized formats for excel.

let me know if you are interested.
========================================================

 use Spreadsheet::WriteExcel;
     $sth= $dbh->prepare($select_query);
     $sth->execute();

    my $names     = $sth->{'NAME'};
    my $numFields = $sth->{'NUM_OF_FIELDS'};

    ### Retrieve the returned rows of data and save them on a excel spreadsheet.
    my @row;

    # Create a new Excel workbook called XXXX.xls
    my $workbook = Spreadsheet::WriteExcel->new($database.".xls");

    # Add some worksheets
    my $sheet1 = $workbook->addworksheet();
    $sheet1->activate();

    my $header    =  $workbook->add_format(bold => 1, italic => 1, color => 'brown', bg_color => 22 );
    my $grayline  =  $workbook->add_format( bg_color => 22 );
    my $normalln  =  $workbook->add_format( bg_color => 9 );

    for (my $i = 0;  $i < $numFields;  $i++) {
            $sheet1->write_string(0,$i,$$names[$i], $header );
    }
     $j=1;
     while ( @row = $sth->fetchrow_array() ) {
         for ( my $i = 0; $i <= $#row; $i++) {
           if ( defined($row[$i]) ) {
               $row[$i] =~ s/\.soups\.com//;
           }
           if ( $i == 4 || $i == 5 || $i == 6 || $i == 13 ) {
               $sheet1->write_string($j,$i, (defined($row[$i]) ? $row[$i]: " "), (round($j/2) == 0 ? $normalln : $grayline));
           } else {
              $sheet1->write($j,$i, (defined($row[$i]) ? $row[$i]: " "), ( round($j/2) == 0 ? $normalln : $grayline));
           }

$sheet1->write($j,$i,(defined($hosts{$row[0]})?$hosts{$row[0]}:0),(round($j/2)==0?$normalln : $grayline));
         }
         $j++;
     }
     warn "Data fetching terminated early by error: $DBI::errstr\n"
         if $DBI::err;


regards,
esv.
It alright.... anyway thanks guys :)
This is exactly what i was looking for...well almost...the only problem I have is that it duplicates columns.

Example:

I am selecting data from table users

select * from users where registered>'2008-03-04'

Example output:

name surname adress city
Paul    Paul       Jones   Jones
John   John       Brewer Brewer
etc.

Hope you get the point, I tried to find the error in the code, but no success.
Can you please help?
Thanks





I get an error which is beyond me.  The query works fine in html.

Here's the error message:
1045 - Access denied for user 'dbuser'@'localhost' (using password: YES)