jinhuang1992 has asked for the wisdom of the Perl Monks concerning the following question:

use Win32::OLE;
$file = "test.xls";
my $excel = Win32::OLE->new('Excel.Application');
my $book = $excel->Workbooks->Open($file);
my $sheet = $book->Worksheets(1);
my $range = "A1:C5";
$sheet->Range($range)->Interior->{ColorIndex} = 27;
$sheet->Range($range)->Font->{FontStyle} = "Bold";
$sheet->Range($range)->{HorizontalAlignment} = "Left";
$excel->Save();
$excel->Quit();

Question???
$sheet->Range($range)->{HorizontalAlignment} = "Left";
Why is the above line not working?

Replies are listed 'Best First'.
Re: Horizontal Alignment
by ikegami (Patriarch) on Jan 26, 2007 at 19:58 UTC
    According to the docs, the value needs to be one of the XlHAlign values. Note that XlHAlign is an enumeration, so the values are numbers. "Left" is definitely wrong. Constants can usually be imported using Win32::OLE::Const.
    use Win32::OLE::Const 'Microsoft Excel'; ... $sheet->Range($range)->{HorizontalAlignment} = xlHAlignCenter;

    Untested.

      Thank you very much. You've just given me the perfect answer.
Re: Horizontal Alignment
by SheridanCat (Pilgrim) on Jan 26, 2007 at 20:04 UTC
    I know you already rejected Spreadsheet::WriteExcel, but it should be noted, that module makes this easier:
    my $format = $workbook->add_format(); $format->set_align('center'); $format->set_align('vcenter');
    Also, please consider using code tags in your posts.
      ikegami gave me the answer that I want. use Win32::OLE::Const 'Microsoft Excel';
      ...,/br> $sheet->Range($range)->{HorizontalAlignment} = xlHAlignCenter;
      Thank you very much for your help.