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

To all,

I'm writting a web based application which lets the user query various parts of out database and output the data to your browser. Such data is outputted in a table format to make for easy readability. Some of the attributes that you may see are: UserID, TestResult, Create_Date, Create_Time, Release, UserComments, etc (there are many, many others).

Anyway, I also output this data to a XLS file so the user can save and manipulate this data outside of the application. Currently, I just open a file handle (call it XLS) and so something like print XLS "$UserID\t$TestResult\t$Create_Date\t$Create_Time\t$Release\t$UserComment\n" Which is most cases works fine, but sometimes, some of the data is a list of multiple thing but in the xls cell, it comes out as one long string.

Question 1: Is there a way to add in a "newline" in a XLS cell via using print statement above? I'm obviously not using any Excel modules, so I'm guessing I'll have to do that or are they any quick workarounds ?

Question 2: Are there any recommended modules that I can output this some data to either a .txt format or .doc format ?

Thank in advance all !!

Replies are listed 'Best First'.
Re: Formatting text when outputting to a XLS file
by dragonchild (Archbishop) on Feb 14, 2005 at 15:21 UTC
    Have you looked at Excel::Template or the module it wraps, which is Spreadsheet::WriteExcel? It seems that either of those modules should do what you want, and more.

    I'm not sure you really understand what you're asking in terms of lists of multiple things. How exactly do you want the Excel to turn out? Also, it's important to note that unless you're using Win32::OLE, it's not possible to completely control the XLS output from Perl.

    As for .txt or .doc ... you'll want to look at Text::xSV for the .txt (if you want tab-delim). .doc is out of my knowledge.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Formatting text when outputting to a XLS file
by RazorbladeBidet (Friar) on Feb 14, 2005 at 15:23 UTC
    Perhaps DBD::Excel would be of some use to you for your first question.

    I'm not sure otherwise, there's probably a special character to get around it.

    You might want to make an Excel file, do what you're saying (put a list of things in a cell), then export to text and see what it loos like. It may be an XLS specific "XML".

    .txt isn't really a "format", so to speak. You might want the "format" Perl command if that's what you mean. Not sure for .doc either because you'd really need a template.

    HTH
Re: Formatting text when outputting to a XLS file
by jmcnamara (Monsignor) on Feb 15, 2005 at 00:08 UTC

    In answer to your first question, it isn't possible to put a newline in a Excel cell using the tab delimited format that you show. (Strangely, Excel will store a tab delimited file with this feature but doesn't read it back in correctly).

    You can do it using a csv file by quoting cells that contain newlines, using only \n in the cell and \r\n for the end of line.

    Here is an example:

    #!/usr/bin/perl -w use strict; use Text::CSV_XS; my @data = ( ["Hello,\nworld" ], ["One\nTwo\nThree"], ); my $csv = Text::CSV_XS->new({binary => 1}); open CSV, "> example.csv" or die "Couldn't open file. $!\n"; binmode CSV; for my $aref (@data) { $csv->combine(@$aref); print CSV $csv->string(), "\r\n"; }

    (As an aside, anyone who recommends csv as an *easy* way of writing data to Excel should consider how arcane this example is).

    Using Spreadsheet::WriteExcel you can can achieve this effect by specifying a text_wrap format:

    #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new('example.xls'); my $worksheet = $workbook->add_worksheet(); my $format = $workbook->add_format(text_wrap => 1); $worksheet->write('A1', "Hello\nWorld", $format);

    See the following for other ways of writing Excel files.

    --
    John.