【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'"

安装成功的标志:

简单示例

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

perl 复制代码
#!/usr/bin/perl

=pod
==========================================
Purpose : Excel Witer Example
Author  : On the way , running
Date      : 2024-06-16
==========================================
=cut

use 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 file

for (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();

结果示意:

相关推荐
JJJJ_iii10 小时前
【左程云算法09】栈的入门题目-最小栈
java·开发语言·数据结构·算法·时间复杂度
枫叶丹410 小时前
【Qt开发】显示类控件(三)-> QProgressBar
开发语言·qt
Bear on Toilet10 小时前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
码猿宝宝11 小时前
浏览器中javascript时间线,从加载到执行
开发语言·javascript·ecmascript
OEC小胖胖11 小时前
App Router vs. Pages Router:我应该如何选择?
开发语言·前端·前端框架·web·next.js
max50060011 小时前
OpenSTL PredRNNv2 模型复现与自定义数据集训练
开发语言·人工智能·python·深度学习·算法
Pocker_Spades_A11 小时前
Python快速入门专业版(二十八):函数参数进阶:默认参数与可变参数(*args/**kwargs)
开发语言·python
努力努力再努力wz11 小时前
【c++进阶系列】:map和set的模拟实现(附模拟实现的源码)
java·linux·运维·开发语言·c++
Cloud Traveler12 小时前
8.FC平台模块梳理
java·linux·开发语言
歪歪10012 小时前
webpack 配置文件中 mode 有哪些模式?
开发语言·前端·javascript·webpack·前端框架·node.js