Sponsored Content
Top Forums Shell Programming and Scripting How to convert a xls file to csv? Post 302505954 by pravin27 on Friday 18th of March 2011 05:25:30 AM
Old 03-18-2011
Here is the perl code to parse Excel.

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";

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

From xls to csv file

Can we convert an xls file into csv format in Unix Thanks Suresh (1 Reply)
Discussion started by: sureshg_sampat
1 Replies

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

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

4. Shell Programming and Scripting

converting xls file to txt file and xls to csv

I need to convert an excel file into a text file and an excel file into a CSV file.. any code to do that is appreciated thanks (6 Replies)
Discussion started by: bandar007
6 Replies

5. Shell Programming and Scripting

Shell convert xls to csv

Hi does anybody know how to convert xls to csv undex linux. I need only data (that is log from test, dont need any macro and so on) from xls. Any idea how to do that? Perl? shell? Could you give me any example? Thanks in advance for answer. Gracjan (4 Replies)
Discussion started by: Gracjan
4 Replies

6. Shell Programming and Scripting

how to convert XLS to CSV and DOC/RTF to TXT

Hi, i don't know anything about PERL. Can anyone help me providing PERL scripts for 1. converting XLS to CSV (and vice-versa) 2. converting DOC/RTF to TXT Thanks much Prvn (1 Reply)
Discussion started by: prvnrk
1 Replies

7. Shell Programming and Scripting

how to convert .xls to .csv

Hi, I have problem..How to convert .xls file to .csv.. Plz help me for this problem.. (1 Reply)
Discussion started by: varma457
1 Replies

8. AIX

How to convert csv file to xls file

Hi All, I have a java program running in AIX machine which gives me the output in form of .CSV but my clients wants output in the form of .xls When I gave the command mv <filename.csv> <filename.xls> The contents of this .xls file is not exactly in seprate columns as in CSV, the contents... (1 Reply)
Discussion started by: chetu777
1 Replies

9. UNIX and Linux Applications

Tool for Convert XLS into CSV in UNIX

Hi I wanted to convert some XLS files into CSV format in my UNIX box. Unix box is handling very important data which are related to data warehouse.It is fully optimized by installing minimum packages since server need more resources to handle reports generating. Just for convert XLS files... (6 Replies)
Discussion started by: luke_devon
6 Replies

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

NAME
Spreadsheet::ParseExcel::Utility - Utility functions for Spreadsheet::ParseExcel. SYNOPSIS
use Spreadsheet::ParseExcel::Utility qw(ExcelFmt ExcelLocaltime LocaltimeExcel); # Convert localtime to Excel time my $datetime = LocaltimeExcel(11, 10, 12, 23, 2, 64); # 1964-3-23 12:10:11 print $datetime, " "; # 23459.5070717593 (Excel date/time format) # Convert Excel Time to localtime my @time = ExcelLocaltime($datetime); print join(":", @time), " "; # 11:10:12:23:2:64:1:0 # Formatting print ExcelFmt('yyyy-mm-dd', $datetime), " "; # 1964-3-23 print ExcelFmt('m-d-yy', $datetime), " "; # 3-23-64 print ExcelFmt('#,##0', $datetime), " "; # 23,460 print ExcelFmt('#,##0.00', $datetime), " "; # 23,459.51 DESCRIPTION
The "Spreadsheet::ParseExcel::Utility" module provides utility functions for working with ParseExcel and Excel data. Functions "Spreadsheet::ParseExcel::Utility" can export the following functions: ExcelFmt ExcelLocaltime LocaltimeExcel col2int int2col sheetRef xls2csv These functions must be imported implicitly: # Just one function. use Spreadsheet::ParseExcel::Utility 'col2int'; # More than one. use Spreadsheet::ParseExcel::Utility qw(ExcelFmt ExcelLocaltime LocaltimeExcel); ExcelFmt($format_string, $number, $is_1904) Excel stores data such as dates and currency values as numbers. The way these numbers are displayed is controlled by the number format string for the cell. For example a cell with a number format of '$#,##0.00' for currency and a value of 1234.567 would be displayed as follows: '$#,##0.00' + 1234.567 = '$1,234.57'. The "ExcelFmt()" function tries to emulate this formatting so that the user can convert raw numbers returned by "Spreadsheet::ParseExel" to a desired format. For example: print ExcelFmt('$#,##0.00', 1234.567); # $1,234.57. The syntax of the function is: my $text = ExcelFmt($format_string, $number, $is_1904); Where $format_string is an Excel number format string, $number is a real or integer number and "is_1904" is an optional flag to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch. "ExcelFmt()" is also used internally to convert numbers returned by the "Cell::unformatted()" method to the formatted value returned by the "Cell::value()" method: my $cell = $worksheet->get_cell( 0, 0 ); print $cell->unformatted(), " "; # 1234.567 print $cell->value(), " "; # $1,234.57 The most common usage for "ExcelFmt" is to convert numbers to dates. Dates and times in Excel are represented by real numbers, for example "1 Jan 2001 12:30 PM" is represented by the number 36892.521. The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day. By applying an Excel number format the number is converted to the desired string representation: print ExcelFmt('d mmm yyyy h:mm AM/PM', 36892.521); # 1 Jan 2001 12:30 PM $is_1904 is an optional flag to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch. Excel for Windows generally uses 1900 and Excel for Mac OS uses 1904. The $is1904 flag isn't required very often by a casual user and can usually be ignored. ExcelLocaltime($excel_datetime, $is_1904) The "ExcelLocaltime()" function converts from an Excel date/time number to a "localtime()"-like array of values: my @time = ExcelLocaltime($excel_datetime); # 0 1 2 3 4 5 6 7 my ( $sec, $min, $hour, $day, $month, $year, $wday, $msec ) = @time; The array elements from "(0 .. 6)" are the same as Perl's "localtime()". The last element $msec is milliseconds. In particular it should be noted that, in common with "localtime()", the month is zero indexed and the year is the number of years since 1900. This means that you will usually need to do the following: $month++; $year += 1900; See also Perl's documentation for localtime(): The $is_1904 flag is an optional. It is used to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch. LocaltimeExcel($sec, $min, $hour, $day, $month, $year, $wday, $msec, $is_1904) The "LocaltimeExcel()" function converts from a "localtime()"-like array of values to an Excel date/time number: $excel_datetime = LocaltimeExcel($sec, $min, $hour, $day, $month, $year, $wday, $msec); The array elements from "(0 .. 6)" are the same as Perl's "localtime()". The last element $msec is milliseconds. In particular it should be noted that, in common with "localtime()", the month is zero indexed and the year is the number of years since 1900. See also Perl's documentation for localtime(): The $wday and $msec elements are usually optional. This time elements can also be zeroed if they aren't of interest: # sec, min, hour, day, month, year $excel_datetime = LocaltimeExcel( 0, 0, 0, 1, 0, 101 ); print ExcelFmt('d mmm yyyy', $excel_datetime); # 1 Jan 2001 The $is_1904 flag is also optional. It is used to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch. col2int($column) The "col2int()" function converts an Excel column letter to an zero-indexed column number: print col2int('A'); # 0 print col2int('AA'); # 26 This function was contributed by Kevin Mulholland. int2col($column_number) The "int2col()" function converts an zero-indexed Excel column number to a column letter: print int2col(0); # 'A' print int2col(26); # 'AA' This function was contributed by Kevin Mulholland. sheetRef($cell_string) The "sheetRef()" function converts an Excel cell reference in 'A1' notation to a zero-indexed "(row, col)" pair. my ($row, $col) = sheetRef('A1'); # ( 0, 0 ) my ($row, $col) = sheetRef('C2'); # ( 1, 2 ) This function was contributed by Kevin Mulholland. xls2csv($filename, $region, $rotate) The "xls2csv()" function converts a section of an Excel file into a CSV text string. $csv_text = xls2csv($filename, $region, $rotate); Where: $region = "sheet-colrow:colrow" For example '1-A1:B2' means 'A1:B2' for sheet 1. and $rotate = 0 or 1 (output is rotated/transposed or not) This function requires "Text::CSV_XS" to be installed. It was contributed by Kevin Mulholland along with the "xls2csv" script in the "sample" directory of the distro. See also the following xls2csv utilities: Ken Prows' "xls2csv": http://search.cpan.org/~ken/xls2csv/script/xls2csv and H.Merijn Brand's "xls2csv" (which is part of Spreadsheet::Read): http://search.cpan.org/~hmbrand/Spreadsheet-Read/ AUTHOR
Maintainer 0.40+: John McNamara jmcnamara@cpan.org Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org Original author: Kawai Takanori kwitknr@cpan.org COPYRIGHT
Copyright (c) 2009-2010 John McNamara Copyright (c) 2006-2008 Gabor Szabo Copyright (c) 2000-2006 Kawai Takanori All rights reserved. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. perl v5.10.1 2010-09-17 Spreadsheet::ParseExcel::Utility(3pm)
All times are GMT -4. The time now is 07:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy