perl读数据库并写excel,完整代码如下:
#!/usr/bin/perl-w
useSpreadsheet::WriteExcel;
usedBI;
my$workbook=Spreadsheet::WriteExcel->new('perl.xls');
$worksheet=$workbook->add_worksheet();
my$dbh=DBI->connect('DBI:mysql:test;host=192.168.1.10;port=3306',
'foo','foo')ordie("Failedtoconnect:$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++;
}