>-----Original Message-----
>From: John Delacour [mailto:johndelac...@gmail.com]
>Sent: Wednesday, January 05, 2011 18:32
>To: Perl Beginners
>Subject: Re: Getting LineFeed for Excel within q quoted field
>
>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~;
[Wags] 
[Wags] For something that should have been relatively easy, this has been a 
real pain in getting to work as I would have expected it to.
        I am using printf to do my printing and in this case, was using sprintf 
to place into a hash and when done, then I was doing the write to the file. I 
was placing within double quotes, but still ending up with the question marks 
within Excel.

        So I tried the binmode approach, but ended up a mixed bag there. Now 
everything was LF and the two columns that had the data with soft returns, the 
first one showed the question marks while the second column everything looked 
good.

        So to get things in what I believe was a correct setup and using the 
binmode which was giving me what I thought Excel wanted, I replaced my \n with 
\r\n and did the \x0a for the soft returns. Now when it is opened in Excel, 
there are no question marks in either of the two columns that I am doing soft 
returns..

        To me, this was way too much work for something that should have been 
relatively easy to accomplish. If I was doing anything OTHER than the \x0a, it 
was done. But since Perl is trying to help me, it took me down a road of way 
too much work to get this accomplished.

        I appreciate all the inputs and suggestions. Without them, I would not 
have completed what I needed to do.

        Thanks so much...
 
Wags ;) 
David R. Wagner 
Senior Programmer Analyst 
FedEx Services 
1.719.484.2097 Tel 
1.719.484.2419 Fax 
1.408.623.5963 Cell
http://Fedex.com/us

>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