[英]Import Text file data to Excel
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". 我希望将其导入Excel文件,例如,起始单元格为“ 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. 我有一个只能更新Excel文件中单个单元格的脚本,但是我不知道如果将某些文本用逗号分隔的正确方法是什么。
Edit: I forgot that I have a working code on this using VB but I need a help on converting it to Powershell. 编辑:我忘记了我在使用VB上有一个有效的代码,但是我需要将其转换为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()
I don't know what is the right approach if it will be some texts delimited by comma.
如果某些文本用逗号分隔,我不知道什么是正确的方法。
Such a text file format is commonly known as CSV ( C omma- S eparated V alues). 这样的文本文件格式通常被称为CSV(C omma-šeparated V alues)。
Fortunately, PowerShell comes with an arsenal of CSV parsing tools, including Import-CSV
: 幸运的是,PowerShell附带了许多CSV解析工具,包括
Import-CSV
:
$Lines = Import-Csv C:\my\textfile.txt -Header 'ID','Name','Age','City'
Now, $Lines
is an array of objects representing each row in the text file. 现在,
$Lines
是一个对象数组,代表文本文件中的每一行。 Each object will have properties named ID
, Name
, Age
and City
that you can use to grab the different values: 每个对象将具有名为
ID
, Name
, Age
和City
属性,您可以使用它们来获取不同的值:
foreach($Line in $Lines)
{
'{0} from {1} is {2} years old' -f $Line.Name,$Line.City,$Line.Age
}
Now all you need to do is substitute the foreach
loop with a for
loop and start populating your excel worksheet! 现在,您所需要做的就是用
for
循环替换foreach
循环,然后开始填充excel工作表!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.