This code was written for a web site where the optimal data source for the dynamic pages would be an Excel spreadsheet. This choice in data source, while perhaps not as flexible as a relational database, would allow the client to update their web site through an administrative web interface where the updated data set from the Excel spreadsheet can be readily uploaded to the site.
The following code supports multiple 'categories' of data sets across multiple Excel sheets and returns the sheet data in the hash key 'data' and the data fields in the hash key 'fields' - At present, this code assumes the data field names to reside in the first row of the Excel sheet with data fields aligned in columns.
The resultant hashref returned by this routine can readily be accessed by other modules such as Data::Dumper and XML::Simple - If there is sufficient interest, I can post some additional code showing how I have employed the returned hashref in my code.
sub _load_xls ($) {
my ($fname) = shift;
return undef unless (-e $fname);
my ($parse) = Spreadsheet::ParseExcel->new();
my ($excel) = $parse->Parse($fname);
my (%data);
for (my $sheetcount = 0; $sheetcount < $excel->{SheetCount}; $shee
+tcount++) {
my ($sheet) = $excel->{Worksheet}[$sheetcount];
my (@fields, @data);
for (my $field = $sheet->{MinCol}; defined $sheet->{MinCol} &&
+ $field <= $sheet->{MaxCol}; $field++) {
my ($cell) = $sheet->{Cells}[$sheet->{MinRow}][$field];
push (@fields, $cell->Value) if ($cell);
};
for (my $row = $sheet->{MinRow} + 1; defined $sheet->{MinRow}
+&& $row <= $sheet->{MaxRow}; $row++) {
my (%item);
for (my $col = $sheet->{MinCol}; defined $sheet->{MinCol}
+&& $col <= $sheet->{MaxCol}; $col++) {
my ($cell) = $sheet->{Cells}[$row][$col];
$item{$fields[$col - $sheet->{MinCol}]} = $cell->Value
+ if ($cell);
};
push (@data, \%item);
};
$data{$sheet->{Name}} = {
'fields' => \@fields,
'data' => \@data,
};
};
return (\%data);
};
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|