简体   繁体   English

需要更多关于如何使用Spreadsheet :: ParseExcel的示例

[英]Need more examples on how to use Spreadsheet::ParseExcel

I have been using the Spreadsheet::ParseExcel to list the contents of spreadsheet. 我一直在使用Spreadsheet :: ParseExcel列出电子表格的内容。 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. 以下来自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);
  }
 }
}

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. 例如,我有一个包含7列和n行的电子表格。 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. 也许我想为每一行取第6列并将一些文本附加到存储在单元格中的字符串的末尾。 How would that be set up? 怎么设置?

To modify an Excel file, the Spreadsheet::ParseExcel::SaveParser module is very useful. 要修改Excel文件, Spreadsheet :: ParseExcel :: SaveParser模块非常有用。 It allows you to read a file, make modifications, and then write the altered content to a new file. 它允许您读取文件,进行修改,然后将更改的内容写入新文件。 Here's a simple example: 这是一个简单的例子:

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);

For many other examples, see the excellent documentation: 对于许多其他示例,请参阅优秀文档:

To reformat the data in each of the columns in a certain way, you can use the combination of Spreadsheet::ParseExcel and Spreadsheet::WriteExcel modules like 要以某种方式重新格式化每个列中的数据,您可以使用Spreadsheet::ParseExcelSpreadsheet::WriteExcel模块的组合,如

Create 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. 通过Spreadsheet::WriteExcel模块创建一个新的Excel,并在其中添加工作表。然后解析现有的Excel并使用格式化将内容写入新的Excel。

Here is a sample example: 这是一个示例示例:

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
                }
            }
        }

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 从现有的excel中编写一个新的excel后,你可以使用Spreadsheet::WriteExcel API在其中附加数据。它将解决像

  1. First it will retain your existing Excel file. 首先,它将保留您现有的Excel文件。
  2. you can do formating into new excel file without modifying the original Excel File. 您可以在不修改原始Excel文件的情况下格式化为新的Excel文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM