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

Is the memory usage a problem? Do you get out of memory errors? If not, I wouldn't worry.

Unfortunately .XLS files are a binary format which you cannot process on a row by row basis. Everything will be read in memory at once and I do not think there is another way around this.

I did find Spreadsheet::ParseExcel_XLHTML (an old module, last updated in 2009) which claims to be faster and does "less fancy stuff". It replaces the parsing subroutine of Spreadsheet::ParseExcel and perhaps uses less memory? I haven't tried it, so I have no idea if it works well. Also there is no ::Simple version of it. However, if you look at the source code of Spreadsheet::ParseExcel::Simple it seems easy to make a Spreadsheet::ParseExcel_XLHTML::Simple yourself.

Update: Unfortunately I cannot install Spreadsheet::ParseExcel_XLHTML since it needs bin:xlhtml which is not available on my PC.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^4: Append new line in excel sheets
by perl_new_b (Novice) on Jul 14, 2014 at 05:36 UTC

    Hi guys, thanks for the response, I figured out to calculate the no of rows using SaveParser but using this I'm unable to save the modified row. Im posting my code , kindly provide any feedback or any thing that I'm missing

    sub to_test { my ($self, $in_file) = @_; $count = @_; #To Test util->logit("[$PARENT_PROC_NAME]: in_file[$in_file]"); my $parser_temp = Spreadsheet::ParseExcel::SaveParser->new( CellHandler => \&cell_handler_1, NotSetCell => 0 ); util->logit("[$PARENT_PROC_NAME]: parser_temp[$parser_temp]"); my $sheet_ct = 0; my $Book = $parser_temp->Parse($in_file); util->logit("[$PARENT_PROC_NAME]: parser_temp[$parser_temp], glob_ +sheet_index[$glob_sheet_index],pre_row[$pre_row]"); @count_of_sheet_rows[$glob_sheet_index] = $pre_row; util->logit("[$PARENT_PROC_NAME]: count_of_sheet_rows_3[@count_of_ +sheet_rows], glob_sheet_index[$glob_sheet_index],pre_row[$pre_row]"); #my $workbook = Spreadsheet::WriteExcel->new(); #my $sheet_work = $parser_temp->worksheet(0); #$sheet_work->AddCell( 1, 0, 'Hi Excel!' ); my $temp = "temp"; my $new_path = "$out_file$underscore$temp$extension"; my $Saved_Workbook = $Book->SaveAs($new_path); foreach my $worksheet ($Book->worksheets()) { my $value_row = @count_of_sheet_rows[$sheet_ct]+2; my $value_cell = 0; util->logit("[$PARENT_PROC_NAME]: count value_row[$value_row], wor +ksheet[$worksheet]"); #$worksheet->AddCell(@count_of_sheet_rows[$sheet_ct] + 1, 0, local +time); $worksheet->AddCell($value_row, $value_cell, "World2"); ##$worksheet->AddCell( 1, 0, 'Hi Excel!' ); #$worksheet->write(@count_of_sheet_rows[$sheet_ct], 0, 'Hi Exce +l!'); $sheet_ct++; } #$Book->close(); #$Book->Close(); #$parser_temp->Quit(); #$Saved_Workbook->SaveAs($new_path); $Saved_Workbook->close(); #undef $Book; #undef $parser_temp; return $new_path; }

    Kindly suggest something...

      We cannot run this code, as it is a solitary subroutine without any documentation of input and expected output. I suspect it does not even run under "use strict" and contains a lot of irrelevant code not directly linked to your problem.

      Please, write a simple script that is only about your problem and then we may be able to assist.

      Also you haven't answered my question about the problem(?) with the high(?) memory use. Any solution which is based on Spreadsheet::ParseExcel will run into the same problem.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

        Hi CountZero, Sorry I didn't respond your query. Here is my actual problem: I'm converting Excel(.xls, .xlsx) files to .csv files. Since a single file contains around 10k lines so on calling parse in the below code:

        my $parser = Spreadsheet::ParseExcel->new(); my $Book = $parser->parse($in_file);

        memory out of bound error comes on command prompt, but I figured out to resolve this using cell_handler. But for few excel files cell handler skips the last row of the excel files which contain tables. After doing some R&D I found out that after adding a row in the end of file resolves the problem. In order to make it work I have to append a new row in the end. I don't want to use OLE as on our production system MS Office will not be installed. So kindly suggest me something. In response of your question: Yes the program fails, It throws memory out of bound on command prompt. And when I monitor the memory usage it hits 1GB mark for this size. The version of Excel files I'm working on is Microsoft Office Standard 2010. They might be of older version also as we get these excel files from some other resources which can not be controlled by us. In case I missed to mention something kindly point out. I will be very thanks... I'm so stuck here.