gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I am using Spreadsheet::WriteExcel and can not find this in the doc. Does anyone know if it is possable to write text vertically (rotated 90 degrees left or right) in a merged column?

Replies are listed 'Best First'.
Re: Spreadsheet::WriteExcel, rotate text
by jmcnamara (Monsignor) on Jul 10, 2002 at 21:45 UTC

    Merging cells is possible with Spreadsheet::WriteExcel and so is rotating text. However, strictly speaking it isn't possible to do both in the same cell or group of cells.

    This is due to the fact that Spreadsheet::WriteExcel is currently based on the Excel5 file format where merging and rotation are mutually exclusive.

    However Excel 97 and Excel 2000 ignore this fact and you can get the effect that you want using the code shown below.

    #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new('test.xls'); my $worksheet = $workbook->addworksheet(); my $format = $workbook->addformat( border => 1, color => 'blue', merge => 1, rotation => 1, ); $worksheet->write('B2', 'Hello', $format); $worksheet->write('C2', '', $format); $worksheet->write('D2', '', $format);

    However, you should consider this to be a kludge. ;-) Hopefully it will be possible to do this more robustly when the module moves to the Excel97+ format.
    --
    John.

      Thank you for your help. I have found another problem and then subsequently a solution for the writing vertically in a merged column. The trick is to write what you want to the destination cell and then merge the cells. If you merge them first and then write the data to the beginning cell of the merged column it only registers as being written to that cell and it is treated as though it is in a single cell rather than a merged column. Here is the working code:
      #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; # Variables my $file_name = 'switch_report.xls'; my $sheet_title='Traffic Estimations Report -May 2002'; my @array=( ['Switch 1','','above cell blank','456','789'], ['Switch 2','DEF','GHI'] ); #print $1; #my $array_ref=\$1; my $array_ref=\@array; # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new($file_name); # Add a worksheet my $worksheet = $workbook->addworksheet(); $worksheet->fit_to_pages(1,1); $worksheet->set_landscape(); $worksheet->set_margins(.25); # Add formats my $cb_format = $workbook->addformat(); # Add a format my $rotation_format = $workbook->addformat(); # Define Formats # $cb_format $cb_format->set_bold(); $cb_format->set_italic(); $cb_format->set_underline(); $cb_format->set_align('center'); $cb_format->set_size(18); # $rotation_format $rotation_format->set_rotation(2); $rotation_format->set_bold(); $rotation_format->set_align('vcenter'); # Write to the worksheet $worksheet->write(0,1,$sheet_title, $cb_format); $worksheet->merge_cells(0,1,2,29); $worksheet->merge_cells(3,1,3,29); #$worksheet->write_row(11,1,$array_ref); $worksheet->write(4,0,"Day of the month",$rotation_format); $worksheet->merge_cells(4,0,37,0); # write_switch_data my $row = 4; my $col = 1; $worksheet->write_row($row, $col, $array_ref);

        That's interesting. If you don't mind I'll add it to the Spreadsheet::WriteExcel documentation.

        It doesn't work with Excel 97 but it is still very useful.

        Thanks.
        --
        John.

Re: Spreadsheet::WriteExcel, rotate text
by RMGir (Prior) on Jul 10, 2002 at 19:01 UTC
    I found this reading through the Spreadsheet::WriteExcel perldoc, in the Spreadsheet::WriteExcel::Format section.

    It looks like you should create a format, then use set_rotation() on it.

    my $vertical=$workbook->addformat(); $vertical->set_rotation(1); # letters run from top to bottom # 2 for 90 degs anticlockwise # 3 for 90 degs clockwise
    Then just add the $vertical argument to the end of your write calls...
    $worksheet->write(0,0, "Title", $vertical);
    Edit: D'oh, missed the merged column part. ++jmcnamara!
    --
    Mike