modify Existing MS excel workbook in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modify Existing MS excel workbook in perl
# 1  
Old 04-26-2010
modify Existing MS excel workbook in perl

Hi

I need to modify an excel file in perl and for which I installed perl in
Linux

1. Open a existing excel file

2. delete an unwanted Sheet called "summary"

3. and i want to insert some data into range of cells ( B1:B11)

4. Remove unwanted value called "Sum" repeated in the sheet.

Could you please tell me what is the most suitable perl module that we can use ?

I tried to use following packages .

John McNamara / Spreadsheet-WriteExcel - search.cpan.org .

John McNamara / Spreadsheet-ParseExcel - search.cpan.org .

but no luck. I couldn't find the result yet. Could you plese give me some guide as well as a sample code for this ?

Thanks in advance
Luke
# 2  
Old 04-27-2010
Hi,

Here is the example for the read and write excel.You have to pass source and destination excel to the script.

Code:
#!/usr/bin/perl

use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;
use Data::Dumper;


my $sourcename = shift @ARGV;
my $destname = shift @ARGV or die "invocation: $0 <source file> <destination file>";

my $source_excel = new Spreadsheet::ParseExcel;

my $source_book = $source_excel->Parse($sourcename) or die "Could not open source Excel file $sourcename: $!";

my $storage_book;

foreach my $source_sheet_number (0 .. $source_book->{SheetCount}-1)
{
 my $source_sheet = $source_book->{Worksheet}[$source_sheet_number];

 print "--------- SHEET:", $source_sheet->{Name}, "\n";

 # sanity checking on the source file: rows and columns should be sensible
 next unless defined $source_sheet->{MaxRow};
 next unless $source_sheet->{MinRow} <= $source_sheet->{MaxRow};
 next unless defined $source_sheet->{MaxCol};
 next unless $source_sheet->{MinCol} <= $source_sheet->{MaxCol};

 foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow})
 {
  foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol})
  {
   my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
   if ($source_cell)
   {
    print "( $row_index , $col_index ) =>", $source_cell->Value, "\n";

    if ($source_cell->{Type} eq 'Numeric')
    {
  $storage_book->{$source_sheet->{Name}}->{$row_index}->{$col_index} = $source_cell->Value;
    }
    else
    {
  $storage_book->{$source_sheet->{Name}}->{$row_index}->{$col_index} = $source_cell->Value;
    } # end of if/else
   } # end of source_cell check
  } # foreach col_index
 } # foreach row_index
} # foreach source_sheet_number

print "Perl recognized the following data (sheet/row/column order):\n";
print Dumper $storage_book;

my $dest_book  = Spreadsheet::WriteExcel->new("$destname") or die "Could not create a new Excel file in $destname: $!";

print "\n\nSaving recognized data in $destname...";

foreach my $sheet (keys %$storage_book)
{
 my $dest_sheet = $dest_book->addworksheet($sheet);
 foreach my $row (keys %{$storage_book->{$sheet}})
 {
  foreach my $col (keys %{$storage_book->{$sheet}->{$row}})
  {
   $dest_sheet->write($row, $col, $storage_book->{$sheet}->{$row}->{$col});
  } # foreach column
 } # foreach row
} # foreach sheet

$dest_book->close();

print "done!\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl script to merge cells in column1 which has same strings, for all sheets in a excel workbook

Perl script to merge cells ---------- Post updated at 12:59 AM ---------- Previous update was at 12:54 AM ---------- I am using below code to read files from a dir and print to excel. open(my $in, '<', $file) or die "Could not open file: $!"; my $rowCount = 0; my $colCount = 0;... (11 Replies)
Discussion started by: Jack_Bruce
11 Replies

2. Shell Programming and Scripting

Modify existing values in a file

Hi, I am trying to write a shell script which will modify existing values and take backup of old values. file trying to modify is : /etc/sysctl.conf script name: modify.sh execute ./modify.sh enter ref no: 123 add below values in file sysctl.cnf kernel.shmall = 4194304 ... (7 Replies)
Discussion started by: darling
7 Replies

3. Shell Programming and Scripting

Perl - Append data to existing excel cell

Hello All, I have the following code in PERL to write data to excel sheet. Can someone please help me about how to append data to an exisitng cell? For ex in the below given case,Cell 1,1 has Active State PERL Now I want to add a new line like "prorgamming" without overwritting the... (3 Replies)
Discussion started by: prasperl
3 Replies

4. Shell Programming and Scripting

Help to modify in Excel format

Hi I ran some query in solaris machine and the result of the query is as below. Result: dn: uid=xxx,dc=example,dc=com id: xxx firstname: sam lastname: nam But my question is , how to get result in excel format in solaris and result attributes dn,id,firstname,lastname values ... (1 Reply)
Discussion started by: buzzme
1 Replies

5. Shell Programming and Scripting

how to modify existing env var

greetings, i have the following env variable: LSB_HOSTS='t70cra102 t70cra102 t70cra108 t70cra108'the variable could be any number of host names and sometimes the same name several times. i was hoping there's a way to turn it into the following: NEW_LSB_HOSTS=,,,]always appreciate the help... (2 Replies)
Discussion started by: crimso
2 Replies

6. UNIX for Dummies Questions & Answers

Add columns to an existing excel sheet

Hi All, i have an excel sheet as below: day ----- monday tuesday wenesday thursday friday i need to append the two more columns in this existing file below: insert date should be todays date--- day insert date insert user ---- ---------- ... (3 Replies)
Discussion started by: arunmanas
3 Replies

7. Shell Programming and Scripting

PERL: Modify Excel cell formatting

Hi, I would like to modify an existing excel cell format to a custom format. Is there a way to do it with out writing into a new excel file. In-place editing? Thanks (0 Replies)
Discussion started by: sandeep78
0 Replies

8. Shell Programming and Scripting

PERL: Split Excel Workbook to Indiv Excel files

Hi, I am trying to find a way to read an excel work book with multiple worksheets. And write each worksheet into a new excel file using perl. My environment is Unix. For example: I have an excel workbook TEST.xls and it has Sheet1, Sheet2, Sheet3 worksheets. I would like to create... (2 Replies)
Discussion started by: sandeep78
2 Replies

9. Shell Programming and Scripting

writeExcelXML will work on existing excel sheet

I want to know that will SpreadSheet::WriteExcelXML will work on the existing excel file. (1 Reply)
Discussion started by: akash
1 Replies

10. UNIX for Advanced & Expert Users

How to modify an existing pdf with unix shell commands

Hi, I know that to create a pdf file I can use the txt2pdf command. But if I would change an existing pdf file, by inserting lines in particular positions of this file, what can I use? And How? (3 Replies)
Discussion started by: fandwick
3 Replies
Login or Register to Ask a Question