上面的博客幫了我很大忙,我希望這提供了一個完整的可測試解決方案。
爲了與上述複製粘貼保持一致, 我能夠採取上述建議並將公式複製到一個範圍,然後根據需要計算公式。整體思路是,
- 打開CSV文件中的每一列,以免失去YYYY-MM-DD和HH文本:MM:SS日期時間格式,並有列保持前導零在需要的地方。
- 插入一些列。
- 插入一個公式。
- 將公式複製到一個範圍內,從而顯示新的值。
- 只將值複製到另一個單元格。
- 關閉工作表,工作簿和Excel。
建議的示例數據是ASCII TXT CSV文件命名爲C:\ TEMP \ input.txt.csv
"Col1","Col2","Col3","Col4_is_a_string_of_at_least_32_characters"
"Row2","R2C2","R2C3","R2C4_is_a_string_leadz_0234_XYZ.zip"
"row3","r3c2","r3c3","r3c4_keep_leading_zero_0000_XYZ.zip"
代碼
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
### Set input and output path
$inputCSV = "C:\temp\input.txt.csv"
$outputXLSX_xlsx = "C:\temp\output.xlsx"
### Create a new Excel Workbook with one empty sheet
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
### Build the QueryTables.Add command
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
### Set the delimiter (, or ;) according to your regional settings
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
### Execute & delete the import query
$query.Refresh()
$query.Delete()
###
$xlShiftToRight = -4161
#$eCol=$worksheet.cells.item(1,4).entireCol
$eCol=$worksheet.Range("D1").EntireColumn
[void] $eCol.Insert($xlShiftToRight)
$eCol=$worksheet.Range("D1").EntireColumn
[void] $eCol.Insert($xlShiftToRight)
$eCol=$worksheet.Range("D1").EntireColumn
$eCol.NumberFormat = "General"
$eCol=$worksheet.Range("E1").EntireColumn
$eCol.NumberFormat = "General"
<# Get the number of rows and columns in the "Used Range" #>
$objRange=$worksheet.UsedRange
$rows=$objRange.Rows.Count
$cols=$objRange.Columns.Count
<# After inserting new columns, label the columns with headers and
## insert a formula to copy substring from cells to the right #>
$worksheet.Range("D1","D1").Value2 = "Ship"
$formula_ship = "=MID(RC[2],24,4)"
$worksheet.Cells.Item(2,4).Formula = "=MID(RC[2],24,4)"
$worksheet.Range("E1","E1").Value2 = "Station"
$formula_station = "=MID(RC[1],29,3)"
$worksheet.Cells.Item(2,5).Formula = "=MID(RC[1],29,3)"
## Copy the formula from D2:E2 to D3:E<rows> ##
[void] $worksheet.Range("D2","E2").Copy()
$paste_alpha="D3"
$paste_omega="E"+$rows.ToString()
$objRange=$worksheet.Range($paste_alpha,$paste_omega)
[void] $objRange.PasteSpecial(-4123)
## Copy the values calculated by the formula ##
$copy_alpha="D1"
$copy_omega="E"+$rows.ToString()
[void] $worksheet.Range($copy_alpha,$copy_omega).Copy()
$paste_alpha="H1"
$paste_omega="I"+$rows.ToString()
$objRange=$worksheet.Range($paste_alpha,$paste_omega)
[void] $objRange.PasteSpecial(-4163)
## Ensure the ship number format has leading zeros ##
$eCol=$worksheet.Range("D1").EntireColumn
$eCol.NumberFormat = "0000"
### Save & close the Workbook as XLSX. This may seem like overkill but without something kept a lock on the xlsx file.
$Workbook.SaveAs($outputXLSX_xlsx,51)
$excel.Application.Quit()
$a = Release-Ref($worksheet)
$a = Release-Ref($workbook)
$a = Release-Ref($excel)
嘗試沒有'.Value' - '$ objSheet.Cells.Item(1,3)=「嗨」' –
謝謝!但是,似乎這只是暫時的解決方案,只能在一種情況下起作用。如果我想與公式,字體,註釋等其他屬性一起工作,我無法確定背後的問題。 – msinfo
根據這個鏈接,應該沒有任何問題:http:// learn-powershell。net/2012/12/20/powershell-and-excel-adding-some-formatting-to-your-report/ –