Thread Rating:
  • 4 Vote(s) - 2.75 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whether xlsx extension works while importing data sheet?
#18
Solved: 11 Years, 3 Months, 2 Weeks ago
I required this for my testing today, so here is a fully functional XLSX importer

Disclaimer: This does not format or cleanup the Excel sheets themselves, just renames them for import so they are CamelCase. If you want your columns in the datasheets to be CamelCase with no special characters you will need to do some further extension.

Code:
'Usage for XLS or XLSX:
Dim filePath
filePath = "C:\File\Path\Goes\Here\FileName.xlsx"
importAnyXL filePath

Function importAnyXL(filePath)
   Dim xl, xlFile 'Objects
   Dim FileFormatNum, iSheetCount, a, i 'Integers
   Dim newFilePath, sheetName, cleanSheetName 'Strings
   Dim fileDelete 'Boolean
   Dim sheetList()  'Array
        Set xl = CreateObject("Excel.Application")
        xl.DisplayAlerts = False
        Set xlFile = xl.Workbooks.Open(filePath)
        iSheetCount = xlFile.Sheets.Count
        For a = 0 To (iSheetCount - 1)
            ReDim Preserve sheetList(a)
            sheetList(a) = xlFile.Sheets(a + 1).Name
        Next
        If UCase(Right(Trim(filePath), 4)) = "XLSX" Or UCase(Right(Trim(filePath), 4)) = "XLSM" Then 'Dont forget to cater for macro enabled files ;)
            FileFormatNum = -4143
            filePath = Left(filePath, Len(filePath) - 5) & "_Temp.xls"
            xlFile.SaveAs filePath, FileFormatNum
            fileDelete = True
        End If
        xlFile.Close False
        xl.DisplayAlerts = True
        xl.Quit
        Set xl = Nothing
        For Each sheetName In sheetList
            cleanSheetName = camelCase(sheetName)
            DataTable.AddSheet camelCase(sheetName)
            DataTable.ImportSheet filePath, sheetName, cleanSheetName
        Next
        'Clean-up after yourself :)
        If fileDelete Then
            Dim FSO
            Set FSO = CreateObject("Scripting.FileSystemObject")
            If FSO.FileExists(filePath) Then
               FSO.DeleteFile filePath
            End If
            Set FSO = Nothing
        End If
End Function

And for a Bonus Cool
Code:
Function camelCase(strIn)
   Dim strOut, boolUp, i, c, tc
    strOut = ""
    boolUp = True
        For i = 1 To Len(strIn)
            c = Mid(strIn, i, 1)
            If c = " " Or c = "'" Or c = "-" Then
                strOut = strOut & c
                boolUp = True
            Else
                If boolUp Then
                    tc = UCase(c)
                Else
                    tc = LCase(c)
                End If
                strOut = strOut & tc
                boolUp = False
            End If
        Next
    camelCase = Replace(Replace(strOut, " ", ""), ".*", "")
End Function
Reply


Messages In This Thread
RE: Importing data sheet format? - by Akhila - 09-30-2008, 06:26 AM
RE: Whether xlsx extension works while importing data sheet? - by cflow - 07-25-2012, 01:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel sheet serenediva 1 9,597 03-03-2017, 10:07 AM
Last Post: vinod123
  Chrome Extension UFT Agent for Chrome Browser testing with UFT santhosh kumar 2 12,439 04-20-2016, 01:20 AM
Last Post: santhosh kumar
Question QTP not performing iterations based on local data sheet Breaker 18 56,673 01-25-2016, 04:32 PM
Last Post: arunshuklainbox
  How QTP record/play works on google chrome shobi 6 7,541 01-11-2016, 09:53 AM
Last Post: vinod123
  Importing data sheet Santu 2 3,377 08-20-2015, 11:37 PM
Last Post: ADITI1992

Forum Jump:


Users browsing this thread: 5 Guest(s)