最近,由于项目需要,需要利用Perl把MYSQL数据库中的某些表的数据输出到Excel文件中方便打印,备份保存和数据移动。(由于之前未用过Perl,所以学了一下)。
需求描述:使用Perl从数据库中取出数据,把数据输出到Excel中。
问题解决方案:
1、利用Spreadsheet::WriteExcel实现对Excel文件的写入工作。
2、解决向Excel写入中文时产生乱码的问题
3、利用DBI对查询数据库,取出符合条件的数据。
4、解决从MYSQL数据库查询数据库返回结果的中文产生乱码问题
(说明:2和4两个问题是在开发过程中遇到的。)
第一步:利用Spreadsheet::WriteExcel实现对Excel文件的写入工作。
上网查,在使用Perl的情况下,很多人都会说利用Spreadsheet::WriteExcel来实现对对Excel文件的写入功能。利用它的几个函数,就可以方便地把数据写入到Excel相应的位置中,同时还可以设置单元格的格式,如字体大小,单元格大小,是否加粗,底色等等。
下面代码实现一个简单的对Excel的写入工作:
- #!/usr/bin/perl
- use Spreadsheet::WriteExcel;
- #************生成Excel文档****************
- my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
- #生成Excel表
- my $xlsheet = $xl->add_worksheet(“TestSheet”);
- #添加格式(表头)
- $rptheader = $xl->add_format(); # Add a format
- $rptheader->set_bold();
- $rptheader->set_size('12');
- $rptheader->set_align('center');
- #添加格式(表内容)
- $normcell = $xl->add_format(); # Add a format
- $normcell->set_size('9');
- $normcell->set_align('center');
- $normcell->set_bg_color('22');
- #设置列的宽度
- $xlsheet->set_column('A:A',10);
- $xlsheet->set_column('B:B',12);
- $xlsheet->set_column('C:C',17);
- #写表头(格式是使用上面添加的表头格式)
- $xlsheet->write("A2","Number", $rptheader);
- $xlsheet->write("B2","Name",$rptheader);
- $xlsheet->write("C2","Language",$rptheader);
- #写内容(格式是使用上面添加的表内容格式)
- $xlsheet->write("A3","1", $normcell);
- $xlsheet->write("B3","Test",$normcell);
- $xlsheet->write("C3","Perl",$normcell);
- #关闭操作excel的对象.
- $xl->close();
完成。通过上面的代码,可以看出,写入数据到Excel中并不难,主要是用new,add_worksheet,add_format,write这几个函数就可以了。
第二步:解决向Excel写入中文时产生乱码的问题
在第一步中,虽然实现了向Excel写入数据,但只能写入英文数据,如果是写入中文数据,在Excel中会产生中文乱码。同样,表单的名称当插入中文也会有乱码,查了很久,很多人都说用Unicode::Map进行中文写入。但用了它还是解决不了。最后是使用Encode解决了问题。
下面代码实现一个简单的对Excel的写入中文:
- #!/usr/bin/perl
- use Spreadsheet::WriteExcel;
- use Encode;#(这里增加Encode)
- #************生成Excel文档****************
- my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
- #生成Excel表
- my $xlsheet = $xl->add_worksheet(decode('utf8' ,“测试写入Excel”));#(中文名称)
- #添加格式(表头)
- $rptheader = $xl->add_format(); # Add a format
- $rptheader->set_bold();
- $rptheader->set_size('12');
- $rptheader->set_align('center');
- #添加格式(表内容)
- $normcell = $xl->add_format(); # Add a format
- $normcell->set_size('9');
- $normcell->set_align('center');
- $normcell->set_bg_color('22');
- #设置列的宽度
- $xlsheet->set_column('A:A',10);
- $xlsheet->set_column('B:B',12);
- $xlsheet->set_column('C:C',17);
- #写表头(格式是使用上面添加的表头格式)(这里是输入中文)
- $xlsheet->write("A2",decode(‘utf8’,"号码"), $rptheader);
- $xlsheet->write("B2", decode(‘utf8’,"名称"),$rptheader);
- $xlsheet->write("C2", decode(‘utf8’,"语言"),$rptheader);
- #写内容(格式是使用上面添加的表内容格式)(这里是输入中文)
- $xlsheet->write("A3", decode(‘utf8’,"1"), $normcell);
- $xlsheet->write("B3", decode(‘utf8’,"测试"),$normcell);
- $xlsheet->write("C3", decode(‘utf8’,"Perl"),$normcell);
- #关闭操作excel的对象.
- $xl->close();
第三步:利用DBI对查询数据库,取出符合条件的数据
可以向Excel写入数据了,接下来就开始进行查询数据库操作。在Perl中进行数据查询,用的DBI。步骤是先连接数据库:$dbhA = DBI->connect;然后准备查询语句$sth = $dbhA->prepare; 执行SQL语句$sth ->execute();用fetchrow_array()取回数据。
- #!/usr/bin/perl
- use Spreadsheet::WriteExcel;
- use Encode;#
- use DBI; (这里增加DBI)
- #*****连接数据库:数据库名:db_Test ,IP及端口localhost:3306,用户:user ,密码:111****
- my $dbhA = DBI->connect("DBI:mysql:db_Test:localhost:3306", "user", "111")
- or die "Couldn't connect to database: " . DBI->errstr;
- #***准备查询语句
- my $sth = $dbhA->prepare("select * from table1")||die $DBI::errstr;
- #执行查询
- $sth ->execute();
-
- #以下是对Excel操作
- #************生成Excel文档****************
- my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
- #生成Excel表
- my $xlsheet = $xl->add_worksheet(decode('utf8' ,“测试写入Excel”));#(中文名称)
- #添加格式(表头)
- $rptheader = $xl->add_format(); # Add a format
- $rptheader->set_bold();
- $rptheader->set_size('12');
- $rptheader->set_align('center');
- #添加格式(表内容)
- $normcell = $xl->add_format(); # Add a format
- $normcell->set_size('9');
- $normcell->set_align('center');
- $normcell->set_bg_color('22');
- #设置列的宽度
- $xlsheet->set_column('A:A',10);
- $xlsheet->set_column('B:B',12);
- $xlsheet->set_column('C:C',17);
- #写表头(格式是使用上面添加的表头格式)
- $xlsheet->write("A2",decode(‘utf8’,"号码"), $rptheader);
- $xlsheet->write("B2", decode(‘utf8’,"名称"),$rptheader);
- $xlsheet->write("C2", decode(‘utf8’,"语言"),$rptheader);
-
- #循环输出到Excel
- my $num=3; #excel中的位置
- my $record_num = 0; #编号
- while(my @row = $query_statement->fetchrow_array())
- {
- $record_num++;
- $name = $row[1];
- $language = $row[2];
- $xlsheet->write("A$num", decode(‘utf8’,$record_num ), $normcell);
- $xlsheet->write("B$num", decode(‘utf8’, $name),$normcell);
- $xlsheet->write("C$num", decode(‘utf8’, $language),$normcell);
- $num++;
- }
-
- $query_statement->finish();#完成
- $dbhA->disconnect(); #断开数据库连接
- $xl->close(); #关闭操作excel的对象.
以上代码可以实现从数据库查询数据,并放进Excel中。但,如果数据库中有中文,输出到Excel中还是会有乱码。这就要第四步进行解决。
第四步:解决从MYSQL数据库查询数据库返回结果的中文产生乱码问题
要这个问题,就要让返回的结果集支持utf8。set character_set_results=utf8 ,代码如下:
- #!/usr/bin/perl
- use Spreadsheet::WriteExcel;
- use Encode;#
- use DBI;
- #*****连接数据库:数据库名:db_Test ,IP及端口localhost:3306,用户:user ,密码:111****
- my $dbhA = DBI->connect("DBI:mysql:db_Test:localhost:3306", "user", "111")
- or die "Couldn't connect to database: " . DBI->errstr;
- #在结果集中增加中文支持
- my $before = "set character_set_results=utf8";
- my $sth = $dbhA->prepare($before);
- $sth->execute();
-
- #***准备查询语句
- $sth = $dbhA->prepare("select * from table1")||die $DBI::errstr;
- #执行查询
- $sth ->execute();
-
- #以下是对Excel操作
- #************生成Excel文档****************
- my $xl = Spreadsheet::WriteExcel->new("TEST.xls");
- #生成Excel表
- my $xlsheet = $xl->add_worksheet(decode('utf8' ,“测试写入Excel”));#(中文名称)
- #添加格式(表头)
- $rptheader = $xl->add_format(); # Add a format
- $rptheader->set_bold();
- $rptheader->set_size('12');
- $rptheader->set_align('center');
- #添加格式(表内容)
- $normcell = $xl->add_format(); # Add a format
- $normcell->set_size('9');
- $normcell->set_align('center');
- $normcell->set_bg_color('22');
- #设置列的宽度
- $xlsheet->set_column('A:A',10);
- $xlsheet->set_column('B:B',12);
- $xlsheet->set_column('C:C',17);
- #写表头(格式是使用上面添加的表头格式)
- $xlsheet->write("A2",decode(‘utf8’,"号码"), $rptheader);
- $xlsheet->write("B2", decode(‘utf8’,"名称"),$rptheader);
- $xlsheet->write("C2", decode(‘utf8’,"语言"),$rptheader);
-
- #循环输出到Excel
- my $num=3; #excel中的位置
- my $record_num = 0; #编号
- while(my @row = $query_statement->fetchrow_array())
- {
- $record_num++;
- $name = $row[1];
- $language = $row[2];
- $xlsheet->write("A$num", decode(‘utf8’,$record_num ), $normcell);
- $xlsheet->write("B$num", decode(‘utf8’, $name),$normcell);
- $xlsheet->write("C$num", decode(‘utf8’, $language),$normcell);
- $num++;
- }
-
- $query_statement->finish();#完成
- $dbhA->disconnect(); #断开数据库连接
- $xl->close(); #关闭操作excel的对象.
至此,四个步骤已经完成,也完成了需求中所述内容。可以从数据库中查询数据,并写入到Excel中。同时解决中文乱码问题。
以上是个人解决这个需求的方法,大家可以参考并一起交流。