b310 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have an Excel spreadsheet that I would like to import into a MySQL table. Currently, we are manually typing in the data into the table.

Does anyone know of an automated way where I can take this Excel spreadsheet and import it into the table into the correct fields?

Thanks.

Replies are listed 'Best First'.
Re: Import file into MySQL table
by edoc (Chaplain) on Jun 05, 2003 at 00:09 UTC

    keep it simple.. save the excel sheet as a csv file, then have a look at LOAD DATA INFILE. As long as MySQL can read the file, an sql command such as this should do it:

    mysql> LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name -> FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' -> LINES TERMINATED BY '\n';

    cheers,

    J

      Hello,

      I'll give this a try. Sounds pretty straightfoward. Thanks for the input.
      Hi,

      I'm working on loading the data into a table. I'm receiving an error message that the text file cannot be located.

      Here's the exact statement I'm typing in that calls in the text file.

      mysql> LOAD DATA LOCAL INFILE 'C:\\Helen\\file.csv' INTO TABLE test

      The rest of the command is the same as you gave me. I've read through the MySQL book on how to read a local file and I'm not sure what's happening.

      Any ideas? Thanks.
Re: Import file into MySQL table
by drewbie (Chaplain) on Jun 04, 2003 at 19:53 UTC

    Here's some untested code, but it's adapted from a script I wrote. It should be enough to get you going in the right direction. Hope this helps.

    #!/usr/bin/perl use strict; use DBI; use Spreadsheet::ParseExcel; # connect to DB & prepare statement handle my $dbh = DBI->connect("your:dsn:here"); my $sth = $dbh->prepare("INSERT INTO table (foo,bar) values(?,?)"); # get workbook my $file = $ARGV[0] || usage(); my $parser = Spreadsheet::ParseExcel->new(); my $book = $parser->Parse($file) || die("No workbook found!"); # get the first worksheet my $sheet = $book->{Worksheet}[0]; my ($min, $max) = $sheet->RowRange(); # iterate over each row... foreach my $row ($min..$max) { # get the values in each column you want... my $name = $sheet->Cell($row,1)->Value; my $address = $sheet->Cell($row,2)->Value(); # and add a new row $sth->execute($name, $address); } $dbh->disconnect();
Re: Import file into MySQL table
by talexb (Chancellor) on Jun 04, 2003 at 19:42 UTC
Re: Import file into MySQL table
by daeve (Deacon) on Jun 05, 2003 at 00:04 UTC
Re: Import file into MySQL table
by blueAdept (Beadle) on Jun 04, 2003 at 21:52 UTC
    I'd use DBI for everything..like this:
    1) File | Save As and save the spreadsheet as a CSV file
    2) Remove the .csv extension if excel tries to put one on the newly created csv file
    3) Use DBD::CSV to read in the data, and DBD::mysql to insert the rows into your new table.

    Notes:
    -The name of the CSV file is the table name for DBD::CSV, thus why I suggest removing the .csv extension
    -Lots of ways I think you can get the column names dynamically, and do a one time creation in the myssql database. One perhaps bad idea is if your query is "SELECT * from csvFileName" then you can access the column names with $statement->{NAME} # its an array ref
Re: Import file into MySQL table
by TomDLux (Vicar) on Jun 04, 2003 at 20:38 UTC

    Don't ask me for the details, but it seems to me you should be able to load the Excel file into MS Access---assuming you have the whole Office package installed. It might involve saving the file as comma-separated values, and then using Access to connect to MySQL. At my last job we used Access as a desktop DB client, but while I configured my machine, I never understood the details.

    Sorry for an unhelpful answer, but perhaps checkign the Access manual will provide the info you need.