怎样将两个数组分别输出到excel的两个sheet中?

怎样将两个数组分别输出到excel的两个sheet中?

如题,下面的程序只能将数组 @glossary 输出到sheet1中,请问怎样将 另一数组@odds 输出到sheet2中?


use strict;
  use warnings;
use Cwd;
use Win32::OLE qw();
use Win32::OLE::Const 'Microsoft Excel';



my @glossary = (
  '北京',
  '上海',
  '天津',
  '广州',
  '深圳',
  '南京',
  '武汉',
);

my @odds = (
  'beijing',
  'shanghai',
  'tianjin',
  'guangzhou',
  'shenzhen',
  'nanjing',
  'wuhan',
);

print join "\n", &sort_gloss_in_excel(@glossary);


sub sort_gloss_in_excel {
  print ">>>>>>>>>>>> Exporting glossary entries into Microsoft Excel.\n";
  my (@gloss) = @_;
  my @sorted_gloss;
  my $gloss_sheet_file = "d:/gloss_entry.xls";
  my $current_path = getcwd;
  my $gloss_nums = @gloss;

  # delete last $gloss_sheet_file
  unlink($gloss_sheet_file) if (-e $gloss_sheet_file);

  # create new Excel objects
  my $Gloss_Excel = Win32::OLE->new( 'Excel.Application', sub {$_[0]->Quit;} )
    or die "\n   \$%\@&#! DID YOU INSTALL MS Excel IN YOUR MACHINE?\n";
  my $Gloss_Book = $Gloss_Excel->Workbooks->Add(1);
  my $Gloss_Sheet = $Gloss_Book->Worksheets(1);

  # write @gloss to Excel
  foreach my $row (1..$gloss_nums) {
    $Gloss_Sheet->Cells($row, 1)->{'Value'} = $gloss[$row-1];
  }

  print ">>>>>>>>>>>> Sorting glossary entries in Microsoft Excel.\n";
  $Gloss_Book->SaveAs("$gloss_sheet_file";
  undef $Gloss_Book;
  undef $Gloss_Excel;

  return @sorted_gloss;
}
http://lena.franken.de/perl_hier/excel.html

对你有帮助么?
用这种方式写Excel文件,效率不高

[Copy to clipboard] [ - ]
CODE:
foreach my $row (1..$gloss_nums) {
    $Gloss_Sheet->Cells($row, 1)->{'Value'} = $gloss[$row-1];
}

可以用这种方式添加记录,效率比较高

[Copy to clipboard] [ - ]
CODE:
$Gloss_Sheet->Rang("A1:C4")->{'Value'} = $data;            # $data 是一个二维数组的引用

形如:

[Copy to clipboard] [ - ]
CODE:
$data = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]];

怎样用perl将n行4列的Excel文件读出并存成成txt文件呢?
使用2层循环...
第一层的for循环行(比如有N行)..
第2层循环1--4列
然后读取对应行.列的数据,写到文件即可..