保存SQLite查询结果在Excel中与Perl

我有以下perl代码来执行sqlite查询并将结果保存在文本文件中。 但是我想将结果保存在Excel表格中。 有什么办法可以做到吗?

#!/usr/local/bin/perl -w use strict; use DBI; my $dbfile = 'C:\usage.db3'; # your database file my $dbh = DBI->connect( # connect to your database, create if needed "dbi:SQLite:dbname=$dbfile", # DSN: dbi, driver, database file "", # no user "", # no password { RaiseError => 1 }, # complain if something goes wrong ) or die $DBI::errstr; #use Data::Dump::Streamer; my $array1 = $dbh->selectall_arrayref("SELECT USR.id,USR.name, ST.license FROM users USR, status ST, upd_ate UD WHERE UD.upt_id = (select max(p2.upt_id) from upd_ate p2) AND ST.id = USR.id AND ST.upt_id = UD.upt_id ORDER BY ST.license,USR.name"); open FILE, ">btc.txt" or die $!; foreach my $Ilink (@$array1) { my ($id, $name, $license) = @$Ilink; print FILE "$id|$name|$license\n"; } close FILE; 

谢谢

我使用了Excel :: Template模块来实现类似的目标:在我们的项目中,我们不得不以.xls格式创build数百(甚至数千)电子表格。 这些电子表格具有相同的布局,但是当然,里面的数据非常不同。 )

我喜欢这个模块的一般概念:你创build一个模板文件(让我们称之为license.xml ):

 <workbook> <worksheet name="licenses"> <loop name="license_data"> <row> <cell text="$id"></cell> <cell text="$name"></cell> <cell text="$license"></cell> </row> </loop> </worksheet> </workbook> 

…然后传递一些数据,如下所示:

 use strict; use Excel::Template; my $template = Excel::Template->new( filename => 'license.xml', ); $template->param('license_data' => [ { id => 1, name => 'John Doe', license => '77-77-7' }, { id => 2, name => 'Jack Right', license => '88-88-8' } ]); $template->write_file('license.xls'); 

…瞧! 在脚本工作结束时,您有一个完整的XLS文件。 )

更新:这是很久以前,我完全搞砸在这篇文章的第一个版本的模板的语法。 :(现在它工作正常,我已经检查。)

另外,我还想提一下,Excel :: Template的function是有限的(例如,插入一个合并的单元格是一个非常复杂的过程)。 我读过Excel :: Template :: Plus有点更强大,但还没有find一个真正的任务来练习它。 )

我用DBI_to_Excel.pm和from_dbi_to_excel.pl使用fbs_load.yml