perl读取数据库并写入excel的代码,用到了DBI与Spreadsheet::WriteExcel模块,供大家学习参考。
#!/usr/bin/perl -w
use Spreadsheet::WriteExcel;
use DBI;
my $workbook = Spreadsheet::WriteExcel->new('perl.xls');
$worksheet = $workbook->add_worksheet();
my $dbh=DBI->connect('DBI:mysql:test;host=10.1.205.112;port=3306',
'foo', 'foo') or die("Failed to connect: $DBI::errstrn");
# 我只从表foo中取了10行
my $sth=$dbh->prepare("select * from foo limit 10");
$sth->execute;
my $row = 0;
while(@row = $sth->fetchrow_array){
my $col = 0;
while($col < $#row+1){
$worksheet->write($row, $col, $row[$col]);
$col++;
}
$row++;
}