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
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))
ReplyDeletei.e. strData = Trim(objExcelWS.Cells(j,i).Value)
I savor, lead to I found just what I used to be taking a look for.
ReplyDeleteYou've ended my four day long hunt! God Bless you man. Have a
nice day. Bye
Excellent post however I was wondering if you could write a litte more on this subject?
ReplyDeleteI'd be very thankful if you could elaborate a little bit more.
Thanks!