我有一个excel文件。它有5列。
1st column: URL
2nd column: Pagerank
3rd column: Niche
4th column: Language
5th column: IP
总共有2000行。
我只想提取出Niche == Poker
所在的行,并将其保存在一个新的excel文件中。我如何在Perl中做到这一点?
要读取/解析Excel文件,可以使用:
- CCD_ 2;旧的";后缀为``的Excel文件(Excel 97-2003)
Spreadsheet-XLSX
用于后缀为.xlsx
的新Excel文件(Excel 2007+)
一旦你提取了你想要的数据,就可以写另一个excel文件:
-
CCD_ 5;旧的";带
.xls
后缀的Excel文件(Excel 97-2003)Spreadsheet::WriteExcel
文档包含许多示例,开发人员网站提供了更多信息。 -
Excel::Writer::XLSX
用于带有.xlsx
后缀的新Excel文件(Excel 2007+)
下面是一个简短的工作示例,使用Spreadsheet::ParseExcel读取输入文件,使用Spread::WriteExcel写出匹配的数据。
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
# Read the input and output filenames.
my $in_filename = shift;
my $out_filename = shift;
# Simple check for valid arguments.
if ( !$in_filename || !$out_filename ) {
die( "Usage: poker_extract oldfile.xls newfile.xlsn" );
}
# Create a parser to read the input Excel file.
my $parser = Spreadsheet::ParseExcel->new();
my $in_workbook = $parser->parse( $in_filename );
# Check for any parse errors.
if ( !defined $in_workbook ) {
die "Parsing error: ", $parser->error(), ".n";
}
# Create a writer to store the new Excel data.
my $out_workbook = Spreadsheet::WriteExcel->new( $out_filename );
# Read the data from the first worksheet (or whichever).
my $in_worksheet = $in_workbook->worksheet( 0 );
my $out_worksheet = $out_workbook->add_worksheet();
my $out_row = 0;
# Get the data range for the input worksheet.
my ( $row_min, $row_max ) = $in_worksheet->row_range();
my ( $col_min, $col_max ) = $in_worksheet->col_range();
# Loop over the input rows.
for my $in_row ( $row_min .. $row_max ) {
# Check the cell in the third column.
my $cell = $in_worksheet->get_cell( $in_row, 2 );
if ( $cell && $cell->value() eq 'Poker' ) {
# We have a matched row. Read the rest of the row data.
for my $col ( $col_min .. $col_max ) {
# Get a cell object.
my $cell = $in_worksheet->get_cell( $in_row, $col );
# Skip to the next cell if this one is empty.
next unless $cell;
# Store the data in the output file.
$out_worksheet->write( $out_row, $col, $cell->unformatted() );
}
$out_row++;
}
}
__END__
如何用Perl读取Excel文件?
http://www.ibm.com/developerworks/linux/library/l-pexcel/
这些可能会让你开始