in reply to Excel to XML issue with Spreadsheet::ParseExcel

dhoss you don't appear to have made any progress since last we talk about this, ooh, 12 hours ago? (You had put roughly the same code on your scratchpad.)

Like I told you then: "You can't just make shit up and expect Perl (or in this case, Template::Toolkit) to know what you mean." You're opening an Excel file, taking a reference to a worksheet, and feed it directly to Template::Toolkit, and expect it to extract what it needs, which I assume to be an AoH. (Actually it's worse still, you're passing a dereferenced (as an array) reference to the worksheet, hence, a list, not a single item.)

So I did the experiment, I replaced the reference to the worksheet with an array of hashes, and ran the code. And I do end up with an XML file I expected to end up with.

In order not to duplicate the effort, here's my sample code:

## process the template print "Converting...\n\n"; print "Writing to file...\n\n"; $xmlfile->process( $xmltemplate, { xml => [ { id => 'ID1', category => 'CATEGORY1', code => 'CODE1', title => 'TITLE1', group => 'GROUP', sub_group => 'SUB-GROUP1', sequence => 'SEQUENCE1', role_mandatory => 'ROLE-MANDATORY1', role_recommended => 'ROLE-RECOMMENDED1', role_optional => 'ROLE-OPTIONAL1', url => 'URL1', modality => 'MODALITY1', length => 'LENGTH1', }, { id => 'ID2', category => 'CATEGORY2', code => 'CODE2', title => 'TITLE2', group => 'GROUP', sub_group => 'SUB-GROUP2', sequence => 'SEQUENCE2', role_mandatory => 'ROLE-MANDATORY2', role_recommended => 'ROLE-RECOMMENDED2', role_optional => 'ROLE-OPTIONAL2', url => 'URL2', modality => 'MODALITY2', length => 'LENGTH2', } ] },

So that's apparently the format TT2 expects. "All" you still have to do, is actually extract the data out of the Excel file, and turn it into a proper AoH. The "old code" you linked to should work as a good source, you most likely have to process the file row by row, producing a hash per row, and the hash keys should be derived from the column names, either out of the headers (likely row 0, if present), or be hardcoded based on the column number. The cell contents will become the value in the hash.

Maybe there are easier ways, it could be that the AnyData/DBD::AnyData family of file readers could let you treat the spreadsheet as a database. Does it not? Or else, ODBC surely must be capable of it.

p.s. Did you think of XML-escaping the strings? I haven't looked, but somehow, I doubt it...
update I see that you haven't. Well, it may be a finishing touch, but still, it needs to be handled eventually, in order to get a solid product.

Replies are listed 'Best First'.
Re^2: Excel to XML issue with Spreadsheet::ParseExcel
by stonecolddevin (Parson) on Jan 17, 2007 at 22:56 UTC

    I appreciate the advice bart. I figured it would come down to something like you said, creating the AoH row by row.

    As far as XML-escaping, no, I haven't. The person I'm writing this for didn't mention anything like that, so I'm just going along with said directions.

    meh.