stonecolddevin has asked for the wisdom of the Perl Monks concerning the following question:
UPDATE 2: can't use DBD::AnyData as I have to use the current modules installed at this time. This project has to be done tonight, so the company I'm doing it for can go live with it tomorrow. Updated code. Suggestions?
Update: per bart's advice, I think I'll try DBD::AnyData, unless someone can suggest a more spreadsheet oriented module, or, of course, it doesn't work.
Hey gang,
I'm attempting to refactor some old code to convert the Excel sheet into an XML file based on a schema/template given to me. I'm getting some weird error messages from Spreadsheet::ParseExcel saying "Character in 'C' format wrapped in pack at C:/.../lib/Spreadsheet/ParseExcel/FmtDefault.pm line 68". Template::Toolkit seems to be doing just fine, as it's not throwing errors, and until Spreadsheet::ParseExcel comes into the picture, writes out the XML file with whatever variables I throw interpolated.
The code is as follows:
(Perl source)
#!perl -w use warnings; use strict; use Template; use Spreadsheet::ParseExcel; use Data::Dumper; ## define the xml template here my $xmltemplate = "learning.xtt"; ## directory to write xml files to my $write_directory = "xml/"; my $rand = rand($$); my $outputname = "$rand.xml"; ## set up the output settings and such my $xmlfile = Template->new({ OUTPUT_PATH => "xml" }) || die $Template +::ERROR; my $oExcel = new Spreadsheet::ParseExcel; my $filename = $ARGV[0]; chomp $filename; unless (-e $filename) { print "Can't find file $filename"; exit ; } ## read in the spread sheet my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename); ## begin conversion... my @raw_data = (); # excel data... foreach my $oWkS (@{$oBook->{Worksheet}}) { next unless defined $oWkS->{MinRow} and defined $oWkS->{MaxRow}; for my $iR ($oWkS->{MinRow} .. $oWkS->{MaxRow}) { for my $iC ($oWkS->{MinCol} .. $oWkS->{MaxCol}) { my %raw_data; my $oWkC = $oWkS->{Cells}[$iR][$iC]; next if ! defined $oWkC; my @columns = qw( id category code title group sub_group sequence role_mandato +ry role_recommended role_optional url modality length ); @raw_data{ @columns } = map { $oWkC->Value } @columns; } } } ## process the template print "Converting...\n\n"; print "Writing to file...\n\n"; open FH, ">", "debug.txt"; print FH Dumper (\@raw_data); close FH; #$xmlfile->process( $xmltemplate, # { # xml => \@excel_data # }, # $outputname ## for test purposes #) || die $Template::ERROR; print "Done!\n\n";
(The XML file {learning.xtt})
<?xml version="1.0" encoding="ISO-8859-1" ?> <star:learning-paths xmlns:star="http://schemas.sun.com/star-ns/" xml: +lang="en"> [% FOREACH xml %] <star:course order=""> <star:id>[% id %]</star:id> <star:category>[% category %]</star:category> <star:code>[% code %]</star:code> <star:title>[% title %]</star:title> <star:group>[% group %]</star:group> <star:sub-group>[% sub_group %]</star:sub-group> <star:sequence>[% sequence %]</star:sequence> <star:role-mandatory>[% role_mandatory %]</star:role-mandatory> <star:role-recommended>[% role_recommended %]</star:role-recommended +> <star:role-optional>[% role_optional %]</star:role-optional> <star:url>[% url %]</star:url> <star:modality>[% modality %]</star:modality> <star:length>[% length %]</star:length> </star:course> [% END %] </star:learning-paths>
Thanks in advance all. If the Excel file is needed, please /msg me.
P.S. bart said something aboutXML::Excel, but I'm not sure of the flexibility of this module, as I have to follow this template quite explicitly, and it doesn't seem to have many features at this version.
|
|---|