Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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); };

In reply to Excel Spreadsheet Data Set by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-24 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found