Now, let me explain how this works. Instead of writing to your Excel file by hand with Spreadsheet::WriteExcel, you specify your layout using a template (that just happens to be in XML), pass it your data structure, and Excel::Template will convert that into the appropriate S::WE calls. (Yes, Excel::Template uses Spreadsheet::WriteExcel under the covers.)use Excel::Template; my $template = Excel::Template->new( file => \*DATA, ); my @data; for ($j = 1; $j <= $#model; $j++) { push @data, { device_model => $model[$j], devices_per_model => $device_per_model, devices_per_variant => $devicesPerVariant[$j], model_variant_processor => $processor[$j], device_memory => $memory[$j], device_software => $softwareVersion[$j], }; } $template->param( data => \@data, ); $template->write_to_file( "somefile.xls" ); __DATA__ <workbook> <worksheet> <row> <cell>Equipment Model</cell> <cell>Devices Per Model</cell> <cell>Devices Per Variant</cell> <cell>Model Variant Processor</cell> <cell>Device Memory</cell> <cell>Device Software</cell> </row> <loop name="data"> <row> <cell><var name="equipment_model"/></cell> <cell><var name="devices_per_model"/></cell> <cell><var name="devices_per_variant"/></cell> <cell><var name="model_variant_processor"/></cell> <cell><var name="device_memory"/></cell> <cell><var name="device_software"/></cell> </row> </loop> </worksheet> </workbook>
You have to convert your current data structure, which is a bunch of parallel arrays, into an array of hashes for the E::T loops to work correctly. This is the same data structure that HTML::Template, PDF::Template, and Template Toolkit all use. (By the way, this is a better data structure to use throughout your program, because it's more robust and all the data about a given thing is kept in one place - nothing can get out of sync.) That's what the @data array is for. Then, you specify your template, which is pretty self-explanatory.
Now, here's the major benefit of using Excel::Template vs. directly using S::WE. Let's say you wanted to make a whole section bold. Just put the whole section in <bold> tags. Or, let's say you wanted to add more items, or maybe another worksheet. Just modify the template, add the data to your data structure, and it's that simple. Plus, you can re-use the data structure with HTML::Template and PDF::Template, meaning you now have 3 formats from one data structure. AND, if there's any bugs in the Excel generation, assuming you've used E::T right, it's my fault, not yours. :-)
In reply to Re: problem using perl module Spreadsheet::WriteExcel cannot get data into rows when using for loop on a list to populate rows
by dragonchild
in thread problem using perl module Spreadsheet::WriteExcel cannot get data into rows when using for loop on a list to populate rows
by adamlee14
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |