Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write Excel by Perl

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report