in reply to Reaped: Re^6: XLS to CSV file with Text wrap
in thread XLS to CSV file with Text wrap

Hi Poj ,

I have resolved the Text wrap issue with the below stated change in the code . Now I could able to see the Text is getting wrapped properly with newlines in the newly generated XLS. Now there is only one problem , The column width of the wrapped columns is too large which is taking more space for the wrapped text. How can we reduce the size of the column width so that it will autofit the wrapped content .

Please find my script.

#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use Text::Wrap; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('Project_status_tracking_DFT.xls'); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $workbook1 = Spreadsheet::WriteExcel->new('Homested_Status.xls'); my @color = qw(white green orange pink blue magenta silver); #my @format; my $format1 = $workbook1->add_format(bg_color => 'green' ); #$format1->set_text_wrap(); $format1->set_align('vjustify'); #for my $i (1..@color){ # $format[$i] = $workbook1->add_format(bg_color => $color[$i]); # $format[$i]->set_align('vjustify'); #$format[$i]->set_text_wrap(); #} for my $worksheet ( $workbook->worksheets() ) { my $worksheet1 = $workbook1->add_worksheet(); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); # set minimum column width my @width; $width[$_] = 10 for ($col_min .. $col_max); for my $row ( $row_min .. $row_max ) { my @value = (); # my @value_1 = (); # my @value_2 = (); my $fmt; # format for complete row # store row in array for my $col ( $col_min .. $col_max ) { #local $Text::Wrap::columns = 20; #$value = wrap ("", "", $source_cell->Value); my $cell = $worksheet->get_cell( $row, $col ); if (defined $cell) { local $Text::Wrap::columns = 60; #$Text::Wrap::separator="\n"; my $value = wrap("", "", $cell->value()); #$longdna_string =~ s/.{60}\K/\n/g; # my $value = $cell->value(); #$value =~ s/.{60}\K/\n/g; if ($value=~ /DFT Tasks for Homestead/ .. $value =~ /END + OF HOMESTEAD/){ $value[$col] = $value; if ($value[$col] =~ m/Completed/i) { $fmt = $format1; } # determine max width for each column my $len = length($value[$col]); $width[$col] = $len if $len > $width[$col]; } } } # write row for my $col ( $col_min .. $col_max ) { $worksheet1->write($row, $col, $value[$col],$fmt); } } # set column widths for my $col ( $col_min .. $col_max ) { $worksheet1->set_column($col,$col,$width[$col]) } }
Thanks Kshitij Kulshreshtha