in reply to Re^6: Append new line in excel sheets
in thread Append new line in excel sheets

perl_new_b:

I should've read the entire thread before posting my last response. If you're just trying to convert the spreadsheets to .CSV files and add some data to the end, then it's really pretty simple. Writing a new cell handler (as described in the section "Reducing the memory usage of Spreadsheet::ParseExcel" of the Spreadsheet::ParseExcel documentation is pretty easy--all you need to do is store the value, not the formatting, etc. That will make the program consume *much* less RAM. (The cell handler is what builds the *huge* data structure containing all the formatting information, the cell value, unformatted value, etc. Also, each cell is an object containing other overhead such as references to the parent structures, miscellaneous information, etc.

It should be as easy as:

#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; # 3D array for the workbook data: # [0]=sheet #, [1]=row, [2]=col my @WBData; my $parser = Spreadsheet::ParseExcel->new( CellHandler => \&cell_handler, NotSetCell => 1 ); my $workbook = $parser->parse('file.xls'); sub cell_handler { my $workbook = $_[0]; my $sheet_index = $_[1]; my $row = $_[2]; my $col = $_[3]; my $cell = $_[4]; # Store *just* the value into our array $WBData[$sheet_index][$row][$col] = $cell->value(); }

Note: this is untested, it's just lifted straight from the example in the previous link with a couple minor edits.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^8: Append new line in excel sheets
by perl_new_b (Novice) on Jul 23, 2014 at 12:49 UTC

    Hi robotics, Thanks a lot. but kindly can u guide, actually i got confused at where you are appending a new row at the end in the code mentioned by you.

      perl_new_b:

      The bit of code I showed doesn't append a new row. Instead, it creates an array with the data in the original Excel spreadsheet. All you need to do is to append the data to the appropriate places in the array, and then write the data. Excel can read a .CSV file, so I'd just use Text::CSV to write the data file. (You can even name it with a .xls extension and Excel will read it nicely. Remember, though, there will be no formatting or charts.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Hi @Roboticus,

        I want to append the data to a excel file having .xls extension, can TEXT::CSV do that?

        And you are right I don't need any formatting in that.

      Hi @roboticus, I want to append a new line at the end of a excel file(.xls to be precise).

      Kindly guide me for the above. Thanks