Sponsored Content
Top Forums Shell Programming and Scripting Perl script to Convert XLSX or XLS files to CSV file Post 302798855 by bishnu.bhatta on Thursday 25th of April 2013 08:08:54 AM
Old 04-25-2013
Quote:
Originally Posted by manab86
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 <excel2csv.pl> file.xlsx/xls output.csv

I've googled for the same, but didn't find anything concrete. I hope you guys wont disappoint me.

Thanks.
Try this...The target file name is same as the source file name but the extension will be CSV instead of XLS
Code:
#!/usr/bin/perl

use strict;
use Spreadsheet::ParseExcel;

my $sourcename = shift @ARGV or die "invocation: $0 <source file>\n";
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";

 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, "\t";
    print  $source_cell->Value, "\t";
   }
  } 
  print "\n";
 } 
}
print "done!\n";


Last edited by Franklin52; 04-25-2013 at 09:12 AM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

Convert xlsx worksheet to separate csv

Hi All, I need a solution to convert my .xlsx file which has lot of worksheet, convert to separate csv file. Is there any shell script to do this? I can’t use excel macro to do this, since it was password locked. I need the csv output as an input to my shell script. (1 Reply)
Discussion started by: ranjancom2000
1 Replies

6. 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

7. 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

8. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: kshitij
3 Replies

9. 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

10. 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
Spreadsheet::WriteExcel::Chart::Column(3pm)		User Contributed Perl Documentation	       Spreadsheet::WriteExcel::Chart::Column(3pm)

NAME
Column - A writer class for Excel Column charts. SYNOPSIS
To create a simple Excel file with a Column chart using Spreadsheet::WriteExcel: #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' ); my $worksheet = $workbook->add_worksheet(); my $chart = $workbook->add_chart( type => 'column' ); # Configure the chart. $chart->add_series( categories => '=Sheet1!$A$2:$A$7', values => '=Sheet1!$B$2:$B$7', ); # Add the worksheet data the chart refers to. my $data = [ [ 'Category', 2, 3, 4, 5, 6, 7 ], [ 'Value', 1, 4, 5, 2, 1, 5 ], ]; $worksheet->write( 'A1', $data ); __END__ DESCRIPTION
This module implements Column charts for Spreadsheet::WriteExcel. The chart object is created via the Workbook "add_chart()" method: my $chart = $workbook->add_chart( type => 'column' ); Once the object is created it can be configured via the following methods that are common to all chart classes: $chart->add_series(); $chart->set_x_axis(); $chart->set_y_axis(); $chart->set_title(); These methods are explained in detail in Spreadsheet::WriteExcel::Chart. Class specific methods or settings, if any, are explained below. Column Chart Methods There aren't currently any column chart specific methods. See the TODO section of Spreadsheet::WriteExcel::Chart. EXAMPLE
Here is a complete example that demonstrates most of the available features when creating a chart. #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new( 'chart_column.xls' ); my $worksheet = $workbook->add_worksheet(); my $bold = $workbook->add_format( bold => 1 ); # Add the worksheet data that the charts will refer to. my $headings = [ 'Number', 'Sample 1', 'Sample 2' ]; my $data = [ [ 2, 3, 4, 5, 6, 7 ], [ 1, 4, 5, 2, 1, 5 ], [ 3, 6, 7, 5, 4, 3 ], ]; $worksheet->write( 'A1', $headings, $bold ); $worksheet->write( 'A2', $data ); # Create a new chart object. In this case an embedded chart. my $chart = $workbook->add_chart( type => 'column', embedded => 1 ); # Configure the first series. (Sample 1) $chart->add_series( name => 'Sample 1', categories => '=Sheet1!$A$2:$A$7', values => '=Sheet1!$B$2:$B$7', ); # Configure the second series. (Sample 2) $chart->add_series( name => 'Sample 2', categories => '=Sheet1!$A$2:$A$7', values => '=Sheet1!$C$2:$C$7', ); # Add a chart title and some axis labels. $chart->set_title ( name => 'Results of sample analysis' ); $chart->set_x_axis( name => 'Test number' ); $chart->set_y_axis( name => 'Sample length (cm)' ); # Insert the chart into the worksheet (with an offset). $worksheet->insert_chart( 'D2', $chart, 25, 10 ); __END__ AUTHOR
John McNamara jmcnamara@cpan.org COPYRIGHT
Copyright MM-MMX, John McNamara. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. perl v5.10.1 2010-02-02 Spreadsheet::WriteExcel::Chart::Column(3pm)
All times are GMT -4. The time now is 10:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy