Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi friends,i have a output file from perl as follow:

1.....2.34 3.21 2.67 2.....3.98 4.36 8.92 3 . . .

how can i send this file to an excel file? thank you.

Replies are listed 'Best First'.
Re: output to excel
by moritz (Cardinal) on Aug 04, 2011 at 11:30 UTC
      It's lunch time, I'm bored. Something like this should work .
      #!perl.exe use Modern::Perl; #or use strict;use warnings; use Spreadsheet::WriteExcel; my $input_fn = 'perlmonks.org.918502.txt'; # or the name of your file my $output_fn = 'perlmonks.org.918502.xls'; # or the name of the xls f +ile you want to produce #open the input for reading open my $input_fh, '<', $input_fn or die "Unable to open $input_fn: $1 +\n"; #open xls file for output my $workbook = Spreadsheet::WriteExcel->new($output_fn); die "Problems creating Excel file: $!\n" unless defined $workbook; #create a worksheet with the name of the input file my $worksheet = $workbook->add_worksheet($input_fn); #split the line, and write to the matching row in the spreadsheet while (my $line = <$input_fh>) { $line =~ /(\d+).{5}(\d+.\d+)\s(\d+.\d+)\s(\d+.\d+)/; my @columns = ($2, $3, $4); $worksheet->write_row($1,0,\@columns); }

      Or Excel::Writer::XLSX if you have the latest version of MS Excel and you want to write in it's native XML format.

      You might want to do this go get around the limitations in the older Excel format (eg: maximum of 256 columns and 2**16 rows per sheet).

      Both modules are written by the same author and claim to have the same interface, so you could decide which to use at runtime while only having a few lines of code that depend on which file format you plan to write.

Re: output to excel
by thenaz (Beadle) on Aug 04, 2011 at 12:26 UTC

    The easiest thing (in my opinion) is to use a comma-separated-values format, and import into Excel.

    open INPUT, "<./input.txt"; # assuming that's your input open OUTPUT, ">./output.csv"; # comma-separated-values output print OUTPUT "First column heading,Second column heading,Third column +heading\n"; while (<INPUT>) { my $first = s/(\d+)\.+//; my @others = split /\s\+/; print OUTPUT join(',' ($first, @others)).$/; }
      If you're going to take this approach, you'll probably be better of looking at Text::CSV unless you can be 100% sure that your input will never contain any commas.
Re: output to excel
by CountZero (Bishop) on Aug 04, 2011 at 13:45 UTC
    Your specifications are unclear.

    Does every line have to be put in a separate cell? How to split each row? What to do with 1.....2.34: is this one cell or multiple cells?

    The various Spreadsheet modules on CPAN will be a good place to start.

    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

Re: output to excel
by tmaly (Monk) on Aug 04, 2011 at 14:50 UTC

    I agree that the easiest way is a comma separated file with the extension csv. Excel will open this up when you double click on the file.

    If your data has commas in it, you could change the separator to a pipe, but then you would have to do a data import into excel and specify the separator which is a little bit more work

      You might be better with tab / \t as excel will load that with a .csv extension as well.