【Perl】与【Excel】

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

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

相关文章

Mac excel 同时冻结首行和首列

1. 选择B2窗格 2. 选择视图 3. 选择冻结窗格 最后首行和首列的分割线加粗了就表示成功了

【服务器运维】CentOS7 minimal 离线安装 gcc perl vmware-tools

0. 本机在有网的情况下,下载CentOS镜像 https://www.centos.org/download/ 1. 取出rpm 有的情况可能不需要net-tools,但是如果出现跟ifconfig相关的错误,就把它安装上。另外如果不想升级内核版本的话,就找对应内核版本的rpm版本安装 perl-Time-Local-1.2300-2.el7.noarch.rpmperl-Tim

LeetCode--171 Excel表列序号

题目 给定一个Excel表格中的列名称,返回其相应的列序号。例如,A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 ... 示例 示例 1:输入: "A"输出: 1示例 2:输入: "AB"输出: 28示例 3:输入: "ZY"输出: 701 class Solution {public:int titleToNumber(strin

浅谈 MySQL for excel

欢迎关注微信公众号“Python生态智联”  MySQL for excel是一个大小只有几兆的MySQL附件,它能让我们在Microsoft excel中处理MySQL数据。小编用了两天时间浏览了MySQL for excel的使用指南并按demo演示了一遍(手册地址https://dev.mysql.com/doc/mysql-for-excel/en/),现从功能和局限两方面对MySQL

Excel实用技巧——二级下拉菜单、数据验证

EXCEL系列文章目录   Excel系列文章是本人亲身经历职场之后萌发的想法,为什么Excel覆盖如此之广,几乎每个公司、学校、家庭都在使用,但是它深藏的宝藏功能却很少被人使用,PQ、BI这些功能同样适用于数据分析;并且在一些需要简单及时的数据分析项目前,Excel是完胜python、R、SPSS这些科学专业的软件的。因此决心开启Excel篇章。 数据分析为什么要学Excel Excel图表

PHP生成csv格式Excel,秒级别实现excel导出功能

防止报超内存,兼容中文,兼容科学技术法。 爽。。。。很爽。。。。 /*** 告诉浏览器下载csv文件* @param string $filename*/public static function downloadCsv($data, $filename, $encoding = 'utf-8'){header("Content-type: text/csv");header("Conten

PHP 读取或生成大的Excel

场景,在很多情况下,需要读取Excel文件。 常用的有PHPExcel包或者使用 maatwebsite/excel 包 但是使用这个包读取或生成excel,如果excel文件过大,很容易出现超内存情况。 解决方法: 上传:要求上传者使用.csv 文件上传。然后使用php自带的 fgetcsv()函数来读取文件。http://php.net/manual/zh/function.fgetc

示例:推荐一个基于第三方开源控件库DataGridFilter封装的FilterColumnDataGrid,可以像Excel拥有列头筛选器

一、目的:基于第三方开源控件库DataGridFilter封装的FilterColumnDataGrid,可以像Excel拥有列头筛选器,感兴趣的可以去下方链接地址查看开源控件库地址。本控件封装的目的在于将第三方库的皮肤和样式封装到皮肤库中可统一设置样式,同时生成nuget方便调用 二、效果如下 三、环境 VS2022 Net7 四、使用方式 1、安装nuget包:H.Con

poi生成的excel,输入数字后变成1.11111111111111E+23

poi版本4.1.2 生成excel后,单元格输入数字,过长的话变成这样 解决:生成的时候设置单元格格式为文本格式 import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;import java.io.IOEx

Excel做简单的趋势预测

这种方法不能代替机器学习,时序分析等,只是为后面的时序预测提供一个经验认识。 step1         选中序号列(或时间列)与预测列如图1所示: 图1 step2         工具栏点击“数据”,然后再“数据”下点击“预测模型”,如图2所示: 图2 step3         这样就会跳出窗口,此处可以进行一些参数设置如图3所示: 图3