首页 > 代码库 > perl 写excel,支持中文
perl 写excel,支持中文
e warnings; use DBI; use Spreadsheet::WriteExcel; use Encode; #support Chinese # connect my $dbh = DBI->connect("DBI:mysql:database=newbbk;host=127.0.0.1;port=3306", "root", "root", {'RaiseError' => 1,AutoCommit => 0}); $dbh->do('set names utf8'); $dbh->commit(); &fix_school_by_city(); $dbh->disconnect(); sub fix_school_by_city(){ my $statement = "SELECT province_id, province_name from d_province;"; my $sth = $dbh->prepare($statement); unless($sth){ print "Can't prepare $statement: $dbh->errstr/n"; return; } my $rv = $sth->execute(); unless($rv){ print "can't execute the query: $sth->errstr"; return; } while(my @row = $sth->fetchrow_array) { my ($pid,$pname) = @row[0..1]; next unless($pid =~/^(\d)+$/ and $pname); my $dest_pbook = Spreadsheet::WriteExcel->new("$pname.xls"); # not support xlsx my $cstatement = "SELECT city_id, city_name from d_city where province_id=$pid;"; my $csth = $dbh->prepare($cstatement); my $crv = $csth->execute(); return unless($crv); while(my @crow = $csth->fetchrow_array) { my ($cid,$cname) = @crow[0..1]; next unless($cid =~/^(\d)+$/ and $cname); my $worksheet=$dest_pbook->add_worksheet(decode('UTF8',"$cname")); my $sstatement = "SELECT school_id,school_name from d_school where region_id=$cid;"; my $ssth = $dbh->prepare($sstatement); my $srv = $ssth->execute(); return unless($srv); my $i=1; $worksheet->write(0,0,decode('UTF8','学校id')); $worksheet->write(0,1,decode('UTF8',"学校名称")); while(my @srow = $ssth->fetchrow_array) { my ($sid,$sname) = @srow[0..1]; next unless($sid =~/^(\d)+$/ and $sname); $worksheet->write($i,0,decode('UTF8',$sid)); $worksheet->write($i,1,decode('UTF8',"$sname")); $i++; } } $dest_pbook->close(); } }
说明:将数据库中的学校信息,按省分成excel文件,按市分成worksheet
支持中文
perl 写excel,支持中文
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。