免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1037 | 回复: 0
打印 上一主题 下一主题

从MySQL导出XLS数据库工具(zz) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-15 20:44 |只看该作者 |倒序浏览
http://www.yangwenjun.com/article.asp?id=27
这个脚本是使用Perl生成excel xls文件的工具。依赖一些模块,你可以在linux下使用,产生xls文件。使用方式是命令行+参数。非常方便。
#!/usr/bin/perl
#===============================
# mysql to excel
# lastmodify at 2005-1-5
# copyright by hoowa
#=============================
use strict; #严格语法检测
use DBI; #数据库引擎
use Unicode::Map; #Unicode引擎
#use Spreadsheet::WriteExcel; #Excel报表引擎
use Spreadsheet::WriteExcel::Big; #大文件Excel报表引擎
my $hostname='192.168.1.133';
my $username='user';
my $password='pass';
my $dbname='db';
my $trans_compress=1; #任何非一的数关闭数据库到程序间传输压缩
$|=1;
my @cols=('A:A','B:B','C:C','D:D','E:E','F:F','G:G','H:H','I:I','J:J',
'K:K','L:L','M:M','N:N','O:O','P:P','Q:Q','R:R','S:S','T:T','U:U',
'V:V','W:W','X:X','Y:Y','Z:Z','AA:A','BB:B','CC:C','DD:D','EE:E',
'FF:F','GG:G','HH:H','II:I','JJ:J','KK:K','LL:L','MM:M','NN:N',
'OO:O','PP:P','QQ:Q','RR:R','SS:S','TT:T','UU:U','VV:V','WW:W',
'XX:X','YY:Y','ZZ:Z');
#解析来内容
if ($#ARGV != '1') {
print qq~syntax: my2excel.pl  "[where expression]"
~;
exit;
}
$ARGV[1]=~ s/\"//g;
warn qq~
mysql to excel
by hoowa.sun
=====================
sql: $ARGV[1]
~;
my $dbh =
DBI->connect("DBI:mysql:mysql_compression=$trans_compress;
database=$dbname;host=$hostname",$username,$password);
my $sth = $dbh->prepare("$ARGV[1]") || die $dbh->errstr;
my $rows = $sth->execute() or die $sth->errstr;
warn "rows: $rows found.\n";
my @cols_name = @{$sth->{'NAME'}};
if ($#cols_name > $#cols) {
print "table $ARGV[1] fields out of allow!!(max num. > ".($#cols+1).")\n";
exit;
}
warn "write to: $ARGV[0]\n";
#生成GB2312编码系统
my $map = Unicode::Map->new("gb2312");
#产生报表
my $report = Spreadsheet::WriteExcel::Big->new("$ARGV[0]") || die "不能生成报表文件:$!";
#创建报表的工作表
my $sheet = $report->add_worksheet('data_report');
#创建格式
my $title_style = $report->add_format(); $title_style->set_size(11); $title_style->set_bold(); $title_style->set_align('center');
#初始化数据指针
my $sheet_col = 0;
#创建表格
for (my $i=0;$iset_column($cols[$i], length($cols_name[$i])+4);
$sheet->write_unicode($sheet_col,$i,$map->to_unicode($cols_name[$i]),$title_style);
}
$sheet->freeze_panes(1, 0);#冻结行
while (my @row = $sth->fetchrow_array) {
$sheet_col++;
for (my $i=0;$iwrite_unicode($sheet_col,$i,$map->to_unicode($row[$i]));
}
}
warn "all done!!!\n";
#结束
END {
$report->close() if ($report);
$dbh->disconnect();
}

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10071/showart_185131.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP