perl 常用處理函數

 最近,由於項目需要,需要利用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的寫入工作:

  1. #!/usr/bin/perl  
  2. use Spreadsheet::WriteExcel;  
  3. #************生成Excel文檔****************  
  4. my $xl = Spreadsheet::WriteExcel->new("TEST.xls");  
  5. #生成Excel表  
  6. my $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",$rptheader);  
  25. #寫內容(格式是使用上面添加的表內容格式)  
  26. $xlsheet->write("A3","1"$normcell);  
  27. $xlsheet->write("B3","Test",$normcell);  
  28. $xlsheet->write("C3","Perl",$normcell);  
  29. #關閉操作excel的對象.  
  30. $xl->close();  

 

完成。通過上面的代碼,可以看出,寫入數據到Excel中並不難,主要是用newadd_worksheetadd_formatwrite這幾個函數就可以了。

 

第二步:解決向Excel寫入中文時產生亂碼的問題

    在第一步中,雖然實現了向Excel寫入數據,但只能寫入英文數據,如果是寫入中文數據,在Excel中會產生中文亂碼。同樣,表單的名稱當插入中文也會有亂碼,查了很久,很多人都說用Unicode::Map進行中文寫入。但用了它還是解決不了。最後是使用Encode解決了問題。

下面代碼實現一個簡單的對Excel的寫入中文:

  1. #!/usr/bin/perl  
  2. use Spreadsheet::WriteExcel;  
  3. use Encode;#(這裏增加Encode)  
  4. #************生成Excel文檔****************  
  5. my $xl = Spreadsheet::WriteExcel->new("TEST.xls");  
  6. #生成Excel表  
  7. my $xlsheet = $xl->add_worksheet(decode('utf8' ,“測試寫入Excel”));#(中文名稱)  
  8. #添加格式(表頭)  
  9. $rptheader = $xl->add_format(); # Add a format  
  10. $rptheader->set_bold();  
  11. $rptheader->set_size('12');  
  12. $rptheader->set_align('center');  
  13. #添加格式(表內容)  
  14. $normcell = $xl->add_format(); # Add a format  
  15. $normcell->set_size('9');  
  16. $normcell->set_align('center');  
  17. $normcell->set_bg_color('22');  
  18. #設置列的寬度  
  19. $xlsheet->set_column('A:A',10);  
  20. $xlsheet->set_column('B:B',12);  
  21. $xlsheet->set_column('C:C',17);  
  22. #寫表頭(格式是使用上面添加的表頭格式)(這裏是輸入中文)  
  23. $xlsheet->write("A2",decode(‘utf8’,"號碼"), $rptheader);  
  24. $xlsheet->write("B2", decode(‘utf8’,"名稱"),$rptheader);  
  25. $xlsheet->write("C2", decode(‘utf8’,"語言"),$rptheader);  
  26. #寫內容(格式是使用上面添加的表內容格式)(這裏是輸入中文)  
  27. $xlsheet->write("A3", decode(‘utf8’,"1"), $normcell);  
  28. $xlsheet->write("B3", decode(‘utf8’,"測試"),$normcell);  
  29. $xlsheet->write("C3", decode(‘utf8’,"Perl"),$normcell);  
  30. #關閉操作excel的對象.  
  31. $xl->close();  

 

第三步:利用DBI對查詢數據庫,取出符合條件的數據

      可以向Excel寫入數據了,接下來就開始進行查詢數據庫操作。在Perl中進行數據查詢,用的DBI。步驟是先連接數據庫:$dbhA = DBI->connect;然後準備查詢語句$sth = $dbhA->prepare; 執行SQL語句$sth ->execute();用fetchrow_array()取回數據。

[c-sharp] view plaincopy
  1. #!/usr/bin/perl  
  2. use Spreadsheet::WriteExcel;  
  3. use Encode;#  
  4. use DBI; (這裏增加DBI)  
  5. #*****連接數據庫:數據庫名:db_Test ,IP及端口localhost:3306,用戶:user ,密碼:111****  
  6. my $dbhA = DBI->connect("DBI:mysql:db_Test:localhost:3306""user""111")  
  7.  or die "Couldn't connect to database: " . DBI->errstr;  
  8. #***準備查詢語句  
  9. my $sth = $dbhA->prepare("select * from table1")||die $DBI::errstr;  
  10. #執行查詢  
  11. $sth ->execute();  
  12.  
  13. #以下是對Excel操作  
  14. #************生成Excel文檔****************  
  15. my $xl = Spreadsheet::WriteExcel->new("TEST.xls");  
  16. #生成Excel表  
  17. my $xlsheet = $xl->add_worksheet(decode('utf8' ,“測試寫入Excel”));#(中文名稱)  
  18. #添加格式(表頭)  
  19. $rptheader = $xl->add_format(); # Add a format  
  20. $rptheader->set_bold();  
  21. $rptheader->set_size('12');  
  22. $rptheader->set_align('center');  
  23. #添加格式(表內容)  
  24. $normcell = $xl->add_format(); # Add a format  
  25. $normcell->set_size('9');  
  26. $normcell->set_align('center');  
  27. $normcell->set_bg_color('22');  
  28. #設置列的寬度  
  29. $xlsheet->set_column('A:A',10);  
  30. $xlsheet->set_column('B:B',12);  
  31. $xlsheet->set_column('C:C',17);  
  32. #寫表頭(格式是使用上面添加的表頭格式)  
  33. $xlsheet->write("A2",decode(‘utf8’,"號碼"), $rptheader);  
  34. $xlsheet->write("B2", decode(‘utf8’,"名稱"),$rptheader);  
  35. $xlsheet->write("C2", decode(‘utf8’,"語言"),$rptheader);  
  36.  
  37. #循環輸出到Excel  
  38. my $num=3;  #excel中的位置  
  39. my $record_num = 0;  #編號  
  40. while(my @row = $query_statement->fetchrow_array())  
  41. {     
  42.     $record_num++;  
  43.     $name = $row[1];  
  44.     $language = $row[2];  
  45.          $xlsheet->write("A$num", decode(‘utf8’,$record_num ), $normcell);  
  46.          $xlsheet->write("B$num", decode(‘utf8’, $name),$normcell);  
  47.          $xlsheet->write("C$num", decode(‘utf8’, $language),$normcell);  
  48.     $num++;  
  49. }  
  50.   
  51. $query_statement->finish();#完成  
  52. $dbhA->disconnect();  #斷開數據庫連接  
  53. $xl->close();   #關閉操作excel的對象.  

以上代碼可以實現從數據庫查詢數據,並放進Excel中。但,如果數據庫中有中文,輸出到Excel中還是會有亂碼。這就要第四步進行解決。

 

第四步:解決從MYSQL數據庫查詢數據庫返回結果的中文產生亂碼問題

要這個問題,就要讓返回的結果集支持utf8set character_set_results=utf8 ,代碼如下:

  1. #!/usr/bin/perl  
  2. use Spreadsheet::WriteExcel;  
  3. use Encode;#  
  4. use DBI;   
  5. #*****連接數據庫:數據庫名:db_Test ,IP及端口localhost:3306,用戶:user ,密碼:111****  
  6. my $dbhA = DBI->connect("DBI:mysql:db_Test:localhost:3306""user""111")  
  7.  or die "Couldn't connect to database: " . DBI->errstr;  
  8. #在結果集中增加中文支持  
  9. my $before = "set character_set_results=utf8";  
  10. my $sth = $dbhA->prepare($before);  
  11. $sth->execute();  
  12.   
  13. #***準備查詢語句  
  14. $sth = $dbhA->prepare("select * from table1")||die $DBI::errstr;  
  15. #執行查詢  
  16. $sth ->execute();  
  17.   
  18. #以下是對Excel操作  
  19. #************生成Excel文檔****************  
  20. my $xl = Spreadsheet::WriteExcel->new("TEST.xls");  
  21. #生成Excel表  
  22. my $xlsheet = $xl->add_worksheet(decode('utf8' ,“測試寫入Excel”));#(中文名稱)  
  23. #添加格式(表頭)  
  24. $rptheader = $xl->add_format(); # Add a format  
  25. $rptheader->set_bold();  
  26. $rptheader->set_size('12');  
  27. $rptheader->set_align('center');  
  28. #添加格式(表內容)  
  29. $normcell = $xl->add_format(); # Add a format  
  30. $normcell->set_size('9');  
  31. $normcell->set_align('center');  
  32. $normcell->set_bg_color('22');  
  33. #設置列的寬度  
  34. $xlsheet->set_column('A:A',10);  
  35. $xlsheet->set_column('B:B',12);  
  36. $xlsheet->set_column('C:C',17);  
  37. #寫表頭(格式是使用上面添加的表頭格式)  
  38. $xlsheet->write("A2",decode(‘utf8’,"號碼"), $rptheader);  
  39. $xlsheet->write("B2", decode(‘utf8’,"名稱"),$rptheader);  
  40. $xlsheet->write("C2", decode(‘utf8’,"語言"),$rptheader);  
  41.   
  42. #循環輸出到Excel  
  43. my $num=3;  #excel中的位置  
  44. my $record_num = 0;  #編號  
  45. while(my @row = $query_statement->fetchrow_array())  
  46. {     
  47.     $record_num++;  
  48.     $name = $row[1];  
  49.     $language = $row[2];  
  50.          $xlsheet->write("A$num", decode(‘utf8’,$record_num ), $normcell);  
  51.          $xlsheet->write("B$num", decode(‘utf8’, $name),$normcell);  
  52.          $xlsheet->write("C$num", decode(‘utf8’, $language),$normcell);  
  53.     $num++;  
  54. }  
  55.   
  56. $query_statement->finish();#完成  
  57. $dbhA->disconnect();  #斷開數據庫連接  
  58. $xl->close();   #關閉操作excel的對象.  

 

    至此,四個步驟已經完成,也完成了需求中所述內容。可以從數據庫中查詢數據,並寫入到Excel中。同時解決中文亂碼問題。

    以上是個人解決這個需求的方法,大家可以參考並一起交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章