Friday, May 23, 2008

Publishing an Excel table as static html using jscript.

Oh man! I have worked on it a few days, posted numerous questions on various newsgroups, spent hours on the Internet and in a book store, and finaly I got what I needed!

I have a sheet (Sheet1) in my xlsx document (Excel 2007) which I wanted to save as a static html page using.
I want to use jscript run in the command line, so I could automate the process.
I don't want to use SaveAs( ..., 44 ) method because it creates the sheet name at the bottom.
I also don't want to have the ActiveX embedded into the page (no dynamic html.)

Assuming that the document is c:\mydoc\test.xlsx, I want to generate c:\mydoc\test.html file.

Here is the code of doit.js file:

ea = new ActiveXObject( "Excel.Application" ) ;
ea.DisplayAlerts = 0 ; /* Don't want to see any questions on the screen! */
wb = ea.Workbooks.Open( "c:\\mydoc\\test.xlsx" ) ;
po = wb.PublishObjects ;
po.Add( 1, "c:\\mydoc\\test.html", "Sheet1" ) ; /* 1 == static html. */
po.Item( 1 ).Publish( true ) ; /* Yahoo! */
wb.Close( false ) ; /* I don't want to save the newly added PublishObject */

Isn't that cool!

No comments: