How to read and write Excel package, xls, and xlsx in perl
This article mainly introduces how to read and write Excel packages, xls, and xlsx in perl, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Introduce two packages in perl, which can read Excel files, including 2003 xls files and 2007 xlsx files. Spreadsheet::ParseExcel; Spreadsheet::ParseXLSX
This paper introduces two packages of two perl, which can read Excel files, including 2003 xls files Spreadsheet::ParseExcel, and 2007 xlsx files Spreadsheet::ParseXLSX.
Use Spreadsheet::ParseExcel;# reads the Excel2003 version of the excel file my $parser = Spreadsheet::ParseExcel- > new (); my $workbook = $parser- > parse ('test.xls'); if (! defined $workbook) {die $parser- > error (), ".\ n";} my $worksheet = $workbook- > worksheet (' Sheet1'); my ($row_min, $row_max) = $worksheet- > row_range (); my ($col_min, $col_max) = $worksheet- > col_range () Printf "s s s", 'first_row','second_row','third_row'. "\ n"; for my $row ($row_min.. $row_max) {for my $col ($col_min.. Col_max) {my $cell = $worksheet- > get_cell ($row, $col); next unless $cell; my $va=$cell- > value (); printf "s", $va;} say;} use Spreadsheet::ParseXLSX;# almost my $parser = Spreadsheet::ParseXLSX- > new;my $workbook = $parser- > parse ("file.xlsx"); my $worksheet = $workbook- > worksheet ('Sheet1') My ($row_min, $row_max) = $worksheet- > row_range (); my ($col_min, $col_max) = $worksheet- > col_range (); printf "s s s", 'first_row','second_row','third_row'. "\ n"; for my $row ($row_min. $row_max) {for my $col ($col_min.. $col_max) {my $cell = $worksheet- > get_cell ($row, $col); next unless $cell; my $va=$cell- > value (); printf "s", $va;} say;}
More help: https://metacpan.org/pod/Spreadsheet::ParseExcel
There is also a package that writes excel: Spreadsheet::WriteExcel (2003) and Excel::Writer::XLSX (2007) are used the same way:
Reference Code:
Use Spreadsheet::WriteExcel; # Create a new Excel workbookmy $workbook = Spreadsheet::WriteExcel- > new ('perl.xls'); # Add a worksheet$worksheet = $workbook- > add_worksheet (); # Add and define a format$format = $workbook- > add_format (); # Add a format$format- > set_bold (); $format- > set_color (' red'); $format- > set_align ('center'); # Write a formatted and unformatted string, row and column notation.$col = $row = 0 quality worksheet-> write ($row, $col,' Hi Excelsiors, $format) $worksheet- > write (1, $col,'Hi Excelsior'); # Write a number and a formula using A1 notation$worksheet- > write ('A3 steps, 1.2345); $worksheet- > write ('A4 steps,'= SIN (PI () / 4)')
Reference Code:
Use Excel::Writer::XLSX; # Create a new Excel workbookmy $workbook = Excel::Writer::XLSX- > new ('perl.xlsx'); # Add a worksheet$worksheet = $workbook- > add_worksheet (); # Add and define a format$format = $workbook- > add_format (); $format- > set_bold (); $format- > set_color (' red'); $format- > set_align ('center'); # Write a formatted and unformatted string, row and column notation.$col = $row = 0 $worksheet- > write ($row, $col,'Hi Excelsiors, $format); $worksheet- > write (1, $col,'Hi excels'); # Write a number and a formula using A1 notation$worksheet- > write ('A3 steps, 1.2345); $worksheet- > write ('A4 steps,'= SIN (PI () / 4)'); $workbook- > close () Thank you for reading this article carefully. I hope the article "how to read and write Excel packages, xls, and xlsx in perl" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!