Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read and write Excel by Perl

Shulou Source: shulou.com Published: 2022-06-01 13:03:07 10月23日 Update

This article mainly introduces Perl how to read and write Excel, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Most of the .xls files used in Shengxin analysis are text format files, which can be read by Perl, but not when you encounter real excel files. At this time, you need to use modules.

The modules used are:

Spreadsheet::ParseExcel

Spreadsheet::WriteExcel

Here are two code to read and write Excel, which can be used for reference if necessary.

Read Excel

#! / usr/bin/perl-wuse strict;use Spreadsheet::ParseExcel;use Spreadsheet::ParseExcel::FmtUnicode;my $parser = Spreadsheet::ParseExcel- > new (); my $formatter = Spreadsheet::ParseExcel::FmtUnicode- > new (Unicode_Map= > "CP936"); # Correction of Chinese garbled my $workbook = $parser- > parse ($ARGV [0], $formatter); if (! defined $workbook) {die $parser- > error (), ".\ n" } for my $worksheet ($workbook- > worksheets ()) {# traversal sheet my ($row_min, $row_max) = $worksheet- > row_range (); my ($col_min, $col_max) = $worksheet- > col_range (); for my $row ($row_min. $row_max) {# traverse rows and rows for my $col ($col_min. Col_max) {my $cell = $worksheet- > get_cell ($row, $col); next unless $cell; print "Row, Col = ($row, $col)\ n"; print "Value =", $cell- > value (), "\ n"; # read the information print "\ n" in the cell }} print "\ nA sheet has been read.\ n\ n";}

Write Excel

#! / usr/bin/perl-w use Spreadsheet::WriteExcel; # create a new EXCEL file my $workbook = Spreadsheet::WriteExcel- > new ('Hello.xls'); # add a worksheet $worksheet = $workbook- > add_worksheet (); # create a new style $format = $workbook- > add_format (); # Add a format $format- > set_bold (); # set the font to bold $format- > set_color (' red') # set the cell foreground color to red $format- > set_align ('center'); # set the cell to be centered # write a formatted and unformatted string $col = $row = 0 to the cell using line and column numbers; $worksheet- > write ($row, $col,' Hi Excelsiors, $format); $worksheet- > write (1, $col,'Hi excels') Foreach $row (10.. 20) {foreach $col (10.. 20) {$worksheet- > write ($row, $col, "$row+$col", $format);} # write a number to the cell using the cell name (for example: A1). $worksheet- > write ('A3levels, 1.2345); $worksheet- > write (' A4steps,'= SIN (PI () / 4)'); # add more worksheets $worksheet = $workbook- > add_worksheet (); foreach $row (10.. 20) {foreach $col (10.. 20) {$worksheet- > write ($row, $col, "$row+$col", $format);} exit Thank you for reading this article carefully. I hope the article "Perl how to read and write Excel" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Unit file article format more module reference work no two garbled code value information interest at the same time name font character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei MariaDB OPPO Reno Shulou Technology