Perl script to convert xlsx to xls file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to convert xlsx to xls file
# 1  
Old 05-30-2014
Router Perl script to convert xlsx to xls file

Hi

I am trying one perl script to convert xlsx to xls file but could not able to get all the rows and columns in the xls file . This scriptFILE is basically to convert XLSX to CSV .. I am tweaking the script to convert XLSX to XLS file also

#######################FILE #########################
Code:
#!/usr/bin/perl

use strict;

use warnings;
use Spreadsheet::XLSX;
#use Spreadsheet::ParseExcel::Format
use Spreadsheet::WriteExcel;

    # Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new('perl.xls');


#use Spreadsheet::ParseExcel;

#my $sourcename = shift @ARGV or die "invocation: $0 <source file>\n";
#my $source_excel = new Spreadsheet::XLSX;
my $source_book = Spreadsheet::XLSX -> new ("area_Status.xlsx");
my $worksheet = $workbook->add_worksheet();
#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";

 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})
  {
    #$worksheet->write(
 
    
    #my $col = 0;
    #   foreach my $token (@Fld) {
     
    #$worksheet->write($row_index, $col_index, $token);
    #       $col_index++;
    #   }
    #   $row++;
   my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
     $worksheet->write($row_index, $col_index);

   # $worksheet->write($source_cell);

   if ($source_cell)
   {

     #$worksheet->write($source_cell);
    #print "( $row_index , $col_index ) =>", $source_cell->Value, "\t";
    print  $source_cell->Value, ",";
    

   }
  } 
  print "\n";
 } 
}
print "done!\n";

I am adding
Code:
$workbook = Spreadsheet::WriteExcel->new('perl.xls');
 $worksheet->write($row_index, $col_index);

to the original script to convert XLSX to XLS file but not getting it

Please help me out to tackle this

Thanks
Kshitij



###########################################
# 2  
Old 05-31-2014
Hello kshitij, I didn't read through your entire code. But here's an example.. This is what I do in here.. Read from xlsx file using Spreadsheet::XLSX module and write to xls using Spreadsheet::WriteExcel module.

Code:
#! /usr/bin/perl

use warnings;
use strict;

use Spreadsheet::XLSX;
use Spreadsheet::WriteExcel;

my $excel_xlsx = Spreadsheet::XLSX -> new ('sample.xlsx');
my ($sheet_xlsx, $row, $col, $cell);

my $excel_xls = Spreadsheet::WriteExcel->new('sample.xls');
my $sheet_xls = $excel_xls->add_worksheet();

for $sheet_xlsx ( @{ $excel_xlsx->{Worksheet} } ) {
    for $row ( $sheet_xlsx->{MinRow} .. $sheet_xlsx->{MaxRow} ) {
        for $col ( $sheet_xlsx->{MinCol} .. $sheet_xlsx->{MaxCol} ) {
            my $cell = $sheet_xlsx->{Cells}[$row][$col];
            print "$cell->{Val} ";
            $sheet_xls->write($row, $col, $cell->{Val});
        }
        print "\n";
    }
}

Attached file has a sample.xlsx that will be converted to sample.xls
sample.tar.gz

Code:
[user@host ~]$ ./test.pl
id name
1 tom
2 dick
3 harry
[user@host ~]$ ls -l sample.xls*
-rwxr-xr-x 1 user None 5632 May 31 11:49 sample.xls
-rwxr-xr-x 1 user None 7402 May 31 11:16 sample.xlsx

# 3  
Old 05-31-2014
ok Thanks a lot !
will try it and let you know
# 4  
Old 06-02-2014
Issue with the perl script

This perl script is working fine if the XLSX which I need to convert into XLS is not having any formulas in the columns of the XLSX but wherever are the columns converted XLS file is putting 0 only on all those columns of XLSX wherever forumals are being used...

Please suggest some other solution to tackle this

---------- Post updated at 11:05 AM ---------- Previous update was at 10:41 AM ----------

I tried to use below code but still dont know how the values coming from Formulas in XLSX will come in XLS file
Code:
Not sure how to access formula values 

#! /usr/local/bin/perl

use warnings;
use strict;

#use Spreadsheet::XLSX;
use Spreadsheet::ParseXLSX;
use Spreadsheet::WriteExcel;
my $parser = Spreadsheet::ParseXLSX->new;
my $excel_xlsx = $parser->parse("area_Status.xlsx");
#my $excel_xlsx = Spreadsheet::ParseXLSX -> new ('area_Status.xlsx');
#my $excel_xlsx = Spreadsheet::XLSX -> new ('area_Status.xlsx');
my ($sheet_xlsx, $row, $col, $cell);

my $excel_xls = Spreadsheet::WriteExcel->new('sample.xls');
my $sheet_xls = $excel_xls->add_worksheet();

for $sheet_xlsx ( @{ $excel_xlsx->{Worksheet} } ) {
    for $row ( $sheet_xlsx->{MinRow} .. $sheet_xlsx->{MaxRow} ) {
        for $col ( $sheet_xlsx->{MinCol} .. $sheet_xlsx->{MaxCol} ) {
            my $cell = $sheet_xlsx->{Cells}[$row][$col];
            print "$cell->{Val} ";
            $sheet_xls->write($row, $col, $cell->{Val}, $cell->{Formula});
        }
        print "\n";
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Converting xls file to xlsx on UNIX script / command line.

Hi All, Am needing advise on how to convert xls file to xlsx format on Solaris unix command line or scripting. I tried searching online but it looks like I need to either use Perl packages of Excel or Python packages or some other 3rd party tool. Problem is to install any of these will require... (2 Replies)
Discussion started by: arvindshukla81
2 Replies

2. Shell Programming and Scripting

Merging Multiple XLS into Different tabs in xls/ xlsx

HI, I have multiple files per dept in folder for eg : In a folder File1_Dept100.xls File2_Dept100.xls File3_Dept100.xls File1_Dept200.xls File2_Dept200.xls File3_Dept200.xls Output should be : Dept100.xls which has File1, File2, File3 in different tabs Dept200.xls which has... (1 Reply)
Discussion started by: venkyzrocks
1 Replies

3. Shell Programming and Scripting

Perl script to Convert XLSX or XLS files to CSV file

Hi All, I've got in a situation where I need to convert .xlsx or .xls formatted files into simple text file or .csv file. I've found many options but doing this using PERL script is the best way I believe.I'm in AIX box. Perl code should have 2 params while running. i.e perl... (1 Reply)
Discussion started by: manab86
1 Replies

4. Shell Programming and Scripting

Perl: module to convert xlsx to csv

Is there any perl module to convert .xlsx file(excel sheet 2007) to a csv file. (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Shell Programming and Scripting

perl module to convert xlsx format to xls format

Hi Folks, I have written a perl script that reads data from excel sheet(.xls) using Spreadsheet::ParseExcel module. But the problem is this module doesn't work for excel sheets with extension .xlsx. I have gone through Spreadsheet::XLSX module with which we can read from .xlsx file directly.... (1 Reply)
Discussion started by: giridhar276
1 Replies

6. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

7. Shell Programming and Scripting

How to convert a xls file to csv?

Hi, My requirement is to convert the xls to csv file with utf-8 conversion. Is there any way please suggest me. Thanks, Raja (4 Replies)
Discussion started by: cnraja
4 Replies

8. UNIX for Dummies Questions & Answers

converting xls,xlsx files ??

I think I know the answer to this :rolleyes: but thought I'd ask anyway. You never know. Does anyone know of a program or utility that will run on any unix platform and convert Microsoft Excel files to ascii/plain text files that unix can understand ? Thanks in advance. Floyd (3 Replies)
Discussion started by: fwellers
3 Replies

9. UNIX for Dummies Questions & Answers

Unix script to convert .csv file to.xls format

I have a .csv file in Unix box i need a UNIX script to convert the.csv files to.xls format. Its very urgent please help me. (1 Reply)
Discussion started by: moon_friend
1 Replies

10. Shell Programming and Scripting

Convert a csv file to an xls format

Hi, I have a file coming in xxx.txt(csv format) i do some work on it and i need to send out as a .xls format. Is there any way there is some code i can use in my script to convert this? I'm struggling on this. Thanks (11 Replies)
Discussion started by: Pablo_beezo
11 Replies
Login or Register to Ask a Question