Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Rendering Clean MS-Excel Spreadsheets to a Browser

by emarsee (Acolyte)
on Jul 02, 2003 at 17:41 UTC ( [id://270886]=CUFP: print w/replies, xml ) Need Help??

I use the script below, which uses the excellent Spreadsheet::ParseExcel package (available through ppm) on my web server to render Excel spreadsheets to my intranet. It does a much better job than MS-Office, because it produces clean HTML, and can be customised heavily to effectively use the spreadsheets as the database to drive customised web pages.

The parameters to the script are excelFile=xx.xls, where xx.xls resides in the c:/inetpub/wwwroot/ directory on an IIS server.

#!d:\perl\bin\perl.exe use strict; use Time::gmtime; use CGI qw(:standard); use Spreadsheet::ParseExcel; my $excelFile='c:/inetpub/wwwroot/' . param('excelFile'); my $title=$excelFile; my $oExcel = new Spreadsheet::ParseExcel; my $oBook = $oExcel->Parse($excelFile); my($iR, $iC, $oWkS, $oWkC); printHeader(); printBody(); sub printSpreadsheet() { my $startpage = 0; print "<h1>FILE :", $oBook->{File} , "<h1>\n"; for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++) { $oWkS = $oBook->{Worksheet}[$iSheet]; if($oBook->{SheetCount} gt 1) { print h1($oWkS->{Name}); } print "<table border=0 cellpadding=1 width=100% "; print "BGCOLOR='#808080' width=100%>\n"; for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) + { print "<tr>\n"; for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++) { $oWkC = $oWkS->{Cells}[$iR][$iC]; if($oWkC) { print "<td bgcolor=\'"; print $oExcel->ColorIdxToRGB($oWkC->{Format}->{Fil +l}[1]),"\'>\n"; if ($iR eq 0 or $iC eq 0) { print "<b>"; } if ($oWkC->Value ne '') { print toHtml($oWkC->Value), "\n"; } else { print "&nbsp;"; } if($iC == 2 && $iR gt 1) { print "</a>"; } print "</td>\n"; } else { print "<td bgcolor='#C9C7BA'>&nbsp;</td>\n"; } } print "</tr>\n"; } print "</table>\n"; } } ######################################################### sub printHeader { print "Content-type:text/html\n\n"; print "<html><head>\n"; print "<title>", $title, "</title>"; print "<LINK rel='stylesheet' type='text/css' "; print "href='http://localhost/styles.css'>\n"; print "<SCRIPT language=JavaScript>\n"; print " function linkURL(thisURL)\n"; print " { \n"; print " var re = / /g;\n"; print " var newThisURL = thisURL.replace(re, \"%20\"); \n"; print " window.location =newThisURL; \n"; print " }\n"; print "</SCRIPT>\n"; print "</head>\n"; print "<body bgcolor='#FFFFFF'>\n"; } ######################################################### sub printBody() { printSpreadsheet(); print "x</body>\n"; print "</html>\n"; } ######################################################### sub toHtml() { local ($_) = @_; s/&/&amp;/g; s/>/&gt;/g; s/</&lt;/g; # etc... return $_; }

Replies are listed 'Best First'.
(jeffa) Re: Rendering Clean MS-Excel Spreadsheets to a Browser
by jeffa (Bishop) on Jul 03, 2003 at 15:01 UTC
    That's not the cleanest code i have seen (no strict, variables that are used without first being initialized), but it's a decent first attempt, and the results look really good. Here is a more consise approach - i'll skip trying to format the output to look like Excel's, but i think you will appreciate the 'lack of code' ;)
    use strict; use warnings; use CGI qw(:standard); use DBIx::XHTML_Table; my $file = '/path/to/foo.xls'; my $dbh = DBI->connect("DBI:Excel:file=$file",undef,undef,{RaiseErro +r=>1}); my $table = DBIx::XHTML_Table->new($dbh); my $sheet = ($dbh->tables)[0]; $table->exec_query("select * from $sheet"); print header, $table->output;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Rendering Clean MS-Excel Spreadsheets to a Browser
by jmcnamara (Monsignor) on Jul 03, 2003 at 16:26 UTC

    For anyone who is interested, it is also worth having a look at the xlHtml Excel to Html converter.

    There is even a CPAN module that interfaces with it: Spreadsheet::ParseExcel_XLHTML

    --
    John.

Re: Rendering Clean MS-Excel Spreadsheets to a Browser
by bart (Canon) on Jul 08, 2003 at 15:16 UTC
    print $oWkC->Value, "\n";
    Tsk. You haven't html-escaped the values. What if your text contains one of the no-no characters, in particular "<" and "&"?

    When producing HTML, always escape the text. Always.

      Done - there's also the matter of replacing \013 linefeeds with html breaks, but I've skipped that for now.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found