I have a text file that contains below data:
1,Name1,Age1,City1 2,Name2,Age2,City2 3,Name3,Age3,City3
I want this to import in an Excel file, for example, the starting cell will be "B1".
I have a script that can only update a single cell in the Excel file but I don't know what is the right approach if it will be some texts delimited by comma.
Edit: I forgot that I have a working code on this using VB but I need a help on converting it to Powershell.
Dim objExcel As object
Dim objSheet As object
Dim worksheet As string
Dim txt as string
Dim connectionName as string
worksheet = "Sheet1"
txt = "c:\test.txt"
objExcel = New Excel.Application
Dim objWB As Excel.Workbooks = objExcel.Workbooks
objWB.Open("c:\excelfile.xlsx",IgnoreReadOnlyRecommended:=True)
objSheet = objExcel.ActiveWorkbook.Worksheets(worksheet)
connectionName = "TEXT;" + txt
With objSheet.QueryTables.Add(Connection:=connectionName, Destination:=objSheet.Range("B1"))
.Name = txt
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = 0 'xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = 1 'xlDelimited
.TextFileTextQualifier = 1 'xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.Refresh (BackgroundQuery:=False)
End With
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Save()
objExcel.ActiveWorkbook.Close()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject (objWB)
objExcel.quit()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject (objExcel)
objWB = Nothing
objExcel = Nothing
System.Threading.Thread.Sleep (2000)
GC.Collect()