ITPub博客

首页 > 数据库 > 数据库开发技术 > 使用perl读取生成excel表格

使用perl读取生成excel表格

原创 数据库开发技术 作者:liang573728 时间:2019-01-10 13:54:06 0 删除 编辑
在Linux或者Unix上操作(生成)Excel,怎么说都是比较困难的困难的事情。CPAN上提供了Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel这两个模块。下面我们就来看看 Spreadsheet::WriteExcel 和 Spreadsheet::ParseExcel的使用方法:[@more@]

1、首先,我们要在服务器上安装相应的模块

安装 Excel 模块的 PPM 命令

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

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

2、使用读取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";
}
}
}

#!/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";
}
}
}


3、生成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');#设置单元格居中

my $format_title = $workbook->addformat();
$format_title->set_bold();
$format_title->set_italic();
$format_title->set_color("Blue");
$format_title->set_font("Times New Roman");
$format_title->set_size(12);

my $format_header = $workbook->addformat();
$format_header->set_color("Blue");
$format_header->set_bold();
$format_header->set_font("Times New Roman");
$format_header->set_size(10);

my $format_data = $workbook->addformat();
$format_data->set_color("Black");
$format_data->set_font("Times New Roman");
$format_data->set_size(10);

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

$worksheet->write_string(5, 1,"xxx", $format_data);
$worksheet->write_number(5, 4, 200 ,$format_data);

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

exit;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/640706/viewspace-1022699/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2005-10-15

  • 博文量
    152
  • 访问量
    138341