Recipe 10.38. Exchanging Data Between MySQL and Microsoft Excel


Problem

You want to exchange information between MySQL and Excel.

Solution

Your programming language might provide modules to make this task easier. For example, there are Perl modules that read and write Excel spreadsheet files. You can use them to construct your own data transfer utilities.

Discussion

If you need to transfer Excel files into MySQL, check around for modules that let you do this from your chosen programming language. For example, you can read and write Excel spreadsheets from within Perl scripts by installing a few modules:

  • Spreadsheet::ParseExcel::Simple provides an easy-to-use interface for reading Excel spreadsheets.

  • Spreadsheet::WriteExcel::Simple enables you to create files in Excel spreadsheet format.

These Excel modules are available from the Perl CPAN. (They're actually frontends to other modules, which you also need to install as prerequisites.) After installing the modules, use these commands to read their documentation:

% perldoc Spreadsheet::ParseExcel::Simple % perldoc Spreadsheet::WriteExcel::Simple             

These modules make it relatively easy to write a couple of short scripts for converting spreadsheets to and from tab-delimited file format. Combined with techniques for importing and exporting data into and out of MySQL, these scripts can help you move spreadsheet contents to MySQL tables and vice versa. Use them as is, or adapt them to suit your own purposes.

The following script, from_excel.pl, reads an Excel spreadsheet and converts it to tab-delimited format:

#!/usr/bin/perl # from_excel.pl - read Excel spreadsheet, write tab-delimited, # linefeed-terminated output to the standard output. use strict; use warnings; use Spreadsheet::ParseExcel::Simple; @ARGV or die "Usage: $0 excel-file\n"; my $xls = Spreadsheet::ParseExcel::Simple->read ($ARGV[0]); foreach my $sheet ($xls->sheets ()) {   while ($sheet->has_data ())   {     my @data = $sheet->next_row ();     print join ("\t", @data) . "\n";   } } 

The to_excel.pl script performs the converse operation of reading a tab-delimited file and writing it in Excel format:

#!/usr/bin/perl # to_excel.pl - read tab-delimited, linefeed-terminated input, write # Excel-format output to the standard output. use strict; use warnings; use Spreadsheet::WriteExcel::Simple; my $ss = Spreadsheet::WriteExcel::Simple->new (); while (<>)                            # read each row of input {   chomp;   my @data = split (/\t/, $_, 10000); # split, preserving all fields   $ss->write_row (\@data);            # write row to the spreadsheet } binmode (STDOUT); print $ss->data (); # write the spreadsheet 

to_excel.pl assumes input in tab-delimited, linefeed-terminated format. Use it in conjunction with cvt_file.pl to work with files that are not in that format.

Another Excel-related Perl module, Spreadsheet::WriteExcel::FromDB, reads data from a table using a DBI connection and writes it in Excel format. Here's a short script that exports a MySQL table as an Excel spreadsheet:

#!/usr/bin/perl # mysql_to_excel.pl - given a database and table name, # dump the table to the standard output in Excel format. use strict; use warnings; use DBI; use Spreadsheet::ParseExcel::Simple; use Spreadsheet::WriteExcel::FromDB; # ... process command-line options (not shown) ... @ARGV == 2 or die "$usage\n"; my $db_name = shift (@ARGV); my $tbl_name = shift (@ARGV); # ... connect to database (not shown) ... my $ss = Spreadsheet::WriteExcel::FromDB->read ($dbh, $tbl_name); binmode (STDOUT); print $ss->as_xls (); 

Each of the three utilities writes to its standard output, which you can redirect to capture the results in a file:

% from_excel.pl data.xls > data.txt % to_excel.pl data.txt > data.xls % mysql_to_excel.pl cookbook profile > profile.xls             




MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net