需要更多有关如何使用 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 列,并将一些文本附加到存储在单元格中的字符串的末尾。那将如何设置呢?
I have been using the Spreadsheet::ParseExcel to list the contents of spreadsheet. I've seen several examples on how to dump the entire spreadsheet. I really would like to see how to use this script more selectively.
The example below from IBM basically dumps the content of all cells that have data.
#!/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);
}
}
}
Can someone give me an example of how I can specify some action to take for a certain column for every row?
For example, I have a spreadsheet with 7 columns and n rows. I want to reformat the data in each of the columns in a certain way. Perhaps I want take column 6 for every row and append some text to the end of the string stored in the cell. How would that be set up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要修改 Excel 文件,Spreadsheet::ParseExcel::SaveParser模块非常有用。它允许您读取文件、进行修改,然后将更改的内容写入新文件。这是一个简单的示例:
对于许多其他示例,请参阅优秀的文档:
To modify an Excel file, the Spreadsheet::ParseExcel::SaveParser module is very useful. It allows you to read a file, make modifications, and then write the altered content to a new file. Here's a simple example:
For many other examples, see the excellent documentation:
要以某种方式重新格式化每列中的数据,您可以使用
Spreadsheet::ParseExcel
和Spreadsheet::WriteExcel
模块的组合,例如通过以下方式创建新的 Excel
Spreadsheet::WriteExcel
模块并在其中添加工作表。然后解析现有的 Excel,并将内容格式化写入新的 Excel。这是一个示例:
从现有 Excel 写入新 Excel 后,您可以使用
Spreadsheet::WriteExcel
的 API 在其中追加数据。它将解决诸如以下问题:To reformat the data in each of the columns in a certain way, you can use the combination of
Spreadsheet::ParseExcel
andSpreadsheet::WriteExcel
modules likeCreate a new excel by
Spreadsheet::WriteExcel
module and add worksheet in it.Then Parse the existing Excel and write the content into new excel with formating.Here is a sample example:
After writing a new excel from the existing excel, you can use API's of
Spreadsheet::WriteExcel
to append data in it.It will solve problems like