使用Perl中的Win32 :: OLE生成Excel输出

时间:2014-02-13 13:21:19

标签: perl

我是Perl的初学者,并尝试过使用Perl来了解它的方法和工作!我对arays,哈希和相关主题有基本的了解。我必须为一个主题开发一个脚本,我不确定如何去做。我非常需要帮助,非常感谢任何能够解释“如何做”的人!

我有一个包含3个部分的代码,它为3个不同的组件提供了相同的功能。基本思想是,它从excel文件中获取标记为“A”的所有组件,遍历excel文件,将相应的RAM和ROM值相加,并打印出没有重复条目的输出。第2和第3部分是相同的,但对于组件'B'和'C'。到目前为止,我能够在文本文件中打印出所有3个部分的输出。但是现在我希望excel工作簿中的所有三个输出都是3个独立的工作表!

我不是特别肯定如何去做。真的很受欢迎!

PS:如果我没有在论坛中输入代码,请原谅我!这是我的第一篇文章!!

以下是我的代码到目前为止的样子:

# This Test script was created to try out the possible methods to extract all the Names from the
# excel report without duplicate entries and find their corresponding RAM/ROM size sum

# -excel D:\Abc\Test.xlsx  -out D:\Abc\Output

sub usage($) 
{
   return shift(@_) . <<"END_USAGE";
   Usage: $0 -excel               Specify the file path.
                -out outputdirectory  Specify output directiory             
END_USAGE
}

use Getopt::Long;
use Win32::OLE;
use List::Util qw(sum);
use Data::Dumper qw(Dumper);

my $output_path = ();
my $excel_path = ();
my $no_rows = ();
my $lastCol = ();

GetOptions("excel=s"   =>   \$excel_path,
                 "out=s" =>   \$output_path,
           "h|help" =>   \$help,
          );

#help message
die usage("") if ($help);
system(cls);
print "\n*******************************************************************\n";
print "Component Overview \n";
print "*******************************************************************\n";
print "Please wait, Processing may take couple of minutes... \n";

##File handler for the script file.
$log_path = $output_path."\\log.txt";
$output_file_path = $output_path."\\TestExcel.xlsx";
open LogFile,">",$log_path or die "Cannot create the log file:$log_path !!!";
print LogFile "Start time :".localtime()."\n";

# Start Excel and make it visible
my $xlApp = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');  
$xlApp->{Visible} = 0;

#Opening the work book
my $workBook = $xlApp->Workbooks->Open($excel_path);
#print "X: " . $workBook . " - " . $excel_path . "\n";
my $excelSheet = $workBook->Worksheets("Report");
$excelSheet->Activate();

print "Reading the file...\n";
&ReadExcel();

print LogFile "Completed time :".localtime()."\n";
print "\nCompleted.Please close this window...\n" ;
print "*******************************************************************\n";

# Sub routine to parse the cosipa file
sub ReadExcel()
{
my $row_index;
#Findings the number of valid rows
$no_rows = $excelSheet->UsedRange->Rows->{'Count'};
$lastCol = $excelSheet->UsedRange->Columns->{'Count'};
$row_index = findRowindex();
my @comp_array = ();

# Name => ResourceType => size
my $resultData = {};

for(my $index=($row_index+1);$index<=$no_rows;$index++)
                       {
                            my $X =  $excelSheet->Cells($index,6)->Value();
            my $Y =  $excelSheet->Cells($index,7)->Value();
            my $name = $excelSheet->Cells($index,9)->Value();
            my $resourceType = $excelSheet->Cells($index,3)->Value();
            my $size = $excelSheet->Cells($index,2)->Value();

            #Name Overview                              
            my $currNameTypeMap;

            if ( ! exists $resultNameData->{ $name } )          # ->: arrow operator is used to dereference reference to arrays or hashes.
                {
                    $resultNameData->{ $name } = {};
                }
            $currNameTypeMap = $resultNameData->{ $name };

            $currNameTypeMap->{ $resourceType } += $size;       

            # Y Overview
            my $currYTypeMap;

            if ( ! exists $resultYData->{ $Y } )            # ->: arrow operator is used to dereference reference to arrays or hashes.
                {
                    $resultYData->{ $cluster } = {};
                }
            $currYTypeMap = $resultYData->{ $Y };

            $currYTypeMap->{ $resourceType } += $size;  

            # X Overview
            my $currXTypeMap;

            if ( ! exists $resultXData->{ $X } )            # ->: arrow operator is used to dereference reference to arrays or hashes.
                {
                    $resultXData->{ $X } = {};
                }
            $currXTypeMap = $resultXData->{ $X };

            $currXTypeMap->{ $resourceType } += $size;      
        }   

    my @uniqNameArr = sort keys %$resultNameData;

    my @uniqYArr = sort keys %$resultYData;

    my @uniqXArr = sort keys %$resultXData;

            for my $currName ( @uniqNameArr )
            {
                print $currName . "\n". " RAM: " . $resultNameData->{ $currName }-> { "RAM" } . ", ROM: " . $resultNameData->{ $currName }-> { "ROM" } . "\n";
                #print Dumper %$resultData;
            }

            print "----------------------------------------------------------------------- \n";

            for my $currY ( @uniqYArr )
            {
                print $currY. "\n". " RAM: " . $resultYData->{ $currY }-> { "RAM" } . ", ROM: " . $resultYData->{ $currY }-> { "ROM" } . "\n";
            }

            print "------------------------------------------------------------------------ \n";

            for my $currX ( @uniqXArr )
            {
                print $currX . "\n". " RAM: " . $resultXData->{ $currX }-> { "RAM" } . ", ROM: " . $resultXData->{ $currX }-> { "ROM" } . "\n";
            }

}

#Sub routine to find the starting row index
sub findRowindex()
{
my $ret = ();
for(my $index=1;$index<$no_rows;$index++)
{
    if(defined($excelSheet->Cells($index,1)))
    {
        my $cel_value = $excelSheet->Cells($index,1)->Value();
        if($cel_value =~ m/^Name$/i)
        {
            $ret = $index;
            last;
        }
    }
}
return $ret;

}

#Trim function
sub trim {
(my $s = $_[0]) =~ s/^\s+|\s+$//g;
return $s;        

}

1 个答案:

答案 0 :(得分:2)

一种解决方法:您可以使用Excel :: Writer :: XLSX来创建Excel文件,它工作正常且非常健壮。以下是将标签分隔文件转换为Excel的方法。

阅读excel:Spreadsheet :: XLSX

use Text::Iconv;
my $converter = Text::Iconv -> new ("utf-8", "windows-1251");


use Spreadsheet::XLSX;
my $excel = Spreadsheet::XLSX -> new ('test.xlsx', $converter);
foreach my $sheet (@{$excel -> {Worksheet}}) {
        printf("Sheet: %s\n", $sheet->{Name});
        $sheet -> {MaxRow} ||= $sheet -> {MinRow};
         foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {       
                $sheet -> {MaxCol} ||= $sheet -> {MinCol};              
                foreach my $col ($sheet -> {MinCol} ..  $sheet -> {MaxCol}) {     
                        my $cell = $sheet -> {Cells} [$row] [$col];
                        if ($cell) {
                            printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});
                        }

                }
        }
 }

编写excel:Excel :: Writer :: XLSX

my $workbook = Excel::Writer::XLSX->new( $xls_filename );
my $worksheet = $workbook->add_worksheet('data');
# Create a format for the headings
my $header_format = $workbook->add_format();
$header_format->set_bold(); 
$header_format->set_size( 18 );
$header_format->set_color( 'black' );
$header_format->set_align( 'center' );
my $row=0;
while (my $line = <$fh>){
    chomp($line);       
    my @cols = split(/\t/,$line);
    for(my $col=0;$col<@cols;$col++){
        if ($row == 0 ){
            $worksheet->write_string( $row, $col, $cols[$col],$header_format );
        } else {
            $worksheet->write_string( $row, $col, $cols[$col] );
        }
    }
    $row++;
}
close($fh); 

我希望这会对你有所帮助。

此致