Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Read multple .xls files and Write/Merge into 1 file with multiple Sheets

by bheeshmaraja (Novice)
on Jun 18, 2015 at 09:10 UTC ( [id://1130950]=perlquestion: print w/replies, xml ) Need Help??

bheeshmaraja has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have 4 excel(.xls) files and I want to make these 4 files into 4 sheets in a single file. I have written a small script which reads and writes a xls file. Can you help me to process all 4 files with its input format available in its result file:
#!/usr/bin/perl use strict; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; my $parser = Spreadsheet::ParseExcel->new(); #Creating Object to Read +Worksheet my $parse_workbook = $parser->parse('Infile.xls'); my $write_workbook = new Spreadsheet::WriteExcel('Resultfile.xls'); my $write_worksheet = $write_workbook->add_worksheet('Sheet1'); if ( !defined $parse_workbook) { die $parser->error(), ".\n"; } for my $parse_worksheet ( $parse_workbook->worksheets() ) { my ( $row_min, $row_max ) = $parse_worksheet->row_range(); my ( $col_min, $col_max ) = $parse_worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $parse_worksheet->get_cell( $row, $col ); next unless $cell; my $cell_value = $cell->value(); my $cell_format = $cell->get_format(); $write_worksheet->write($row, $col, $cell_value); } } }
Notes:

1. I just want to merge 4 different xls files into a single file with 4 sheets. 2. I want the same format which comes with input file to be available in the result file which I'm sending to my end user(like first files format should be available in Sheet1 and second files input should be available in Sheet2 and so on). 3.I have to assign Names to all 4 Sheets. 4. Sheet Name has Special Character(/)(ie. From/To) and I want to assign the same to the Sheet Name

  • Comment on Read multple .xls files and Write/Merge into 1 file with multiple Sheets
  • Download Code

Replies are listed 'Best First'.
Re: Read multple .xls files and Write/Merge into 1 file with multiple Sheets
by marto (Cardinal) on Jun 18, 2015 at 09:45 UTC

    "1. I just want to merge 4 different xls files into a single file with 4 sheets."

    The good news here is that you have code which already works for one input file. The sensible thing to do here is make some small alterations to your code and change it into a subrotuine. Rather than hard code values such as Infile.xls pass them to your subroutine as an argument. See also, Tutorial -> Subroutines -> Recursion: The Towers of Hanoi problem for a nice explanation and short example.

    "2. I want the same format which comes with input file to be available in the result file which I'm sending to my end user(like first files format should be available in Sheet1 and second files input should be available in Sheet2 and so on)."

    IIRC these modules don't preserve all formatting, you'd need to check the documentation.

    "3.I have to assign Names to all 4 Sheets."

    Pass them as an additional argument to your subroutine, along with the file name.

    "4. Sheet Name has Special Character(/)(ie. From/To) and I want to assign the same to the Sheet Name"

    I don't believe Excel lets you use the / character in the sheet name.

Re: Read multple .xls files and Write/Merge into 1 file with multiple Sheets
by pme (Monsignor) on Jun 18, 2015 at 10:59 UTC
    This is a possible solution. If you want to provide the input file names in the command line read perlvar and search for 'ARGV'.
    use strict; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; my $parser = Spreadsheet::ParseExcel->new(); #Creating Object to Read +Worksheet my @files = qw/ Infile1.xls Infile2.xls Infile3.xls Infile4.xls /; my $write_workbook = new Spreadsheet::WriteExcel('Resultfile.xls'); my $n = 1; foreach my $file (@files) { my $parse_workbook = $parser->parse($file); my $write_worksheet = $write_workbook->add_worksheet("Sheet$n"); if ( !defined $parse_workbook) { die $parser->error(), ".\n"; } for my $parse_worksheet ( $parse_workbook->worksheets() ) { my ( $row_min, $row_max ) = $parse_worksheet->row_range(); my ( $col_min, $col_max ) = $parse_worksheet->col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $parse_worksheet->get_cell( $row, $col ); next unless $cell; my $cell_value = $cell->value(); my $cell_format = $cell->get_format(); $write_worksheet->write($row, $col, $cell_value); } } } $n++; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1130950]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-26 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found