簡體   English   中英

需要更多關於如何使用Spreadsheet :: ParseExcel的示例

[英]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::ParseExcelSpreadsheet::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在其中附加數據。它將解決像

  1. 首先,它將保留您現有的Excel文件。
  2. 您可以在不修改原始Excel文件的情況下格式化為新的Excel文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM