【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

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

Java利用poi实现word表格转excel

《Java利用poi实现word表格转excel》这篇文章主要为大家详细介绍了Java如何利用poi实现word表格转excel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、每行对象类需要针对不同的表格进行对应的创建。package org.example.wordToEx

利用Python实现添加或读取Excel公式

《利用Python实现添加或读取Excel公式》Excel公式是数据处理的核心工具,从简单的加减运算到复杂的逻辑判断,掌握基础语法是高效工作的起点,下面我们就来看看如何使用Python进行Excel公... 目录python Excel 库安装Python 在 Excel 中添加公式/函数Python 读取

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)

《Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)》:本文主要介绍Java导入、导出excel的相关资料,讲解了使用Java和ApachePOI库将数据导出为Excel文件,包括... 目录前言一、引入Apache POI依赖二、用法&步骤2.1 创建Excel的元素2.3 样式和字体2.

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

python多种数据类型输出为Excel文件

《python多种数据类型输出为Excel文件》本文主要介绍了将Python中的列表、元组、字典和集合等数据类型输出到Excel文件中,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一.列表List二.字典dict三.集合set四.元组tuplepython中的列表、元组、字典