Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

QTP Script to convert content of excel into a html document

Suppose I have some data in excel file and I want to create a html table from the excel file content. Below code can create a html table based on the content of an excel file.We can understand how to create a html file using scripting.filesystemObject in QTP. 


Example of how to call the function:

func_createHTMLTableFromExcel "E:/test.xlsx","Sheet1","e:/ht.htm"

Code for the function:

Public Function func_createHTMLFromExcel(strDataFile,strWorksheetName,strHTMLFile)

Set objXlHandle = Createobject("Excel.Application")

 objXlHandle.visible = false

Set objExcelWB = objXlHandle.Workbooks.Open(strDataFile)

Set objExcelWS = objExcelWB.Worksheets(strWorksheetName)

'Getting column and row count 

 strColumnCount = objExcelWS.UsedRange.Columns.Count

 strTotRows = objExcelWS.UsedRange.Rows.Count

strTable = "<table border=""""2"""">"

'Creating a html table based on content of the excel file

For j=1 to strTotRows

strTable = strTable & "<tr>"

For i=1 to strColumnCount

 strData = Trim(objExcelWS.Cells(j,i))

 strTable= strTable & "<td>"& strData &"</td>"

Next

strTable =strTable & "</tr>"

Next

strTable = strTable & "</table>"

set objFSO=Createobject("scripting.FileSystemObject")

set objtxt = objFSO.createTextFile(strHTMLFile)

    objtxt.write(strTable)

'Closing the workbook

 objExcelWB.Close

 set objFSO =nothing

End Function

3 comments:

  1. I think you need to put .value at the end of this statement to retrieve the data from that cell - strData = Trim(objExcelWS.Cells(j,i))

    i.e. strData = Trim(objExcelWS.Cells(j,i).Value)

    ReplyDelete
  2. I savor, lead to I found just what I used to be taking a look for.
    You've ended my four day long hunt! God Bless you man. Have a
    nice day. Bye

    ReplyDelete
  3. Excellent post however I was wondering if you could write a litte more on this subject?
    I'd be very thankful if you could elaborate a little bit more.

    Thanks!

    ReplyDelete