Perl 輸出內容到 excel

能夠參考:  http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm函數

  1. 使用Spreadsheet::WriteExcel這個模塊,若是能很好的使用這個模塊,從perl輸出到excel的操做也就沒什麼問題了。利用它的幾個函數,就能夠方便地把數據寫入到Excel相應的位置中,同時還能夠設置單元格的格式,如字體大小,單元格大小,是否加粗,底色等等。這一篇爲基礎篇.
  2. 經過命令:perldoc perllocal來查看環境中裝了perl的哪些模塊,看看是否有這個模塊。
  3. 用perl建立excel表格
     
    #!/usr/bin/perl 
    use strict; 
    use Spreadsheet::WriteExcel;  
    #************生成Excel文檔****************  
    my $xl = Spreadsheet::WriteExcel->new("TEST.xls");  #引號中爲生成的excel的名稱,瘦箭頭後面都是模塊Spreadsheet::WriteExcel中的方面。
    #生成Excel表  
    my $xlsheet = $xl->add_worksheet("TestSheet");  #引號中爲excel工做簿中表的名稱
    $xlsheet->freeze_panes(1, 0); #凍結首行
  4. 輸出的格式設置
    #添加格式(表頭)
    my $rptheader = $xl->add_format(); # Add a format
    $rptheader->set_bold(); #加粗
    $rptheader->set_size('18'); #字體大小
    $rptheader->set_align('center'); #居中
    $rptheader->set_font('BrowalliaUPC'); #字體
    #添加格式(表內容)
    my $normcell = $xl->add_format(); # Add a format
    $normcell->set_size('11');
    $normcell->set_align('center');
    $normcell->set_bg_color('21'); #背景色
    #設置列的寬度
    $xlsheet->set_column('A:A',12);
    $xlsheet->set_column('B:B',10);
    $xlsheet->set_column('C:C',14);
     
  5. 輸出
    1. #寫表頭(格式是使用上面添加的表頭格式) 
      $xlsheet->write("A1","Number", $rptheader); #格式爲(單元格位置,寫入的內容,格式)
      $xlsheet->write("B1","Name",$rptheader);
      $xlsheet->write("C1","Language",$rptheader);
      #寫內容(格式是使用上面添加的表內容格式)
      $xlsheet->write("A2","1", $normcell);
      $xlsheet->write("B2","Test",$normcell);
      $xlsheet->write("C2","Perl",$normcell);
      #關閉操做excel的對象.
      $xl->close();
相關文章
相關標籤/搜索