Sponsored Content
Top Forums Shell Programming and Scripting Writing xml from excel sheet .xls using perl script Post 302935368 by omkar.jadhav on Monday 16th of February 2015 03:32:45 AM
Old 02-16-2015
Thanks all for all your input...the syetm is windows...I have below script which will accept data from xls and generate a xml data , but the problem is its also giving me column headers in the excel , i think i am doing some silly mistake in the code..can someplease please suggest me how i can exclude the columns headers appearing in the output :
below is the code :
Code:
 
use strict;
use warnings;
use Spreadsheet::ParseExcel;
use XML::Writer;
use Time::Piece;
my $date = localtime->strftime('%Y-%m-%d');
my $date1 = localtime->strftime('%a, %d %b %Y %H:%M:%S +0000');
my @columns = ('ID', 'Name');
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('myfile.xls') or die $parser->error();
my $worksheet = $workbook->worksheet(0);
my ( $row_min, $row_max ) = $worksheet->row_range();
my @data;
for my $row ( $row_min .. $row_max ) {
    my %hash;
    for my $col (0 .. $#columns) {
        my $cell = $worksheet->get_cell( $row, $col );
        $hash{$columns[$col]} = $cell->value();
    }
    push(@data,\%hash)
}
my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1);
$writer->xmlDecl("UTF-8");
$writer->startTag("BES", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation" => "BES.xsd");
$writer->startTag('Baseline');
$writer->dataElement(Title => "new baseline");
$writer->dataElement(Description => "");
$writer->dataElement(Relevance => "true");
$writer->dataElement(Category=> "");
$writer->dataElement(Source => "Internal");
$writer->dataElement(SourceID => "");
$writer->dataElement(SourceReleaseDate => $date);
$writer->dataElement(SourceSeverity => "");
$writer->dataElement(CVENames => "");
$writer->dataElement(SANSID => "");
$writer->startTag('MIMEField');
$writer->dataElement(Name => "x-fixlet-modification-time");
$writer->dataElement(Value => $date1);
$writer->endTag('MIMEField');
$writer->dataElement(Domain => "BESC");
$writer->startTag('BaselineComponentCollection');
$writer->startTag('BaselineComponentGroup');
for my $row (@data) {
    $writer->startTag("BaselineComponent", Name => $row->{'Name'}, IncludeInRelevance => "true", SourceSiteURL => "http://sync.bigfix.com/cgi-bin/bfgather/bessecurity",SourceID => $row->{'ID'}, ActionName => "Action1");
 $writer->dataElement(ActionScript => "");
 $writer->dataElement(Relevance => "");
 $writer->endTag('BaselineComponent');
}
 $writer->endTag('BaselineComponentGroup');
 $writer->endTag('BaselineComponentCollection');
 $writer->endTag('Baseline');
 $writer->endTag('BES');
$writer->end;
print $writer->to_string;

below is the actual output :
Code:
 
<BES xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchema
Location="BES.xsd">
<Baseline>
<Title>new baseline</Title>
<Description></Description>
<Relevance>true</Relevance>
<Category></Category>
<Source>Internal</Source>
<SourceID></SourceID>
<SourceReleaseDate>2015-02-16</SourceReleaseDate>
<SourceSeverity></SourceSeverity>
<CVENames></CVENames>
<SANSID></SANSID>
<MIMEField>
<Name>x-fixlet-modification-time</Name>
<Value>Mon, 16 Feb 2015 13:56:59 +0000</Value>
</MIMEField>
<Domain>BESC</Domain>
<BaselineComponentCollection>
<BaselineComponentGroup>
<BaselineComponent Name="Name" IncludeInRelevance="true" SourceSiteURL="http://s
ync.bigfix.com/cgi-bin/bfgather/bessecurity" SourceID="ID" ActionName="Action1">
<ActionScript></ActionScript>
<Relevance></Relevance>
</BaselineComponent>
<BaselineComponent Name="MS15-010: Vulnerabilities in Windows Kernel-Mode Driver
 Could Allow Remote Code Execution - Windows Server 2008 R2 SP1 - KB3013455 (x64
)" IncludeInRelevance="true" SourceSiteURL="404 Not Found
her/bessecurity" SourceID="1501031" ActionName="Action1">
<ActionScript></ActionScript>
<Relevance></Relevance>
</BaselineComponent>
<BaselineComponent Name="MS15-011: Vulnerability in Group Policy Could Allow Rem
ote Code Execution - Windows Server 2008 R2 SP1 - KB3000483 (x64)" IncludeInRele
vance="true" SourceSiteURL="http://sync.bigfix.com/cgi-bin/bfgather/bessecurity"
 SourceID="1501121" ActionName="Action1">
<ActionScript></ActionScript>
<Relevance></Relevance>
</BaselineComponent>
<BaselineComponent Name="MS15-002: Vulnerability in Windows Telnet Service Could
 Allow Remote Code Execution - Windows Server 2003 SP2 - KB3020393" IncludeInRel
evance="true" SourceSiteURL="http://sync.bigfix.com/cgi-bin/bfgather/bessecurity
" SourceID="1500215" ActionName="Action1">
<ActionScript></ActionScript>
<Relevance></Relevance>
</BaselineComponent>
<BaselineComponent Name="MS14-064: Vulnerabilities in Windows OLE Could Allow Re
mote Code Execution - Windows Server 2003 SP2 - KB3006226" IncludeInRelevance="t
rue" SourceSiteURL="http://sync.bigfix.com/cgi-bin/bfgather/bessecurity" SourceI
D="1406453" ActionName="Action1">
<ActionScript></ActionScript>
<Relevance></Relevance>
</BaselineComponent>
<BaselineComponent Name="MS14-022: Vulnerabilities in Microsoft SharePoint Serve
r Could Allow Remote Code Execution - SharePoint Server 2013 Client Components S
DK - KB2863854 (x64)" IncludeInRelevance="true" SourceSiteURL="http://sync.bigfi
x.com/cgi-bin/bfgather/bessecurity" SourceID="1402215" ActionName="Action1">
<ActionScript></ActionScript>
<Relevance></Relevance>
</BaselineComponent>
</BaselineComponentGroup>
</BaselineComponentCollection>
</Baseline>
</BES>

as you can see while convwerting the xls which has two column headers - Name and ID. XML output is also including it as below :

<BaselineComponent Name="Name" IncludeInRelevance="true" SourceSiteURL="http://s
ync.bigfix.com/cgi-bin/bfgather/bessecurity" SourceID="ID" ActionName="Action1">

i want the to be removed and script should only convert rest of the data excluding column headers.

Also it will be really helpful if one can let me know how i can directly pass the output in xml format in a separate file (for example : sample.bes) how to inlcude that logic in script itself or i can give a user to allow him to enter the desired file name..something like read statement in shell script ...

Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Excel sheet modification using perl module

Is there any possibility to move the content from one cell to another cell (Excel sheet) using perl module? (3 Replies)
Discussion started by: kavi.mogu
3 Replies

2. Shell Programming and Scripting

Excel sheet modification using perl module

I need to insert new column to already existing file ..can any one help me..?? (6 Replies)
Discussion started by: kavi.mogu
6 Replies

3. Shell Programming and Scripting

Excel sheet modification using perl module

Hi , Is there any possibility to read excel sheet in column by column order ?...Thanks in advance,........ :confused: (1 Reply)
Discussion started by: kavi.mogu
1 Replies

4. Programming

Excel sheet modification using perl module

Hi , can any one tell me,"How to extract the same format from existing excel file to new excel file " using Spreadsheet::WriteExcel or Spreadsheet::ParseExcel module ??? Example_pgm: Below program is used to read existing excel file..In this program "my $cell = $_;" line is used to... (0 Replies)
Discussion started by: kavi.mogu
0 Replies

5. Shell Programming and Scripting

Perl : not capturing all the data from excel sheet

Hi folks, I am working on assignment that captures all the records(2 columns one column contains names and other contain date of birth) from excel sheet stored in a directory and checks for current date and month. If it matches current date and month then the matched records are printed as... (1 Reply)
Discussion started by: giridhar276
1 Replies

6. Shell Programming and Scripting

Perl : Deleting the records in the excel sheet

I have a excel sheet with contains the records as below.. also uploaded the input excelsheet and the output excel sheet(expected output). 322mpls32.net.xyz.comBW: 44.0 M Hrly Avg (IN /... (1 Reply)
Discussion started by: giridhar276
1 Replies

7. Shell Programming and Scripting

Perl Reading Excel sheet isssue

There is a perl scriptwhich will read Excel sheet and create one file(.v) . Excel sheet::: A B C D 1 cpu_dailog 2 3 4 Perl will create the file(.v) like thsi ::: assert (cpu_dailog_iso ==2) ; assert (cpu_dailog_reset ==3); assert (cpu_dailog_idle... (3 Replies)
Discussion started by: naaj_ila
3 Replies

8. Shell Programming and Scripting

Perl : to get all the hyperlinks from the xlsx sheet(hyperlinks not visible in excel sheet directly)

Hi folks, I have a requirement in perl to print all the hyperlink from the spreadsheet(xlsx). Spreadsheet contains few lines of hyperlink data (pic attached). P.S. Hyperlink is behind the data and not visible in excel sheet directly. Now using perl script I need to copy the hyperlinks in... (3 Replies)
Discussion started by: scriptscript
3 Replies

9. Shell Programming and Scripting

Summing up the data from different excel sheet into one excel sheet

Hi Folks, Can you please advise for any script in unix such that for example , i have 3 different excel sheet at the location /ppt/gfr/exc so the name s of the excel sheet are 1excel.xslx 2excel.xslx 3excel.xslx now in these 3 different excel sheet there is lot of data for example each... (3 Replies)
Discussion started by: punpun66
3 Replies

10. Shell Programming and Scripting

Writing in excel sheet using ksh

Hello, I'm trying to write a script to export a txt file to excel. The text file is a delimited with commas Example: Report Date,12/12/2014 Report By, Person Issue, Job Failed I tried exporting as csv and mailed to my id. But if I open the csv file the data is coming in single... (6 Replies)
Discussion started by: annamalaikasi
6 Replies
All times are GMT -4. The time now is 08:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy