Problem Solution Discussion Exchanging Data Between MySQL and Microsoft Excel

1. Expor t t he t able fr om Access in som e t ext for m at , per haps including t he colum n labels. Should you need t o t r ansfor m t he file w it h ot her ut ilit ies t hat assum e t ab- delim it ed, linefeed- t er m inat ed input , it w ill be m ost useful t o expor t in t hat for m at . 2. I f t he t able cont ains dat es and you did not export t hem in I SO form at , it w ill be necessary t o convert t hem for MySQL. cvt _dat e.pl can be used for t his. 3. I f t he MySQL t able int o w hich you w ant t o im port t he Access dat a does not exist , creat e it . The guess_t able.pl ut ilit y m ight be helpful at t his point for gener at ing a CREATE TABLE st at em ent . 4. I m port t he dat afile int o MySQL w it h LOAD DATA or m ysqlim por t .

10.40 Exchanging Data Between MySQL and Microsoft Excel

10.40.1 Problem

You want t o exchange inform at ion bet ween MySQL and Excel.

10.40.2 Solution

Use ut ilit ies such as DBTools or MySQLFront . Or use Perl m odules t hat read and writ e Excel spreadsheet files t o const ruct your own dat a t ransfer ut ilit ies.

10.40.3 Discussion

One w ay t o t ransfer Excel files int o MySQL is t o use t he DBTools or MySQLFront ut ilit ies t hat were discussed in Recipe 10.39 for working wit h Access files. Bot h program s know how t o read Excel files as w ell. But bot h are Window s- specific; for a m ore cross-plat form solut ion t hat works for bot h Unix and Windows, you can read and writ e Excel spreadsheet s from wit hin Perl script s by inst alling a few m odules: • Spreadsheet : : ParseExcel: : Sim ple provides an easy- t o- use int erface for reading Excel spreadsheet s. • Spreadsheet : : Writ eExcel: : Sim ple allows you t o creat e files in Excel spreadsheet form at . These m odules are available from t he Perl CPAN. Theyre act ually front ends t o ot her m odules, which youll also need t o inst all as prerequisit es. Aft er inst alling t he m odules, use t hese com m ands t o read t heir docum ent at ion: perldoc Spreadsheet::ParseExcel::Simple perldoc Spreadsheet::WriteExcel::Simple These m odules m ake it relat ively easy t o writ e a couple of short script s shown below for convert ing spreadsheet s t o and from t ab- delim it ed file form at . Com bined wit h t echniques for im port ing and export ing dat a int o and out of MySQL, t hese script s can help you m ove spreadsheet cont ent s t o MySQL t ables and vice versa. Use t hem as is, or adapt t hem t o suit your own purposes. The follow ing script , from _excel.pl, reads an Excel spreadsheet and conver t s it t o t ab- delim it ed form at : usrbinperl -w from_excel.pl - read Excel spreadsheet, write tab-delimited, linefeed-terminated output to the standard output. use strict; 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; } } exit 0; The t o_excel.pl script perform s t he converse operat ion of reading a t ab- delim it ed file and writ ing it in Excel form at : usrbinperl -w to_excel.pl - read tab-delimited, linefeed-terminated input, write Excel-format output to the standard output. use strict; 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 } print ss-data ; write the spreadsheet exit 0; t o_excel.pl assum es input in t ab- delim it ed, linefeed- t erm inat ed form at . Use it in conj unct ion w it h cvt _file.pl t o work wit h files t hat are not in t hat form at . Anot her Excel- relat ed Perl m odule, Spreadsheet : : Writ eExcel: : From DB, reads dat a from a t able using a DBI connect ion and writ es it in Excel form at . Heres a short script t hat export s a MySQL t able as an Excel spreadsheet : usrbinperl -w mysql_to_excel.pl - given a database and table name, dump the table to the standard output in Excel format. use strict; use DBI; use Spreadsheet::ParseExcel::Simple; use Spreadsheet::WriteExcel::FromDB; ... process command-line options not shown ... ARGV == 2 or die Usage: 0 [options] db_name tbl_name\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; print ss-as_xls ; exit 0; Each of t he t hree ut ilit ies writ es t o it s st andard out put , which you can redirect t o capt ure t he result s 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

10.41 Exchanging Data Between MySQL and FileMaker Pro