in reply to Formatting text when outputting to a XLS file


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.