加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl 常用处理函数

发布时间:2020-12-16 00:19:35 所属栏目:大数据 来源:网络整理
导读:?最近,由于项目需要,需要利用 Perl 把 MYSQL 数据库中的某些表的数据输出到 Excel 文件中方便打印,备份保存和数据移动。(由于之前未用过 Perl ,所以学了一下)。 ???? 需求描述:使用 Perl 从数据库中取出数据,把数据输出到 Excel 中。 ???? 问题解决

?最近,由于项目需要,需要利用PerlMYSQL数据库中的某些表的数据输出到Excel文件中方便打印,备份保存和数据移动。(由于之前未用过Perl,所以学了一下)。

????需求描述:使用Perl从数据库中取出数据,把数据输出到Excel中。

????问题解决方案:

1、利用Spreadsheet::WriteExcel实现对Excel文件的写入工作。

2、解决向Excel写入中文时产生乱码的问题

3、利用DBI对查询数据库,取出符合条件的数据。

4、解决从MYSQL数据库查询数据库返回结果的中文产生乱码问题

(说明:24两个问题是在开发过程中遇到的。)

?

第一步:利用Spreadsheet::WriteExcel实现对Excel文件的写入工作。

????上网查,在使用Perl的情况下,很多人都会说利用Spreadsheet::WriteExcel来实现对对Excel文件的写入功能。利用它的几个函数,就可以方便地把数据写入到Excel相应的位置中,同时还可以设置单元格的格式,如字体大小,单元格大小,是否加粗,底色等等。

下面代码实现一个简单的对Excel的写入工作:

[php]? view plain copy
  1. #!/usr/bin/perl??
  2. use?Spreadsheet::WriteExcel;??
  3. #************生成Excel文档****************??
  4. my?$xl?=?Spreadsheet::WriteExcel->new("TEST.xls");??
  5. #生成Excel表??
  6. $xlsheet?=?$xl->add_worksheet(“TestSheet”);??
  7. #添加格式(表头)??
  8. $rptheader?=?$xl->add_format();?#?Add?a?format??
  9. $rptheader->set_bold();??
  10. $rptheader->set_size('12');??
  11. $rptheader->set_align('center');??
  12. #添加格式(表内容)??
  13. $normcell?=?$xl->add_format();?#?Add?a?format??
  14. $normcell->set_size('9');??
  15. $normcell->set_align('center');??
  16. $normcell->set_bg_color('22');??
  17. #设置列的宽度??
  18. $xlsheet->set_column('A:A',10);??
  19. $xlsheet->set_column('B:B',12);??
  20. $xlsheet->set_column('C:C',17);??
  21. #写表头(格式是使用上面添加的表头格式)??
  22. $xlsheet->write("A2","Number",?$rptheader);??
  23. $xlsheet->write("B2","Name",$rptheader);??
  24. $xlsheet->write("C2","Language",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #写内容(格式是使用上面添加的表内容格式)??
  25. $xlsheet->write("A3","1",0); background-color:inherit">$normcell);??
  26. $xlsheet->write("B3","Test",0); background-color:inherit">$normcell);??
  27. $xlsheet->write("C3","Perl",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #关闭操作excel的对象.??
  28. $xl->close();??

?

完成。通过上面的代码,可以看出,写入数据到Excel中并不难,主要是用newadd_worksheetadd_formatwrite这几个函数就可以了。

?

第二步:解决向Excel写入中文时产生乱码的问题

????在第一步中,虽然实现了向Excel写入数据,但只能写入英文数据,如果是写入中文数据,在Excel中会产生中文乱码。同样,表单的名称当插入中文也会有乱码,查了很久,很多人都说用Unicode::Map进行中文写入。但用了它还是解决不了。最后是使用Encode解决了问题。

下面代码实现一个简单的对Excel的写入中文:

copy
    use?Encode;#(这里增加Encode)??
  1. #************生成Excel文档****************??
  2. my?new("TEST.xls");??
  3. #生成Excel表??
  4. $xl->add_worksheet(decode('utf8'?,“测试写入Excel”));#(中文名称)??
  5. #添加格式(表头)??
  6. $rptheader->set_bold();??
  7. $rptheader->set_size('12');??
  8. $rptheader->set_align('center');??
  9. #添加格式(表内容)??
  10. $normcell->set_size('9');??
  11. $normcell->set_align('center');??
  12. $normcell->set_bg_color('22');??
  13. #设置列的宽度??
  14. #写表头(格式是使用上面添加的表头格式)(这里是输入中文)??
  15. "号码"),?decode(‘utf8’,"名称"),"语言"),248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #写内容(格式是使用上面添加的表内容格式)(这里是输入中文)??
  16. "1"),"测试"),"Perl"),248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #关闭操作excel的对象.??
  17. ?

    第三步:利用DBI对查询数据库,取出符合条件的数据

    ??????可以向Excel写入数据了,接下来就开始进行查询数据库操作。在Perl中进行数据查询,用的DBI。步骤是先连接数据库:$dbhA = DBI->connect;然后准备查询语句$sth = $dbhA->prepare;?执行SQL语句$sth ->execute();用fetchrow_array()取回数据。

    [c-sharp]? copy
    #!/usr/bin/perl??
  1. use?Spreadsheet::WriteExcel;??
  2. use?Encode;#??
  3. use?DBI;?(这里增加DBI)??
  4. #*****连接数据库:数据库名:db_Test?,IP及端口localhost:3306,用户:user?,密码:111****??
  5. my?$dbhA?=?DBI->connect("DBI:mysql:db_Test:localhost:3306",?"user",?"111")??
  6. ?or?die?"Couldn't?connect?to?database:?"?.?DBI->errstr;??
  7. #***准备查询语句??
  8. my?$sth?=?$dbhA->prepare("select?*?from?table1")||die?$DBI::errstr;??
  9. #执行查询??
  10. $sth?->execute();??
  11. ?
  12. #以下是对Excel操作??
  13. #************生成Excel文档****************??
  14. my?$xl?=?Spreadsheet::WriteExcel-> #生成Excel表??
  15. my?$xlsheet?=?$xl->add_worksheet(decode('utf8'?,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #添加格式(表头)??
  16. $rptheader?=?$xl->add_format();?#?Add?a?format??
  17. $rptheader->set_bold();??
  18. $rptheader->set_size('12');??
  19. $rptheader->set_align('center');??
  20. #添加格式(表内容)??
  21. $normcell?=?$xl->add_format();?#?Add?a?format??
  22. $normcell->set_size('9');??
  23. $normcell->set_align('center');??
  24. $normcell->set_bg_color('22');??
  25. #设置列的宽度??
  26. $xlsheet->set_column('A:A',248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> $xlsheet->set_column('B:B',108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> $xlsheet->set_column('C:C',248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> #写表头(格式是使用上面添加的表头格式)??
  27. $xlsheet->write("A2",?$rptheader);??
  28. $xlsheet->write("B2",$rptheader);??
  29. $xlsheet->write("C2",$rptheader);??
  30. #循环输出到Excel??
  31. my?$num=3;??#excel中的位置??
  32. my?$record_num?=?0;??#编号??
  33. while(my?@row?=?$query_statement->fetchrow_array())??
  34. {?????
  35. ????$record_num++;??
  36. ????$name?=?$row[1];??
  37. ????$language?=?$row[2];??
  38. ?????????$xlsheet->write("A$num",$record_num?),?$normcell);??
  39. ?????????$xlsheet->write("B$num",?$name),$normcell);??
  40. ?????????$xlsheet->write("C$num",?$language),$normcell);??
  41. ????$num++;??
  42. }??
  43. ??
  44. $query_statement->finish();#完成??
  45. $dbhA->disconnect();??#断开数据库连接??
  46. $xl->close();???#关闭操作excel的对象.??

以上代码可以实现从数据库查询数据,并放进Excel中。但,如果数据库中有中文,输出到Excel中还是会有乱码。这就要第四步进行解决。

?

第四步:解决从MYSQL数据库查询数据库返回结果的中文产生乱码问题

要这个问题,就要让返回的结果集支持utf8set character_set_results=utf8?,代码如下:

copy
    use?Encode;#??
  1. use?DBI;???
  2. #*****连接数据库:数据库名:db_Test?,IP及端口localhost:3306,用户:user?,密码:111****??
  3. $dbhA?=?DBI->connect("DBI:mysql:db_Test:localhost:3306",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ?or?die?"Couldn't?connect?to?database:?"?.?DBI->errstr;??
  4. #在结果集中增加中文支持??
  5. $before?=?"set?character_set_results=utf8";??
  6. $sth?=?$dbhA->prepare($before);??
  7. $sth->execute();??
  8. #***准备查询语句??
  9. $dbhA->prepare("select?*?from?table1")||die?$DBI::errstr;??
  10. #执行查询??
  11. $sth?->execute();??
  12. ??
  13. #以下是对Excel操作??
  14. #循环输出到Excel??
  15. $num=3;??#excel中的位置??
  16. $record_num?=?0;??#编号??
  17. while(my?@row?=?$query_statement->fetchrow_array())??
  18. {?????
  19. ????$record_num++;??
  20. ????$name?=?$row[1];??
  21. $language?=?$row[2];??
  22. ?????????$xlsheet->write("A$num",0); background-color:inherit">$record_num?),108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ?????????$xlsheet->write("B$num",0); background-color:inherit">$name),0); background-color:inherit">$xlsheet->write("C$num",0); background-color:inherit">$language),0); background-color:inherit">$num++;??
  23. }??
  24. $query_statement->finish();#完成??
  25. $dbhA->disconnect();??#断开数据库连接??
  26. $xl->close();???#关闭操作excel的对象.??

?

??? 至此,四个步骤已经完成,也完成了需求中所述内容。可以从数据库中查询数据,并写入到Excel中。同时解决中文乱码问题。

??? 以上是个人解决这个需求的方法,大家可以参考并一起交流。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读