'************************************************************
Method1
'************************************************************
If the CSV file is having data in below style then use this method.
'**************************************
UserName, EmailId, PhoneNumber 'Treating this row as Columns
sudhakar, ksrbalu@gmail.com, 1234567890
balu, ksr_balu@yahoo.com, 9999999999
 
'**************************************
UserName, EmailId, PhoneNumber 'Treating this row as Columns
sudhakar, ksrbalu@gmail.com, 1234567890
balu, ksr_balu@yahoo.com, 9999999999
'**************************************
'************************************************************Function ImportCsvFiletoDatatable(CsvFilePath,SheetName,HeaderDelimiter)Dim filePathDim fsoDim fDim fDataDim arrDataDim CsvValueDim CsvSheetDim CsvFirstLineDim CsvColumnsDim ColumnIndexDim rIndexDim cIndexfilePath=CsvFilePath    'Specify file Path'Open CSV File using File System ObjectSet fso=createobject("scripting.filesystemobject")
Set f  = fso.OpenTextFile(filePath)CsvFirstLine=f.readline    'Treating like first line is the column namesCsvColumns=split(CsvFirstLine,HeaderDelimiter)    'Split the line using HeaderDelimiterSet CsvSheet=DataTable.GetSheet(SheetName) 'Get the Specified sheet
'Add the splitted values as Datatable ColumnsFor ColumnIndex=lbound(CsvColumns) to ubound(CsvColumns)
CsvSheet.addparameter CsvColumns(ColumnIndex),""NextWhile not f.AtEndOfStream
rIndex=f.Line-1    'Specify Row indexfData=f.ReadLine    ' Read CSV File LinearrData=split(fData,",") 'Split the line
cIndex=1    'Specify Column IndexCsvSheet.SetCurrentRow(rIndex)    'Set Row in the Datatable' Add values in DatatableFor Each CsvValue In arrData
CsvSheet.getparameter(cIndex).value=CsvValue
cIndex=cIndex+1
NextWendf.Close
Set fso=Nothing
End Function
'************************************************************'Calling this FunctionImportCsvFiletoDatatable "C:\Sample.csv","Action1",","
'************************************************************
Method2
'************************************************************
If the CSV file is having data in below style then use this method.
'**************************************
UserName: sudhakar,balu
'**************************************
UserName: sudhakar,balu
EmailId:ksrbalu@gmail.com, ksr_balu@yahoo.com
PhoneNumber: 1234567890, 9999999999
'**************************************
'************************************************************Function ImportCsvFiletoDatatable(CsvFilePath,SheetName,HeaderDelimiter)Dim filePathDim fsoDim fDim fDataDim arrDataDim CsvValueDim CsvSheetDim CsvFirstLineDim CsvColumnsDim ColumnIndexDim rIndexDim cIndexfilePath=CsvFilePath    'Specify file Path'Open CSV File using File System ObjectSet fso=createobject("scripting.filesystemobject")
Set f  = fso.OpenTextFile(filePath)Set CsvSheet=DataTable.GetSheet(SheetName) 'Get Sheet based on the name
While not f.AtEndOfStream
CsvLine=f.readline
arrData=split(CsvLine,HeaderDelimiter,2)    'Split the lineSet CsvParameter=CsvSheet.addparameter(arrData(0),"") 'Adding first word as datatable column
For CsvArrIndex=1 to ubound(arrData)
CsvData=split(arrData(CsvArrIndex),",")rIndex=1
For Each CsvValue In CsvData
CsvSheet.setcurrentrow(rIndex)
CsvParameter.value=CsvValue    'Adding data to the datatable columnrIndex=rIndex+1
NextNextWendf.Close
Set fso=Nothing
End Function
'************************************************************'Calling this FunctionImportCsvFiletoDatatable "C:\Sample.csv","Action1",":"
 
Hi
ReplyDeleteBelow details having CSV file, Can you please write code for those Requirement?
EMP Emp Name
12345 Rahul
12346 Kiran
12345 Kiran
12345 Kiran
12346 Rahul
12347 Sudhakar
12347 Sudhakar
12345 Kiran
IN CSV file, EMP number is duplicate values, i dont want use duplicate values. Excuate the Unique values
Cool. Worked like magic:)
ReplyDeleteVery nice. Works great except my csv data has comma imbedded in a field so when i do a split(data, ",") things get all messed up.
ReplyDeleteoh well. thanks.
Very nice.
ReplyDeleteThanks
ReplyDeleteIt is possble to Add the new column in medle of the data table after import the csv file.
ReplyDelete