【Perl】与【Excel】

2024-06-17 03:28
文章标签 excel perl

本文主要是介绍【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'"

安装成功的标志:

示例

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

#!/usr/bin/perl=pod
==========================================
Purpose : Excel Witer Example
Author  : On the way , running
Date      : 2024-06-16
==========================================
=cutuse warnings;
use Excel::Writer::XLSX;# -------------------Create a new Excel workbook
my $xlsx_file_name = "Example.xlsx";
my $workbook = Excel::Writer::XLSX->new( $xlsx_file_name );
print "Create excel file finished!\n";# Add worksheet
$worksheet1 = $workbook->add_worksheet("Class_1_Information");
$worksheet2 = $workbook->add_worksheet("Class_2_Information");
$worksheet3 = $workbook->add_worksheet("Class_3_Information");# -------------------Add and define format
$format_first_row = $workbook->add_format();
$format_first_row->set_bold();
# $format_first_row->set_italic();
$format_first_row->set_color( 'white' );
$format_first_row->set_align( 'center' );
$format_first_row->set_align( 'vcenter' );
$format_first_row->set_bg_color( 'blue' );
$format_first_row->set_border(1);$format_other_row = $workbook->add_format();
# $format_other_row->set_bold();
# $format_other_row->set_italic();
$format_other_row->set_color( 'black' );
$format_other_row->set_align( 'center' );
$format_other_row->set_align( 'vcenter' );
$format_other_row->set_bg_color( 'white' );
$format_other_row->set_border(1);# -------------------Define functions 
# aim to : generate rand data  
# call format : func_gen_rand_int_data($lower_bound , $upper_bound);
sub func_gen_rand_int_data {return int(rand($_[1] - $_[0] + 1)) + $_[0];
}
# function test 
print "The generated rand data is " . func_gen_rand_int_data(0,1) . "\n";# aim to : generate rand string  
# call format : func_gen_rand_string($lower_bound , $upper_bound , $string_length);
sub func_gen_rand_string {my $string_out;for (my $i = 0; $i < $_[2]; $i++) {$string_out .= chr(func_gen_rand_int_data( $_[0] , $_[1] ));}return $string_out;
}
# function test 
print "The generated rand string is " . func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) . "\n";
print "The generated rand string is " . func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) . "\n";# -------------------Set information
my $class1_student_num = func_gen_rand_int_data(40 , 50);
my $class2_student_num = func_gen_rand_int_data(40 , 50);
my $class3_student_num = func_gen_rand_int_data(40 , 50);my @table_head = ("Index" , "Student_Name" , "Gender" , "Age" , "Interest");
my @table_Gender = ("Male" , "Female");
my @table_Interest = ("Ping-Pong" , "Football" , "Basketball" , "Swimming" , "Hiking" , "Climbing" , "Game");# set cell width/height
$worksheet1->set_column('A:E',30);$worksheet1->set_row(0,30);
$worksheet2->set_column('A:E',30);$worksheet2->set_row(0,30);
$worksheet3->set_column('A:E',30);$worksheet3->set_row(0,30);#-------------------Write Information to excel filefor (my $col = 0; $col < 5; $col++) {for (my $row = 0; $row < $class1_student_num; $row++) {if($row == 0){$worksheet1->write( $row, $col, $table_head[$col], $format_first_row );}else{if ($col == 0) {$worksheet1->write( $row, $col, $row , $format_other_row );}elsif($col == 1) {$worksheet1->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );}elsif($col == 2) {$worksheet1->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );}elsif($col == 3) {$worksheet1->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );}else{$worksheet1->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );}}}
}for (my $col = 0; $col < 5; $col++) {for (my $row = 0; $row < $class2_student_num; $row++) {if($row == 0){$worksheet2->write( $row, $col, $table_head[$col], $format_first_row );}else{if ($col == 0) {$worksheet2->write( $row, $col, $row , $format_other_row );}elsif($col == 1) {$worksheet2->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );}elsif($col == 2) {$worksheet2->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );}elsif($col == 3) {$worksheet2->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );}else{$worksheet2->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );}}}
}for (my $col = 0; $col < 5; $col++) {for (my $row = 0; $row < $class3_student_num; $row++) {if($row == 0){$worksheet3->write( $row, $col, $table_head[$col], $format_first_row );}else{if ($col == 0) {$worksheet3->write( $row, $col, $row , $format_other_row );}elsif($col == 1) {$worksheet3->write( $row, $col, func_gen_rand_string( 65 , 90 , func_gen_rand_int_data(10 , 15)) , $format_other_row );}elsif($col == 2) {$worksheet3->write( $row, $col, $table_Gender[func_gen_rand_int_data(0 , 1)], $format_other_row );}elsif($col == 3) {$worksheet3->write( $row, $col, func_gen_rand_int_data(15, 20), $format_other_row );}else{$worksheet3->write( $row, $col, $table_Interest[func_gen_rand_int_data(0 , 6)], $format_other_row );}}}
}
print "Write Done ! please input < soffice $xlsx_file_name > command in you terminal to open the excel file !\n";$workbook->close();

结果示意:

这篇关于【Perl】与【Excel】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068358

相关文章

Python创建Excel的4种方式小结

《Python创建Excel的4种方式小结》这篇文章主要为大家详细介绍了Python中创建Excel的4种常见方式,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以学习一下... 目录库的安装代码1——pandas代码2——openpyxl代码3——xlsxwriterwww.cppcns.c

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

使用Python在Excel中插入、修改、提取和删除超链接

《使用Python在Excel中插入、修改、提取和删除超链接》超链接是Excel中的常用功能,通过点击超链接可以快速跳转到外部网站、本地文件或工作表中的特定单元格,有效提升数据访问的效率和用户体验,这... 目录引言使用工具python在Excel中插入超链接Python修改Excel中的超链接Python

如何利用Python实现给Excel表格截图

《如何利用Python实现给Excel表格截图》这篇文章主要为大家详细介绍了如何利用Python实现给Excel表格截图功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 我搜索了网络上的方案,感觉把 Excel 表格转换为 html 再用 platwright 截图是比China编程较顺

Java导出Excel动态表头的示例详解

《Java导出Excel动态表头的示例详解》这篇文章主要为大家详细介绍了Java导出Excel动态表头的相关知识,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录前言一、效果展示二、代码实现1.固定头实体类2.动态头实现3.导出动态头前言本文只记录大致思路以及做法,代码不进

10个Python Excel自动化脚本分享

《10个PythonExcel自动化脚本分享》在数据处理和分析的过程中,Excel文件是我们日常工作中常见的格式,本文将分享10个实用的Excel自动化脚本,希望可以帮助大家更轻松地掌握这些技能... 目录1. Excel单元格批量填充2. 设置行高与列宽3. 根据条件删除行4. 创建新的Excel工作表5

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

详解Vue如何使用xlsx库导出Excel文件

《详解Vue如何使用xlsx库导出Excel文件》第三方库xlsx提供了强大的功能来处理Excel文件,它可以简化导出Excel文件这个过程,本文将为大家详细介绍一下它的具体使用,需要的小伙伴可以了解... 目录1. 安装依赖2. 创建vue组件3. 解释代码在Vue.js项目中导出Excel文件,使用第三