答案1
我认为最简单的方法是使用 Excel 中的 VBA 宏:
Sub CleanFiles()
' for each file in a directory
' open it, delete all cell content around the first 20x25 cells, write it back
' 2015-10-15
Dim path As String, aFile As String
Dim arr As Variant
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
path = "D:\TEMP\xls\" ' must have trailing backslash
aFile = Dir(path & "*.xls")
Do While aFile <> ""
Application.StatusBar = aFile
aFile = path & aFile
Workbooks.Open (aFile)
' process file content
arr = ActiveSheet.Range("A1:Y20")
Cells.Clear
ActiveSheet.Range("A1:Y20") = arr
' save and close WB
ActiveWorkbook.Close True, aFile
aFile = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = False
Application.StatusBar = False
End Sub
首先,禁用屏幕更新和自动宏。然后,扫描特定文件夹中的“*.xls”文件并打开每个文件。将要保留的区域的内容保存在内部,然后清除整个工作表并将内容写回到工作表中。关闭文件时,无需提示即可保存。
答案2
Get-Content file.csv | select -First 25 | Export-Csv newfile.csv
如果您指的是前 20-25 行,则可以执行 a 。您可以Get-Content -Header @("a","b","c") file.csv | Select a,b,c | Export-Csv newfile.csv
按此顺序执行 a 等,以一直选择到 y,即第 25 列。不过,也许有更好的方法。