Perl操作Excel的示例代码

来源:爱站网时间:2018-12-26编辑:网友分享
Perl操作Excel的示例代码,在Linux或者Unix上操作(生成)Excel,CPAN上提供了Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel这两个模块,下面就跟随爱站技术小编来看看 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel的使用方法。

Perl操作Excel的示例代码,在Linux或者Unix上操作(生成)Excel,CPAN上提供了Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel这两个模块,下面就跟随爱站技术小编来看看 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel的使用方法。

首先,要在服务器上安装相应的模块。
安装 Excel 模块的 PPM 命令

复制代码 代码如下:

ppm> install OLE::Storage_Lite
ppm> install Spreadsheet::ParseExcel
ppm> install Spreadsheet::WriteExcel


来看两个例子吧。
例1:读取excel文件

 

复制代码 代码如下:

 


#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;

my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('Book1.xls');

for my $worksheet ( $workbook->worksheets() ) {

my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();

for my $row ( $row_min .. $row_max ) {
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";
print "Unformatted = ", $cell->unformatted(), "\n";
print "\n";
}
}
}


例2:生成EXCEL文件

 

复制代码 代码如下:

 


#!/usr/bin/perl -w

use Spreadsheet::WriteExcel;

# 创建一个新的EXCEL文件
my $workbook = Spreadsheet::WriteExcel->new('test.xls');

# 添加一个工作表
$worksheet = $workbook->add_worksheet();

# 新建一个样式
$format = $workbook->add_format(); # Add a format
$format->set_bold();#设置字体为粗体
$format->set_color('red');#设置单元格前景色为红色
$format->set_align('center');#设置单元格居中

#使用行号及列号,向单元格写入一个格式化和末格式化的字符串
$col = $row = 0;
$worksheet->write($row, $col, 'Hi Excel!', $format);
$worksheet->write(1, $col, 'Hi Excel!');

# 使用单元格名称(例:A1),向单元格中写一个数字。
$worksheet->write('A3', 1.2345);
$worksheet->write('A4', '=SIN(PI()/4)');
exit;

 以上就是Perl操作Excel的示例代码,清楚明白了吗,更多关于Perl的内容请继续关注爱站技术频道。

上一篇:perl 删除数组元素的3种方法

下一篇:用perl编写的单位电脑信息采集程序

您可能感兴趣的文章

相关阅读

查看更多 >

热门软件源码

最新软件源码下载