Exporting Excel to HTML

How can I export Excel file (all worksheets) to an HTML file from Delphi using Excel's COM? I want them to be saved in the same way as the "Save As" menu option does. It makes DHTML tabs for all worksheets in the output HTML file. Plz give me some working sample.

Who is Participating?
Try to record what you want to do as a macro and then do the same in Delphi. To get the value of constants, just assign them to a cell in a macro.

With Office 2003 the result is

    Workbooks.Open Filename:= _
        "D:\Documents\Borland Studio Projects\UseExcel\Book1.xls"
    ActiveWorkbook.SaveAs Filename:= _
        "D:\Documents\Borland Studio Projects\UseExcel\Book1.htm", FileFormat:=xlHtml _
        , ReadOnlyRecommended:=False, CreateBackup:=False

procedure TForm1.FormClick(Sender: TObject);
  ex: olevariant;
  ex := CreateOleObject('Excel.Application');
  ex.Workbooks.Open('D:\Documents\Borland Studio Projects\UseExcel\Book1.xls');
  ex.ActiveWorkbook.SaveAs(Filename := 'D:\Documents\Borland Studio Projects\UseExcel\test.htm',
      FileFormat := 44, ReadOnlyRecommended := False, CreateBackup := False);

It works for me exactly as you want. So, the only way it can fail for you is that your office is not 2003 so that your recorded macro would be different.

Try this:

  YourExcelApplication.ActiveWorkbook.SaveAs(FileName:= 'c:\ExcelExport.html', FileFormat := 44);

  - YourExcelApplication with the name of your variable
  - 'c:\ExcelExport.html' with the filename you want.

Regards, Geo
m_adilAuthor Commented:
but this will export only active worksheet. plz read the question again. I wrote, I want to export "all worksheets". So all worksheets that contain any data should be exported.

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Just try it. It will export the workbook not the current sheet only.

Regards, Geo
Eddie ShipmanAll-around developerCommented:
"but this will export only active worksheet"

You have to loop through the worksheets of the workbook and save each one as
HTML using code provided above.
Open the exported html file with InternetExplorer and you'll see the tabs at the bottom-left side of the page. If your current workbook contains one worksheet only then there won't be any tab there.
m_adilAuthor Commented:
yes it works perfect in office 2003. but what about office 2000? will it work with office 2000 in the same way?
You need to get to Office 2000 and record the macro there. I do not have Office 2000, so I cannot help. Most likely, it will work exactly as Save As invoked interactively, that is, if Office 2000 saves all worksheets, it will save them all too.

The  "ReadOnlyRecommended:=False, CreateBackup:=False" part recorder by Office 2003 looks optional, so if Office 2000 does not record that, you may drop it.
m_adilAuthor Commented:
it is working in office 2000 as well - thanks!

now how to grade points? I think both Geo and Octabun comeup with the same solution. how abt if i split and give 75% to Octabun and 25% to Geo as Octabun's code looks more comprehensive.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.