At 17:56 -0600 04/01/2011, Wagner, David wrote:

I am generating an CSV and want a couple of fields to have soft returns in them. I went into Excel and added a couple of soft returns to a couple of different fields and then saved the modified file back to a CSV.

If you run this script you will have what you seem to want. So far as I know any line ending whether CR LF or CRLF is a new row signal and if you want new lines within a cell then you need to *enclose the cell contents in double quotes.*


#!/usr/bin/perl
use strict;
my $csv="temp.csv";
open CSV, ">$csv";
print CSV qq~1,2,"3\n4\n5"\n6,7,8~;
close CSV;
`start excel.exe "$csv"`;


On the Mac, and I guess on UNIX, then you need to use "\r" instead of "\n" to have the multiple lines show in the cell. As a new row signal the "\n" "\r" or "\r\n" will work fine on both platforms regardless of the different new line conventions.

#!/usr/bin/perl
use strict;
my $csv="temp.csv";
open CSV, ">$csv";
print CSV qq~1,2,"3\r4\r5"\n6,7,8~;
close CSV;
`open -a 'Microsoft Excel' '$csv'`;

JD

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to