jcc2007 has asked for the wisdom of the Perl Monks concerning the following question:

Could anyone tell me what is the best way to save an Excelsheet to a different format? I've tried using the Win32::OLE module and also have tried to use Spreadsheet-WriteExcel, SpreadSheet-WriteExcelXML. We are basically trying to save it to HTML so that the results can be displayed on a Web page.

One of the things that did work was saving the Excelsheet in XML, but we are having an issue when trying to view the Excelsheet on the web page the display is not quiet right. We are missing I believe the traslator between the Web interface for IIS 6.0 and XML. Is anyone familiar with what is needed.

First of all thank you all for the responses. I will like to clarify my questions:

1. We get flat file which we create an Excelsheet.
2. The data needs to be displayed on an IIS 6 Server.
We would like to take the Excel file we generate from the flat file, to HTML format.
something like this:

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
# application or open new
my $Book = $Excel->Workbooks->Open("C:\\\\Copy of test.xls"); # open Excel file
my $Sheet = $Book->Worksheets(1);
$Book->SaveAs('c:\\t.htm','FileFormat:=xlHtml');
$Book->Close;

Thanks.

Replies are listed 'Best First'.
Re: Converting EXCEL Workbooks to HTML
by mr_mischief (Monsignor) on Aug 29, 2007 at 21:08 UTC
    If you're going from Excel to something else, you don't really need Spreadsheet::WriteExcel. Have you taken a look at Spreadsheet::ParseExcel? That will let you get at the data from the Excel spreadsheet, which you can then write out some other way. It's a very useful module for when you need to start with the data in Excel and use it somewhere else.

    BTW, there are also Spreadsheet::BasicRead, Spreadsheet::TieExcel, and some others, but I have never tried any of those and can't comment much on them. Perhaps some other monks have more information on those.

      First of all thank you for the response. I will like to clarify my questions:

      1. We get flat file which we create an Excelsheet.
      2. The data needs to be displayed on an IIS 6 Server.
      We would like to take the Excel file we generate from the flat file, to HTML format.
      something like this:

      use strict;
      use Win32::OLE qw(in with);
      use Win32::OLE::Const 'Microsoft Excel';
      $Win32::OLE::Warn = 3; # die on errors...
      my $Excel = Win32::OLE->GetActiveObject
      ('Excel.Application') || Win32::OLE->new
      ('Excel.Application', 'Quit');
      # application or open new
      my $Book = $Excel->Workbooks->Open("C:\\\\Copy of test.xls"); # open Excel file
      my $Sheet = $Book->Worksheets(1);
      $Book->SaveAs('c:\\t.htm','FileFormat:=xlHtml');
      $Book->Close;

      Thanks.

Re: Converting EXCEL Workbooks to HTML
by SheridanCat (Pilgrim) on Aug 29, 2007 at 21:13 UTC
    You can use Spreadsheet::ParseExcel to read the data from the spreadsheet and write out HTML manually or by using some useful templating system such as Template::Toolkit.

    You'll also end up with more manageable HTML that way, I believe.

Re: Converting EXCEL Workbooks to HTML
by b4swine (Pilgrim) on Aug 30, 2007 at 00:59 UTC
    I am not sure what your question is about. Your middle para seems to suggest that you simply want to open the file in Excel and the use the File-Save As menu command to save as HTML or XML. If so, then this has nothing to do with perl, and it has worked for me, even for quite complex worksheets. Actually I used File-Save for Web-HTML format. If as you say,

    ...but we are having an issue when trying to view the Excelsheet on the web page the display is not quiet right

    you need to explain what doesn't display quite right.

    If on the other hand, there is some perl involved here, please explain once again, exactly how what would you like done, and how?

Re: Converting EXCEL Workbooks to HTML
by punch_card_don (Curate) on Aug 30, 2007 at 01:06 UTC
    Sometimes the simplest assumption is the right one;

    If you're not yet looking at it on a web page, then you're looking at it in Excel on a PC? If that's the case, then all versions of Excel for some years have a "Save as..." option that will allow you to save your file directly to HTML.




    Forget that fear of gravity,
    Get a little savagery in your life.
Re: Converting EXCEL Workbooks to HTML
by technojosh (Priest) on Aug 29, 2007 at 21:10 UTC
    As far as helping with this, I have no idea how exactly you are displaying your data to the browser without some idea of the code...

    It shouldn't be a major issue to format the data you get from an excel file to print to the browser. You are getting the data from the file correctly (I assume, since I have not seen your code) with Win32::OLE. So it's just a matter of formatting that, and displaying it correctly.

Re: Converting EXCEL Workbooks to HTML
by Your Mother (Archbishop) on Aug 30, 2007 at 03:51 UTC

    Not a recommendation but a working example of an approach.

    use strict; use warnings; use Spreadsheet::ParseExcel; my $file = shift || die "Give me an Excel file!\n"; -e $file and -r _ or die "Must provide valid Excel file! $file, $!\n"; my $excel_obj = Spreadsheet::ParseExcel->new(); my $workbook = $excel_obj->Parse($file); die "Workbook did not return worksheets!\n" unless ref $workbook->{Worksheet} eq 'ARRAY'; for my $worksheet ( @{$workbook->{Worksheet}} ) { print "<table>\n"; for my $row ( 0 .. $worksheet->{MaxRow} ) { print "<tr>\n"; for my $col ( 0 .. $worksheet->{MaxCol} ) { my $cell = $worksheet->{Cells}[$row][$col]; print "<td>"; print ref $cell ? $cell->Value : ''; print "</td>\n"; } print "</tr>\n"; # record ends } print "</table>\n"; # worksheet ends }