[英]Need more examples on how to use Spreadsheet::ParseExcel
我一直在使用Spreadsheet :: ParseExcel列出电子表格的内容。 我已经看到了几个关于如何转储整个电子表格的例子。 我真的很想看看如何更有选择地使用这个脚本。
以下来自IBM的示例基本上转储了具有数据的所有单元格的内容。
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $oExcel = new Spreadsheet::ParseExcel;
die "You must provide a filename to $0 to be parsed as an Excel file" unless @ARGV;
my $oBook = $oExcel->Parse($ARGV[0]);
my($iR, $iC, $oWkS, $oWkC);
print "FILE :", $oBook->{File} , "\n";
print "COUNT :", $oBook->{SheetCount} , "\n";
print "AUTHOR:", $oBook->{Author} , "\n"
if defined $oBook->{Author};
for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++)
{
$oWkS = $oBook->{Worksheet}[$iSheet];
print "--------- SHEET:", $oWkS->{Name}, "\n";
for(my $iR = $oWkS->{MinRow} ;
defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
$iR++)
{
for(my $iC = $oWkS->{MinCol} ;
defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
$iC++)
{
$oWkC = $oWkS->{Cells}[$iR][$iC];
print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC);
}
}
}
有人能给我一个例子,说明我如何为每一行的某一列指定一些动作?
例如,我有一个包含7列和n行的电子表格。 我想以某种方式重新格式化每个列中的数据。 也许我想为每一行取第6列并将一些文本附加到存储在单元格中的字符串的末尾。 怎么设置?
要修改Excel文件, Spreadsheet :: ParseExcel :: SaveParser模块非常有用。 它允许您读取文件,进行修改,然后将更改的内容写入新文件。 这是一个简单的例子:
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
my $excel_file_name = $ARGV[0];
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook_orig = $parser->Parse($excel_file_name);
# We will edit column 7 of the first worksheet.
my $worksheet = $workbook_orig->worksheet(0);
my $EDIT_COL = 6;
my ($row_min, $row_max) = $worksheet->row_range();
for my $r ($row_min .. $row_max){
my $cell = $worksheet->get_cell($r, $EDIT_COL);
unless (defined $cell){
next; # Modify as needed to handle blank cells.
}
my $val = $cell->value . '_append_text';
$worksheet->AddCell( $r, $EDIT_COL, $val, $cell->{FormatNo} );
}
# You can save the modifications to the same file, but when
# you are learning, it's safer to write to a different file.
$excel_file_name =~ s/\.xls$/_new.xls/;
$workbook_orig->SaveAs($excel_file_name);
对于许多其他示例,请参阅优秀文档:
要以某种方式重新格式化每个列中的数据,您可以使用Spreadsheet::ParseExcel
和Spreadsheet::WriteExcel
模块的组合,如
通过Spreadsheet::WriteExcel
模块创建一个新的Excel,并在其中添加工作表。然后解析现有的Excel并使用格式化将内容写入新的Excel。
这是一个示例示例:
use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
die "You must provide a filename to $0 to be parsed as an Excel file" unless @ARGV;
my $oBook = $oExcel->Parse($ARGV[0]);
my($iR, $iC, $oWkS, $oWkC);
my $workbook = Spreadsheet::WriteExcel->new('/root/Desktop/test_simple.xls');
my $worksheet = $workbook->add_worksheet();
$worksheet->set_column('A:A', 50);
my $format = $workbook->add_format();
$format->set_size(10);
$format->set_bold();
$format->set_color('black');
$format->set_font('Verdana');
$format->set_text_wrap();
for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++)
{
$oWkS = $oBook->{Worksheet}[$iSheet];
for(my $iR = $oWkS->{MinRow} ;defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;$iR++)
{
for(my $iC = $oWkS->{MinCol} ;defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;$iC++)
{
$oWkC = $oWkS->{Cells}[$iR][$iC];
$worksheet->write($iR , $iC, $oWkC->Value, $format) if($oWkC); #writing a new excel from existing excel
}
}
}
从现有的excel中编写一个新的excel后,你可以使用Spreadsheet::WriteExcel
API在其中附加数据。它将解决像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.