Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Thanks !! (XML + Excel) with (win32::OLE and XML::Simple)

by Sporti69 (Acolyte)
on Dec 06, 2008 at 12:22 UTC ( [id://728537]=perlquestion: print w/replies, xml ) Need Help??

Sporti69 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

These last months I spend, from time to time, some effort in creating a script that parses XML to Excel. I want to THANK all of you guys for the hints/tips/ideas/help etc. I want to put it online to give something back to the community.

Maybe someone can make it faster, less lines, or just use it, study it, change it, whatever you want.

Explaination: The script uses the first 3 nested tags to get some general information. All the rest is divided into other spreadsheets with main and sub info data. Run it once, it's pretty cool. The code is 'quite dynamic' because there is almost nothing written in the code that is also in the xml file. I only do a check for an id, a number, or an enclosurenumber. If they exist, they are put in the first column of excel.

I use the script to get a nice overview of a storage boxes internal configuration

POST UPDATED: It contains the last version of the script with suggestions and errors fixed

Example XML

<?xml version="1.0" encoding="ISO-8859-1"?> <Enterprise olcVersion="1.0" versionLastModified="" dateLastModified=" +" versionCreated="" dateCreated="2007/11/28 13:50:07" changesPending= +"false" zPrivate="true" userName="Enterprise 2admin" userID="admin" > <StoragePlex online="false" id="1" userDescription="It is a hidden + storage plex." userName="internal_storage_plex" > <StorageFacility model="932" numESS="1" numEscon="0" numEthern +et="0" numFibreChannelLongWave="6" numFibreChannelShortWave="4" proce +ssorMemory="0" PowerControl="false" powerState="0" configuration="0" +userDescription="" userName="" sequence="00010" wwnn="0" type="2107" +> <ESS processorMemory="0" cacheMemory="0" nvsMemory="0" mod +el="932" essNetCSEnabled="false" limitEssNetCS="false" userDescriptio +n="" userName="Storage Facility Image 11" imageNumber="1" > <ArraySite dualLoop="0" arrayPosition="1" arraySiteSta +tus="0" requiredSpares="0" diskClass="1" diskInterfaceRate="2200" dis +kInterfaceType="FCAL" diskRPM="15000" diskCapacity="146000" deviceAda +pterPair="7" arrayNumber="57" number="58" /> <ArraySite dualLoop="0" arrayPosition="1" arraySiteSta +tus="0" requiredSpares="0" diskClass="1" diskInterfaceRate="2200" dis +kInterfaceType="FCAL" diskRPM="15000" diskCapacity="146000" deviceAda +pterPair="7" arrayNumber="56" number="57" /> <ArraySite dualLoop="0" arrayPosition="1" arraySiteSta +tus="0" requiredSpares="0" diskClass="1" diskInterfaceRate="2200" dis +kInterfaceType="FCAL" diskRPM="15000" diskCapacity="146000" deviceAda +pterPair="7" arrayNumber="55" number="56" /> <ArraySite dualLoop="0" arrayPosition="1" arraySiteSta +tus="0" requiredSpares="0" diskClass="1" diskInterfaceRate="2200" dis +kInterfaceType="FCAL" diskRPM="15000" diskCapacity="146000" deviceAda +pterPair="0" arrayNumber="7" number="8" /> <Rank errorCode="0" numUsedExtents="873" numExtents="9 +999" configurationState="0" dataState="0" extentSize="0" sizeOfStripe +="0" stripSize="0" trackSize="0" numNarrowArrays="0" numWideArrays="0 +" raidType="raid5" extentType="CKD" rankGroup="1" extentPoolNumber="1 +" number="1" > <ArrayRef arrayNumber="0" state="offline"/> <ExtentsByVol lssNumber="224" numExtentsUsed="27" volumeNu +mber="4" /> <ExtentsByVol lssNumber="224" numExtentsUsed="9" volumeNum +ber="16" /> <ExtentsByVol lssNumber="222" numExtentsUsed="9" volumeNum +ber="18" /> </Rank> <Rank errorCode="0" numUsedExtents="873" numExtents="873" conf +igurationState="0" dataState="0" extentSize="0" sizeOfStripe="0" stri +pSize="0" trackSize="0" numNarrowArrays="0" numWideArrays="0" raidTyp +e="raid5" extentType="CKD" rankGroup="2" extentPoolNumber="2" number= +"2" > <ArrayRef arrayNumber="1" /> <ExtentsByVol lssNumber="224" numExtentsUsed="27" volumeNu +mber="4" /> <ExtentsByVol lssNumber="224" numExtentsUsed="9" volumeNum +ber="16" /> <ExtentsByVol lssNumber="222" numExtentsUsed="9" volumeNum +ber="18" /> </Rank> <StorageEnclosure state="0" extEnclosureIndicator="0" rearIndi +cator="0" frontIndicator="0" alertIndicator="0" numStorageDevices="16 +" numStorageDeviceSlots="16" interfaceAddress="0" interfaceID="000000 +0000000000" featureCode="" enclosureNumber="11" serviceState="0" eCLe +vel="" rackNumber="0" location="S11" serialNum="" model="" type="" > <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="57" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> </StorageEnclosure> <StorageEnclosure state="0" extEnclosureIndicator="0" rearIndi +cator="0" frontIndicator="0" alertIndicator="0" numStorageDevices="16 +" numStorageDeviceSlots="16" interfaceAddress="0" interfaceID="000000 +0000000000" featureCode="" enclosureNumber="1" serviceState="0" eCLev +el="" rackNumber="0" location="S11" serialNum="" model="" type="" > <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="57" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> </StorageEnclosure> <StorageEnclosure state="0" extEnclosureIndicator="0" rearIndi +cator="0" frontIndicator="0" alertIndicator="0" numStorageDevices="16 +" numStorageDeviceSlots="16" interfaceAddress="0" interfaceID="000000 +0000000000" featureCode="" enclosureNumber="12" serviceState="0" eCLe +vel="" rackNumber="0" location="S11" serialNum="" model="" type="" > <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="57" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> </StorageEnclosure> <StorageEnclosure state="0" extEnclosureIndicator="0" rearIndi +cator="0" frontIndicator="0" alertIndicator="0" numStorageDevices="16 +" numStorageDeviceSlots="16" interfaceAddress="0" interfaceID="000000 +0000000000" featureCode="" enclosureNumber="2" serviceState="0" eCLev +el="" rackNumber="0" location="S11" serialNum="" model="" type="" > <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="58" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> <StorageDeviceFRU ddmType="" manufacturer="" temperatureSt +atus="0" dualLoop="0" serviceState="0" diskSerialNumber="" arraySiteN +umber="57" arraySitePosition="0" diskClass="1" diskUsage="0" diskInte +rfaceRate="0" diskInterfaceType="1" diskRPM="15000" diskCapacity="146 +000" deviceAdapterPairID="7" fruEnclNumber="11" diskIdentifier="0" st +ate="0" rackNumber="0" sequenceNum="" /> </StorageEnclosure> <IOPort logicalName="cpssfc0333" anonymousFICONPortGro +up="0" wwpn="5766023019803428496" topology="ScsiFcp" anonymousFICONEn +abled="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIA +ccess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="59" state="0" p +ortSpeed="0" portType="20" interfaceId="819" location="U1300.001.RJ42 +628-P1-C4-T3" id="819" /> <IOPort logicalName="cpssfc0332" anonymousFICONPortGro +up="0" wwpn="5766023019803412112" topology="Ficon" anonymousFICONEnab +led="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIAcc +ess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="58" state="0" por +tSpeed="0" portType="20" interfaceId="818" location="U1300.001.RJ4262 +8-P1-C4-T2" id="818" /> <IOPort logicalName="cpssfc0331" anonymousFICONPortGro +up="0" wwpn="5766023019803395728" topology="Ficon" anonymousFICONEnab +led="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIAcc +ess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="57" state="0" por +tSpeed="0" portType="20" interfaceId="817" location="U1300.001.RJ4262 +8-P1-C4-T1" id="817" /> <IOPort logicalName="cpssfc0002" anonymousFICONPortGro +up="0" wwpn="5766023019801642640" topology="ScsiFcp" anonymousFICONEn +abled="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIA +ccess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="2" state="0" po +rtSpeed="0" portType="21" interfaceId="2" location="U1300.001.RJ42629 +-P1-C1-T2" id="2" /> <IOPort logicalName="cpssfc0001" anonymousFICONPortGro +up="0" wwpn="5766023019801626256" topology="ScsiFcp" anonymousFICONEn +abled="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIA +ccess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="1" state="0" po +rtSpeed="0" portType="21" interfaceId="1" location="U1300.001.RJ42629 +-P1-C1-T1" id="1" /> <IOPort logicalName="cpssfc0000" anonymousFICONPortGro +up="0" wwpn="5766023019801609872" topology="ScsiFcp" anonymousFICONEn +abled="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIA +ccess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="0" state="0" po +rtSpeed="0" portType="21" interfaceId="0" location="U1300.001.RJ42629 +-P1-C1-T0" id="0" /> <IOPort logicalName="cpssfc0101" anonymousFICONPortGro +up="0" wwpn="5766023019802150544" topology="Ficon" anonymousFICONEnab +led="false" volumeGroupNumber="10" destinationID="0" anonymousSCSIAcc +ess="false" portSpeedStr="4 Gb/s" portMaskBitIndex="17" state="0" por +tSpeed="0" portType="20" interfaceId="257" location="U1300.001.RJ4263 +7-P1-C1-T1" id="257" /> <AddressGroup numConfiguredVolumes="4096" numLogicalSu +bsystem="16" numLogicalVolumeNumbers="4096" baseLogicalVolumeNumber=" +0" deviceType="CKD" number="13" /> <AddressGroup numConfiguredVolumes="3072" numLogicalSu +bsystem="12" numLogicalVolumeNumbers="4096" baseLogicalVolumeNumber=" +0" deviceType="CKD" number="14" /> <AddressGroup numConfiguredVolumes="9" numLogicalSubsy +stem="2" numLogicalVolumeNumbers="4096" baseLogicalVolumeNumber="0" d +eviceType="FB" number="0" /> <AddressGroup numConfiguredVolumes="4" numLogicalSubsy +stem="4" numLogicalVolumeNumbers="4096" baseLogicalVolumeNumber="0" d +eviceType="FB" number="1" /> <AddressGroup numConfiguredVolumes="2" numLogicalSubsy +stem="2" numLogicalVolumeNumbers="4096" baseLogicalVolumeNumber="0" d +eviceType="FB" number="2" /> <AddressGroup numConfiguredVolumes="4" numLogicalSubsy +stem="4" numLogicalVolumeNumbers="4096" baseLogicalVolumeNumber="0" d +eviceType="FB" number="3" /> <VolumeGroup type="SCSIMap256" userName="vg_svlirc17" +number="3" > <VolumeRef volumeNumber="1" lssNumber="0" /> <VolumeRef volumeNumber="2" lssNumber="0" /> <VolumeRef volumeNumber="1" lssNumber="1" /> <VolumeRef volumeNumber="2" lssNumber="1" /> </VolumeGroup> <VolumeGroup type="SCSIMap256" userName="vg_svlirc17" +number="1" > <VolumeRef volumeNumber="1" lssNumber="0" /> <VolumeRef volumeNumber="2" lssNumber="0" /> <VolumeRef volumeNumber="1" lssNumber="1" /> <VolumeRef volumeNumber="2" lssNumber="1" /> </VolumeGroup> <VolumeGroup type="SCSIMap256" userName="vg_svlirc17" +number="2" > <VolumeRef volumeNumber="1" lssNumber +="1" /> <VolumeRef volumeNumber="2" lssNumber="1" /> </VolumeGroup> </ESS> </StorageFacility> </StoragePlex> </Enterprise>

The script

###################################################################### +####################################### # Script that parses first 3 xml levels to an excel sheet "general inf +ormation". All the level4 tags # # get their own sheet. Every sheet has main information (title and hea +der) and, if available deeper # # levels, it also has subinformation on the spreadsheet. This subinfor +mation is the name of the element # # With all of the containing tags + values in a title/value layout (li +ke the main information) # # The script will parse all of the xml files it finds in your working +directory. # ###################################################################### +####################################### #!/usr/bin/perl -- use strict; use warnings; use Cwd; use Win32::OLE; use XML::Simple; use Win32::OLE::Const 'Microsoft Excel'; ########################################## # Get all xml files in working directory # ########################################## my $dir = cwd(); opendir(DIR, $dir) || die "can't opendir $dir: $!"; print "$dir\n"; my @files = grep(/\.xml$/,readdir(DIR)); closedir(DIR); use Data::Dumper; local $Data::Dumper::Indent=1; #print Dumper($doc); ######################## # Ask for confirmation # ######################## my $input = "x"; while ($input ne "y") { print "\n\nThis script will try to parse all the xml files in the +scripts folder. \nThis might take up to 5 minutes depending on the xm +l file(s) size. \nYou can see what is being parsed at a certain time. +\n\nDo you want to run the script? [y/n]\n"; $input = <STDIN>; chomp $input; } unless( scalar @files ){ print "\n\nNo Files Found in $dir,\nMake sure your script is in a di +rectory with the XML files\n\n"; exit; } parseFile($_) for @files; sub writeNonArray { my ($worksheet, $rowCount, $colCount, $header, $value) = @_; $worksheet->Cells($rowCount, $colCount)->{Value} = $header; $worksheet->Cells($rowCount, $colCount)->Borders() -> {Weight} = 2; $colCount++; $worksheet->Cells($rowCount, $colCount)->{Value} = $value; $worksheet->Cells($rowCount, $colCount)->Borders() -> {Weight} = 2; $rowCount++; $colCount--; return ($rowCount, $colCount); } sub parseFile { print "\n#############################################\n\nInitializing + script ...\n \nAnalysing file $_[0]\n\n############################# +################\n\n"; ####################### # Excel Initialisation# ####################### my $xs1 = XML::Simple->new(); my $doc = $xs1->XMLin($_[0], keyattr=>[], ForceContent=>1, ForceArray= +>1); my $application = Win32::OLE->new("Excel.Application"); $application->{DisplayAlerts} = 0; my $workbook = $application->Workbooks->Add(); for (my $count = 3; $count >= 0; $count--) { if ($workbook->WorkSheets($count)) {$workbook->WorkSheets($count)->Del +ete();} } my $worksheet = $workbook->Worksheets->Add({After=>$workbook->Workshee +ts($workbook->Worksheets->{Count})}); ############################################ # First worksheet with general information # ############################################ $worksheet -> {Name} = "General Information"; $worksheet -> Range("A1") -> Font -> {Size}= 14; $worksheet -> Range("A1") -> Font -> {ColorIndex}= 2; $worksheet -> Range("A1") -> {Value} = "General Information"; $worksheet -> Range("A1:E1") -> Merge; $worksheet -> Range("A1:E1") -> Interior -> {ColorIndex} = 25; $worksheet -> Range("A1:E1") -> Borders() -> {Weight} = 3; #################################################################### # Excel index variables for writing to column and row with headers # #################################################################### my $LastRow = 0; my $new = 0; my $mainHeaders = 3; #main header row my $rowCount=4; #main info row my $colCount = 2; #main info column my $subHeaderRow = 0; #additional info header row my $subDataRow = 50; #additional info data row print "Collecting general information..."; foreach my $Level1 ( sort keys %{ $doc } ){ my $aoh1 = $doc->{$Level1}; unless (ref($aoh1) eq 'ARRAY') { ($rowCount,$colCount) = writeNonArray($worksheet, $rowCount, $ +colCount, $Level1, $aoh1); next; } my $count1 = $#{ $aoh1 }; foreach my $i(0 .. $count1) { foreach my $Level2 ( sort keys %{ $doc->{$Level1}[$i] } ){ my $aoh2 = $doc->{$Level1}[$i]{$Level2}; unless (ref($aoh2) eq 'ARRAY') { ($rowCount,$colCount) = writeNonArray($worksheet, $row +Count, $colCount, $Level2, $aoh2); next; } my $count2 = $#{ $aoh2 }; foreach my $j(0 .. $count2) { foreach my $Level3 ( sort keys %{ $doc->{$Level1}[$i]{ +$Level2}[$j] } ){ my $aoh3 = $doc->{$Level1}[$i]{$Level2}[$j]{$Level +3}; unless (ref($aoh3) eq 'ARRAY') { ($rowCount,$colCount) = writeNonArray($workshe +et, $rowCount, $colCount, $Level3, $aoh3); next; } my $count3 = $#{ $aoh3 }; foreach my $k(0 .. $count3) { foreach my $Level4 ( sort keys %{ $doc->{$Leve +l1}[$i]{$Level2}[$j]{$Level3}[$k] } ){ my $aoh4 = $doc->{$Level1}[$i]{$Level2}[$j +]{$Level3}[$k]{$Level4}; unless (ref($aoh4) eq 'ARRAY') { ($rowCount,$colCount) = writeNonArray( +$worksheet, $rowCount, $colCount, $Level4, $aoh4); + next; } print "Completed\nCollecting $Level4 ..."; my $count4 = $#{ $aoh4 }; $subDataRow = ($count4 + 1) + ($rowCount + + 1); my $worksheet = $workbook->Worksheets->Add +({After=>$workbook->Worksheets($workbook->Worksheets->{Count})}); $worksheet -> {Name} = $Level4; $worksheet -> Range("A1") -> Font -> {Size +}= 14; $worksheet -> Range("A1") -> Font -> {Colo +rIndex}= 2; $worksheet -> Range("A1") -> {Value} = "$L +evel4"; $worksheet -> Range("A1:E1") -> Merge; $worksheet -> Range("A1:E1") -> Interior - +> {ColorIndex} = 25; $worksheet -> Range("A1:E1") -> Borders() +-> {Weight} = 3; my $subDataCol = 1; foreach my $l(0 .. $count4) { my $temp = ""; foreach my $Level5 ( sort keys %{ $doc +->{$Level1}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4}[$l] } ){ my $aoh5 = $doc->{$Level1}[$i]{$Le +vel2}[$j]{$Level3}[$k]{$Level4}[$l]{$Level5}; my $curID = 0; my $nextup = ""; if($doc->{$Level1 +}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4}[$l]{number}){ $curID = $doc- +>{$Level1}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4}[$l]{number};} if($doc->{$Level1 +}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4}[$l]{enclosureNumber}){ $curI +D = $doc->{$Level1}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4}[$l]{enclos +ureNumber};} if(ref($aoh5)){ ################# +####### # EXCEL SUBINFORM +ATION # ################# +####### my $count5 = +$#{ $aoh5 }; foreach m +y $m(0 .. $count5) { if($n +extup ne $Level5){ $ +new = 1; $ +subDataRow++; $ +subHeaderRow = $subDataRow; $ +subDataCol = 1 ; $ +nextup = $Level5; } else +{$new = 0;} $subDataRow++; foreach my $Level6 + ( sort keys %{ $doc->{$Level1}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4 +}[$l]{$Level5}[$m]} ){ + # print "\n$Level1->$Level2->$Level3->$Level4->$Level5->$Level6 : +$doc->{$Level1}[$i]{$Level2}[$j]{$Level3}[$k]{$Level4}[$l]{$Level5}[$ +m]{$Level6}"; # subinfo titl +e for new element eg "Rank : 1" in colors if ($temp ne $ +curID){ $temp = $c +urID; $worksheet +->Cells($subDataRow, $subDataCol)->{Value} = "$Level4 : $curID"; $worksheet +->Cells($subDataRow, $subDataCol)->Interior->{ColorIndex} = 33; $worksheet +->Cells($subDataRow, $subDataCol)->{Font}->{Bold} = 1; $worksheet + ->Cells($subDataRow, $subDataCol)-> Borders() -> {Weight} = 1; $subDataRo +w+=2; $subHeader +Row = $subDataRow; $subDataRo +w++; } # We should wr +ite the header only once if($new==1){ $worksheet +->Cells($subHeaderRow, $subDataCol)->{Value} = $Level6; $worksheet +->Cells($subHeaderRow, $subDataCol)->{Font}->{Bold} = 1; $worksheet + ->Cells($subHeaderRow, $subDataCol)-> Borders() -> {Weight} = 2; $worksheet + ->Cells($subHeaderRow, $subDataCol)-> Borders() -> {ColorIndex} = 25 +; } $worksheet +->Cells($subDataRow, $subDataCol)->{Value} = $doc->{$Level1}[$i]{$Lev +el2}[$j]{$Level3}[$k]{$Level4}[$l]{$Level5}[$m]{$Level6}; $subDataCol++; } $subDataCol=1; } } else{ ################# +######### # EXCEL MAIN INFO +RMATION # ################# +######### # Index, if a +ny of those, will be placed in first column if ($Level5 e +q "number" | $Level5 eq "id" | $Level5 eq "enclosureNumber"){ $workshee +t->Cells($mainHeaders, 1)->{Value} = $Level5; $worksheet->Cells($mainHea +ders, 1)->{Font} -> {ColorIndex} = 25; $worksheet->Cells($mainHea +ders, 1)->{Interior} -> {ColorIndex} = 15; $worksheet->Cells($mainHea +ders, 1)->Borders() -> {Weight} = 2; $worksheet->Cells($mainHea +ders, 1)->{Font}->{Bold} = 1; $worksheet->Cell +s($rowCount, 1)->{Value} = $doc->{$Level1}[$i]{$Level2}[$j]{$Level3}[ +$k]{$Level4}[$l]{$Level5}; $worksheet->Cells($rowCoun +t, 1)->{Font}->{Bold} = 1; $worksheet->Cells($rowCoun +t, 1)->Borders() -> {Weight} = 2; } else{ unless ($ +worksheet->Cells($mainHeaders, $colCount)->{Value}){ $work +sheet->Cells($mainHeaders, $colCount)->{Value} = $Level5; } if ($Level5 eq "ww +pn"){ $worksheet->Cells($row +Count, $colCount)->{NumberFormat} = "0"; $worksheet->Cells($row +Count, $colCount)->Borders() -> {Weight} = 2; + } $workshee +t->Cells($rowCount, $colCount)->Borders() -> {Weight} = 2; $workshee +t->Cells($rowCount, $colCount)->{Value} = $doc->{$Level1}[$i]{$Level2 +}[$j]{$Level3}[$k]{$Level4}[$l]{$Level5}; $workshee +t->Cells($mainHeaders, $colCount)->Borders() -> {Weight} = 2; $worksheet->Cells($mainHea +ders, $colCount)->{Font}->{Bold} = 1; $worksheet->Cells($mainHea +ders, $colCount)->{Font} -> {ColorIndex} = 25; $worksheet->Cells($mainHea +ders, $colCount)->{Interior} -> {ColorIndex} = 15; $colCount++; } } } $rowCount++; $subDataRow++; $colCount=2; $worksheet -> Range("A:X") -> {Colum +ns} -> Autofit; } $subDataRow=$rowCount+2;; $colCount=2; $rowCount=4; #Seperate general information on first sheet. } $rowCount=4; $colCount+=3; } } $rowCount=4; $colCount+=3; } } $rowCount=4; $colCount+=3; } } $LastRow = $worksheet->UsedRange->Find({What=>"*", SearchDirection=>xl +Previous, SearchOrder=>xlByRows})->{Row}; + $worksheet -> Range("1:$LastRow") -> {Columns} -> Autofit; $workbook->SaveAs($dir . '/'.$_[0].'.xls'); $workbook->Close; print "\n\n$_[0].xls generated in $dir\n" ; #print "\a"; }

Thank you all again for the help, new suggestions are welcome. And no, I don't know how to use referencing. I wasn't allowed to spend much time studying perl.

Replies are listed 'Best First'.
Re: Thanks !! (XML + Excel) with (win32::OLE and XML::Simple)
by davidrw (Prior) on Dec 06, 2008 at 15:46 UTC
    Here're my code review comments:
    1. The opendir/readdir/closedir lines become one using glob or something like File::Find::Rule.
    2. $dir is set, but then "." is hardcoded in the copendir.
    3. Prompt text says "all the xml files in the scripts folder" but the script looks for ./*.xml
    4. my $a = "x"; while ($a ne "y") { ... chop $a; }
      can be: while( my $input ne "y" ){ ... ; chomp $input; }
      Avoid $a and $b as var names, since they are non-descriptive, and have special meaning (see sort).
      Don't blindly chop -- use chomp.
    5. Or, just remove that whole while block ... if they didn't wany it run, they shouldn't have executed the script :)
      (yes, you could argue that they ran the script accidentally. Well, then i'd say it's just as likely they accidently hit "y" when they meant "n" to the confirmation, and you should ask again. and again. and .... :) ) Just makes the script have less moving parts (reaD: easier to maintain).
    6. unless ($files[0]){ print "\nSorry, no xml file(s) in script folder\n\nScript Terminat +ed...\n\n"; } else{ ... # a gazillion rows }
      This should read (use array in scalar context; and avoid large block):
      unless( scalar @files ){ warn "no files"; exit; } ...
      You may even want to die instead.
    7. You might want to make a parse_file() sub, so the main code then becomes:
      parse_file($_) for @files;
    8. for my $i(0 .. $count) {
      This is most commonly written as
      foreach my $i ( 0 .. $count ){ <li> to be more explicit (since you want an array), do <c>unless( ref( +$doc->{$sub1}) eq 'ARRAY' ){

      Does it even happen that these aren't arrays? if so, nod. if not, maybe just ditch that clause (and if it ever happens by mistake, it'll just error out).
    9. I think the nested loops become easier to deal with if you work with array values instead of indexes ... something like:
      foreach my $sub1 ( sort keys %{ $doc } ){ ... # $sub1 foreach my $sub2Hash ( @{ $doc->{$sub1} } ){ for my $sub2 ( sort keys %$sub2Hash ){ unless (ref($sub2Hash{$sub2})) { ... # $sub2 $worksheet->Cells($rowCount, $colCount)->{Value} = $su +b2; $worksheet->Cells($rowCount, $colCount)->{Value} = $su +b2Hash{$sub2}; next; } foreach my $sub3Hash( @{$sub2Hash{$sub2}} ){ for my $sub3 ( sort keys %$sub3Hash ){
    10. BUT, I notice that, at each nested level, it all looks the same -- should break that out into a function. Here's something that does that, and the previous comment about using variables as you go: NOTE: I noticed after i wrote it out that the while loses the sort keys functionality you had. If that's important, you can replace while( my ($sub2, $aoh2) = each %$sub2Hash ){ with:
      foreach my $sub2 ( sort keys %$sub2Hash ){ my $aoh2 = $sub2Hash->{$sub2};

      Note: i also feel like there should be a much more elegant (& shorter/less cumbersome) solution maybe recursing through those 6 levels .. at least through the first ~4 or 5. (don't have time at the moment to write it out)
      Nice feedback !!!

      I applied your first 8 remarks to the script.

      Your remark 9: well level 1,2 and 3 are put into one sheet and thus they are arrays, but all the other differ. Even in the deepest level it can be eighter array or hash. I hope you (guys) ran the script to give it a go.

      I cannot try out the other suggestions yet because of the error written below, strange

      I will paste the updated version, BUT I have an error now saying: Couldn't open encmap iso-859-1.enc: No such file or directory at C:/Perl/lib/XML/Parser.pm line 187 I dont get this ? It finds the file as you can see in the output, and I do not use the parser module in the script.

      What's wrong, or do only I have this error all of a sudden.

      XML File is in the first post, this is the updated script:
      Hey all,

      I added the 10 comments you made. Thanks again!

      You oversaw a few things in the suggestions. I added an variable to the function because the sublevel wasn't enough, it was the attributes title, not the value

      Only problem is that your suggestion doesn't keep them sorted any more

      Anyways I changed th sub into level, and some other variables, I renamed more obvious. Documented the code aswell.

      Maybe you can see some other shortcuts now the code looks very nice.

      And the XML again

      To all

      How can we make this even better ?
        Only problem is that your suggestion doesn't keep them sorted any more
        I noted that i at the bottom of my suggestion (after the code blocks), w/how to fix it:
        NOTE: I noticed after i wrote it out that the while loses the sort keys functionality you had. If that's important, you can replace while( my ($sub2, $aoh2) = each %$sub2Hash ){ with:
        foreach my $sub2 ( sort keys %$sub2Hash ){ my $aoh2 = $sub2Hash->{$sub2};
Re: Thanks !! (XML + Excel) with (win32::OLE and XML::Simple)
by AnomalousMonk (Archbishop) on Dec 06, 2008 at 18:09 UTC
    I believe your thanks will be appreciated by all in the monastery.

    Comment: Is there any way Sporti69 could be persuaded to enclose the huge chunks of data and code in the OP in <readmore> ... </readmore> tags?

      Now that I discovered the readmore I wil enjoy using it :) Thx m8

      Updated my posts

      Any ideas on my error ?
        I found out what the error is. Encoding reference in the xml files was invalid. I changed, also in this topic, to <?xml version="1.0" encoding="ISO-8859-1"?>

        I will try to get the last two remarks in the code now.

        Keeping you guys up to date

Re: Thanks !! (XML + Excel) with (win32::OLE and XML::Simple)
by matrixmadhan (Beadle) on Dec 09, 2008 at 03:37 UTC
    I have got couple of comments to be updated

    <?xml version="1.0" encoding="ISO-859-1"?>
    >>encoding information is wrong, that should be a typo to ISO-8589-1

    opendir(DIR, ".");
    >> return status should be checked
    >> opendir(DIR, ".") or die "Unable to open directory <$!>\n";

    closedir(DIR);
    >> closing the directory also might fail, its better to check the return status
    >> closedir(DIR) or die "Unable to close the directory <$!>\n";

    while ($a ne "y")
    >> What if the user is not interested to process the xml files. This seems to be forcing user to select an option to process the files
    >> If this is going to be modified in such a way that user can input either "y" or "n", then prefetching of xml files into array should be done only after based on user's input

    $a = <STDIN>;    chop $a;
    >> I think this is better chomp( $a = <STDIN );

    my $xs1 = XML::Simple->new();
    >> there is no need to create an object for each and every file
    >> same thing applies to other objects repeatitively created where just one instance can be made use of.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://728537]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-03-28 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found