在PowerShell中如何读取excel文件

如何在windows中使用代码读取excel文件

$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open( $excelfile ) # 必须指定$excelfile
$workbook.Sheets | ForEach-Object {
	if ( ![string]::IsNullOrEmpty( $_.Range( "A1" ).Text ) ) { # 跳过A1为空的sheet页
		Write-Host $_.Name # sheet name
		Write-Host ( "=" * $_.Name.Length ) # underline sheet name
		Write-Host
		for ( $row = 1; $row -le $_.UsedRange.Rows.Count; $row++ ) { # iterate through all rows in used range
			for ( $column = 1; $column -le $_.UsedRange.Columns.Count ; $column++ ) { # iterate through all columns in used range
				Write-Host ( "({0}{1})`t{2}" -f ( [char] ( $column + 64 ) ), $row, $_.Columns.Item( $column ).Rows.Item( $row ).Text ) # e.g. '(A4) content_of_cell_A4'
			}
			Write-Host # 在各行间输出空行
		}
	}
}
[void] $workbook.Close( ) # 关闭 Excel 文件
[void] $objExcel.Quit( ) # 应关闭Excel应用程序,但不一定总是能关掉
日期:2020-04-11 23:04:34 来源:oir作者:oir