How to Use CGI to Display an XLS File in a Web Browser

Written by cathlene s. baptista
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use CGI to Display an XLS File in a Web Browser
The Spreadsheet::ParseExcel module allows web pages to display Excel files. (number background image by kuhar from Fotolia.com)

Common gateway interface (CGI) provides web developers with many ways to make websites both dynamic and interactive. CGI is not a programming language in itself, but rather is an interface between web servers and other programs, such as those written in Perl. Perl is the de facto interpreted language used in CGI programs and offers an extensive module library. Using these modules, developers can extend their Perl websites considerably, taking advantage of other applications such as Microsoft Excel.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Web browser
  • Text editor
  • Web server with Perl installed
  • Spreadsheet::ParseExcel Perl module
  • Microsoft Excel

Show MoreHide

Instructions

  1. 1

    Open a text editor and create a text file. Add the path to Perl in the first line and indicate that you want to use the Spreadsheet::ParseExcel package in the file. Save the text file on the web server as excel.pl.

    !/usr/bin/perl

    use Spreadsheet::ParseExcel;

  2. 2

    Open Excel and create a new file that will contain the contents to show in the web page. Enter the text "Cell 1 Contents" in the first cell of the first worksheet and save the file with the name excel_contents.xls. Save the file on the server in the same location as excel.pl.

  3. 3

    Open excel.pl and create a new parser object using the new() method. This method creates a Spreadsheet parser object that will be used to display the cells parsed from excel_contents.xls. Use the parse() method to create a new Workbook object and indicate that the Workbook object should be created using the file excel_contents.xls.

    !/usr/bin/perl

    use Spreadsheet::ParseExcel;

    my $parser = Spreadsheet::ParseExcel->new();

    my $workbook = $parser->parse('excel_contents.xls');

  4. 4

    Continue editing excel.pl. Create a new worksheet using the $workbook->$worksheet() method and assign it to a variable named $worksheet. The $workbook->$worksheet() method can use a name or an index. Use index '0' to indicate the first worksheet in the excel_contents.xls file.

    !/usr/bin/perl

    use Spreadsheet::ParseExcel;

    my $parser = Spreadsheet::ParseExcel->new();

    my $workbook = $parser->parse('excel_contents.xls');

    $worksheet = $workbook->worksheet(0);

  5. 5

    Continue editing excel.pl. Use the worksheet's get_cell() method to indicate that the cell at row index 0 and column index 0 will be used. Assign the cell to a variable named $cell.

    !/usr/bin/perl

    use Spreadsheet::ParseExcel;

    my $parser = Spreadsheet::ParseExcel->new();

    my $workbook = $parser->parse('excel_contents.xls');

    $worksheet = $workbook->worksheet(0);

    $cell = $worksheet->get_cell(0,0);

  6. 6

    Continue editing excel.pl. Use the print command to output the headers to the web page. Print the value of the $cell variable, which is the spreadsheet value stored in row index 0 and column index 0. Save excel.pl to the server.

    !/usr/bin/perl

    use Spreadsheet::ParseExcel;

    my $parser = Spreadsheet::ParseExcel->new();

    my $workbook = $parser->parse('excel_contents.xls');

    $worksheet = $workbook->worksheet(0);

    $cell = $worksheet->get_cell(0,0);

    print "Content-type: text/html\n\n";

    print $cell->value();

  7. 7

    Open excel.pl in a web browser. The contents of the first cell in the file excel_contents.xls will be displayed in the web browser.

Tips and warnings

  • For specific instructions on installing the Spreadsheet::ParseExcel module, refer to the Resources, or try using a web search engine. There are many resources available on the web to help with Perl module installation.
  • Using the CPAN to install Perl modules ensures all dependencies are in place when extending Perl.
  • Win32:OLE is another way to integrate Excel files in a web page when using the Windows platform.
  • Try the Spreadsheet::WriteExcel module to alter data from a spreadsheet and write it to a new Excel file.
  • The Spreadsheet::ParseExcel cannot read files in the Excel 2007 Open XML XLSX format. Refer to the Spreadsheet::XLSX module for this type of file.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.