2011-05-24 32 views

回答

1

这是不可能与Spreadsheet::WriteExcel但它可能与Excel::Writer::XLSX

下面是一个例子:

#!/usr/bin/perl 

use strict; 
use warnings; 
use Excel::Writer::XLSX; 

my $workbook = Excel::Writer::XLSX->new('chart_pie.xlsx'); 
my $worksheet = $workbook->add_worksheet(); 
my $bold  = $workbook->add_format(bold => 1); 

# Add the worksheet data that the charts will refer to. 
my $headings = [ 'Category', 'Values' ]; 
my $data = [ 
    [ 'Apple', 'Cherry', 'Pecan' ], 
    [ 60,  30,  10  ], 
]; 

$worksheet->write('A1', $headings, $bold); 
$worksheet->write('A2', $data); 

# Create a new chart object. In this case an embedded chart. 
my $chart = $workbook->add_chart(type => 'pie', embedded => 1); 

# Configure the series. 
$chart->add_series(
    name  => 'Pie sales data', 
    categories => [ 'Sheet1', 1, 3, 0, 0 ], 
    values  => [ 'Sheet1', 1, 3, 1, 1 ], 
    data_labels => { value => 1 }, 
); 

# Add a title. 
$chart->set_title(name => 'Popular Pie Types'); 

# Set an Excel chart style. Colors with white outline and shadow. 
$chart->set_style(10); 

# Insert the chart into the worksheet (with an offset). 
$worksheet->insert_chart('C2', $chart, 25, 10); 

__END__ 

这是显示在图表它: enter image description here

+0

太谢谢你了!想知道,苹果/樱桃/山核桃有没有办法出现?另外,我查看了Excel :: Writer :: XLSX,并且它的语法看起来与Spreadsheet :: WriteExcel相似。这是真的还是我没有仔细观察? – petranaya 2011-05-25 14:21:20

+0

另外,是否有可能附加到其中有图形的excel文件? – petranaya 2011-05-25 14:54:20

+0

1.可以将名称,值或类别添加到data_labels中。有关更多信息,请参阅Chart.pm文档。 2. Excel :: Writer :: XLSX语法与Spreadsheet :: WriteExcel相同。它是API兼容的。目前在功能上存在一些差异,请参阅文档。 3.这个模块不能附加到现有的excel文件。 – jmcnamara 2011-05-25 15:04:35

相关问题