I already have xlsx which consists of 9 sheets, out of which 1-7 sheet consists of graph while 8 and 9 sheets are blank. What I need to do is write some data in 8th and 9th sheet. Well I tried this using python but after wring the data in the xlsx sheet the graph and charts disappears.
Below code I have tried below perl code to modify the existing xls sheet
Note: Here I have converted the xlsx into xls and reused it.
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $template = $parser->Parse('TEMP.XLS');
my $worksheet = $template->worksheet('8');
my $row = 0;
my $col = 0;
$worksheet->AddCell( $row, $col, 'New string' );
$worksheet->AddCell( $row, $col + 1, 'Newer' );
$template->SaveAs('newfile.xls');
But it resulted as removal of graphs and charts. Can any one help me to do the same in perl or python.
Thanks in advance..
答案 0 :(得分:1)
此代码将保留旧代码并允许您添加一些新代码:
use strict;
use warnings;
use Excel::Writer::XLSX;
use Spreadsheet::Read;
use Data::Dumper;
# read the file
my $book = ReadData ('Factura.xls');
my @data;
my @sheets = (0..1);
my $workbook = Excel::Writer::XLSX->new( 'Factura_1.xls' );
my $worksheet;
# save the old data
foreach my $sheet (1 .. ($#sheets+1)) {
my @rows = Spreadsheet::Read::rows($book->[$sheet]);
$worksheet = $workbook->add_worksheet($sheet);
foreach my $i (1 .. scalar @rows) {
foreach my $j (1 .. scalar @{$rows[$i-1]}) {
$worksheet->write($i-1, $j-1, $rows[$i-1][$j-1] );
my $chart = $workbook->add_chart( type => 'bar', embedded => 1);
$chart->add_series(
name => '='."1".'!$A$0',
values => '='."1".'!$A$2:$A$15',
);
$worksheet->insert_chart( 'E2', $chart, 25, 10 );
}
}
}
# write new data
$worksheet = $workbook->add_worksheet(3);
$worksheet->write( 0, 0, 'New string' );