• 【Perl】与【Excel】




    引言

    perl脚本语言对于文本的处理、转换很强大。对于一些信息量庞大的文本文件,看起来不直观,可以将信息提取至excel表格中,增加数据分析的可视化。perl语言的cpan提供了大量模块。对于excel文件的操作主要用到模块:

    Spreadsheet::ParseXLSX

    Excel::Writer::XLSX

    第一个用于对现有excel 表格的解析,第二个用于创建新的excel文件。如果单纯是将文本信息提取到excel表格中其实第二个模块用的更多。看自己需求吧。

    参考 & 鸣谢:

    perl处理Excel(跨平台) - 潘高的小站 (pangao.vip)



    模块安装

    首先确保你的linux系统有perl。

    安装模块的指令 sudo perl -MCPAN -e "install '模块名'"

    sudo perl -MCPAN -e "install 'Spreadsheet::ParseXLSX'"

    sudo perl -MCPAN -e "install 'Excel::Writer::XLSX'"

    安装成功的标志:

    简单示例

    纯粹为了练习而写的脚本,统计学生信息,名字是随机生成的字符串。

    1. #!/usr/bin/perl
    2. =pod
    3. ==========================================
    4. Purpose : Excel Witer Example
    5. Author : On the way , running
    6. Date : 2024-06-16
    7. ==========================================
    8. =cut
    9. use warnings;
    10. use Excel::Writer::XLSX;
    11. # -------------------Create a new Excel workbook
    12. my $xlsx_file_name = "Example.xlsx";
    13. my $workbook = Excel::Writer::XLSX->new( $xlsx_file_name );
    14. print "Create excel file finished!\n";
    15. # Add worksheet
    16. $worksheet1 = $workbook->add_worksheet("Class_1_Information");
    17. $worksheet2 = $workbook->add_worksheet("Class_2_Information");
    18. $worksheet3 = $workbook->add_worksheet("Class_3_Information");
    19. # -------------------Add and define format
    20. $format_first_row = $workbook->add_format();
    21. $format_first_row->set_bold();
    22. # $format_first_row->set_italic();
    23. $format_first_row->set_color( 'white' );
    24. $format_first_row->set_align( 'center' );
    25. $format_first_row->set_align( 'vcenter' );
    26. $format_first_row->set_bg_color( 'blue' );
    27. $format_first_row->set_border(1);
    28. $format_other_row = $workbook->add_format();
    29. # $format_other_row->set_bold();
    30. # $format_other_row->set_italic();
    31. $format_other_row->set_color( 'black' );
    32. $format_other_row->set_align( 'center' );
    33. $format_other_row->set_align( 'vcenter' );
    34. $format_other_row->set_bg_color( 'white' );
    35. $format_other_row->set_border(1);
    36. # -------------------Define functions
    37. # aim to : generate rand data
    38. # call format : func_gen_rand_int_data($lower_bound , $upper_bound);
    39. sub func_gen_rand_int_data {
    40. return int(rand($_[1] - $_[0] + 1)) + $_[0];
    41. }
    42. # function test
    43. print "The generated rand data is " . func_gen_rand_int_data(0,1) . "\n";
    44. # aim to : generate rand string
    45. # call format : func_gen_rand_string($lower_bound , $upper_bound , $string_length);
    46. sub func_gen_rand_string {
    47. my $string_out;
    48. for (my $i = 0; $i < $_[2]; $i++) {
    49. $string_out .= chr(func_gen_rand_int_data( $_[0] , $_[1] ));
    50. }
    51. return $string_out;
    52. }
    53. # function test
    54. print "The generated rand string is " . func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) . "\n";
    55. print "The generated rand string is " . func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) . "\n";
    56. # -------------------Set information
    57. my $class1_student_num = func_gen_rand_int_data(40 , 50);
    58. my $class2_student_num = func_gen_rand_int_data(40 , 50);
    59. my $class3_student_num = func_gen_rand_int_data(40 , 50);
    60. my @table_head = ("Index" , "Student_Name" , "Gender" , "Age" , "Interest");
    61. my @table_Gender = ("Male" , "Female");
    62. my @table_Interest = ("Ping-Pong" , "Football" , "Basketball" , "Swimming" , "Hiking" , "Climbing" , "Game");
    63. # set cell width/height
    64. $worksheet1->set_column('A:E',30);$worksheet1->set_row(0,30);
    65. $worksheet2->set_column('A:E',30);$worksheet2->set_row(0,30);
    66. $worksheet3->set_column('A:E',30);$worksheet3->set_row(0,30);
    67. #-------------------Write Information to excel file
    68. for (my $col = 0; $col < 5; $col++) {
    69. for (my $row = 0; $row < $class1_student_num; $row++) {
    70. if($row == 0){
    71. $worksheet1->write( $row, $col, $table_head[$col], $format_first_row );
    72. }
    73. else{
    74. if ($col == 0) {
    75. $worksheet1->write( $row, $col, $row , $format_other_row );
    76. }
    77. elsif($col == 1) {
    78. $worksheet1->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );
    79. }
    80. elsif($col == 2) {
    81. $worksheet1->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );
    82. }
    83. elsif($col == 3) {
    84. $worksheet1->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );
    85. }
    86. else{
    87. $worksheet1->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );
    88. }
    89. }
    90. }
    91. }
    92. for (my $col = 0; $col < 5; $col++) {
    93. for (my $row = 0; $row < $class2_student_num; $row++) {
    94. if($row == 0){
    95. $worksheet2->write( $row, $col, $table_head[$col], $format_first_row );
    96. }
    97. else{
    98. if ($col == 0) {
    99. $worksheet2->write( $row, $col, $row , $format_other_row );
    100. }
    101. elsif($col == 1) {
    102. $worksheet2->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );
    103. }
    104. elsif($col == 2) {
    105. $worksheet2->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );
    106. }
    107. elsif($col == 3) {
    108. $worksheet2->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );
    109. }
    110. else{
    111. $worksheet2->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );
    112. }
    113. }
    114. }
    115. }
    116. for (my $col = 0; $col < 5; $col++) {
    117. for (my $row = 0; $row < $class3_student_num; $row++) {
    118. if($row == 0){
    119. $worksheet3->write( $row, $col, $table_head[$col], $format_first_row );
    120. }
    121. else{
    122. if ($col == 0) {
    123. $worksheet3->write( $row, $col, $row , $format_other_row );
    124. }
    125. elsif($col == 1) {
    126. $worksheet3->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );
    127. }
    128. elsif($col == 2) {
    129. $worksheet3->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );
    130. }
    131. elsif($col == 3) {
    132. $worksheet3->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );
    133. }
    134. else{
    135. $worksheet3->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );
    136. }
    137. }
    138. }
    139. }
    140. print "Write Done ! please input < soffice $xlsx_file_name > command in you terminal to open the excel file !\n";
    141. $workbook->close();

    结果示意:

  • 相关阅读:
    FPGA设计时序约束二、输入延时与输出延时
    新南威尔士大学研究团队延长量子相干时间实现基准增长100倍
    VSCode配置C/C++环境
    网络安全神器,资深网工必备(全都免费附安装包)
    看完再买不后悔!希喂、小米、霍尼韦尔宠物空气净化器性价比比拼
    CodeForces刷题:Football、Letter、Indian Summer、Codecraft III、Triangular numbers
    Maven插件之maven-assembly-plugin
    M401H_山东移动_SM0_SD46_国科6323芯片_安卓4_AI语音_免拆卡刷固件包
    【业务场景】用户连点
    使用easyPOI导入excel转换为对象集合
  • 原文地址:https://blog.csdn.net/qq_43045275/article/details/139701085