# 提取 Excel 文件中的文本内容 $envPath = "c:\work\data\env.xlsx" Write-Output "提取 Excel 文件中的文本信息..." Write-Output "文件: $envPath`n" $bytes = [System.IO.File]::ReadAllBytes($envPath) # 尝试读取为文本,查找可打印字符 $sb = New-Object System.Text.StringBuilder(10000) foreach ($byte in $bytes) { if ($byte -band 0x3F -ge 32) { # 可打印 ASCII [void]$sb.Append([char]$byte) } elseif ([char]$byte -in " -~") { # 可打印字符 [void]$sb.Append([char]$byte) } } $text = $sb.ToString() Write-Output "文件预览(前3000个字符):" Write-Output $text.Substring(0, [Math]::Min(3000, $text.Length))) # 查找 Git 相关信息 Write-Output "`n`n搜索 Git 相关信息:" $gitPatterns = @( "github\.com", "git@github\.com", "https?://", "branch:", "master", "main", "origin" ) foreach ($pattern in $gitPatterns) { $matches = [regex]::Matches($text, $pattern, [regex]::Options(!, [regex]::IgnoreCase)) Write-Output "`n找到 '$pattern' - $($matches.Count) 个匹配:" if ($matches.Count -le 5) { $matches | ForEach-Object { Write-Output " $($_.Value)`" } else { Write-Output " 匹太多了,只显示前 3 个:" $matches | Select-Object -First 3 | ForEach-Object { Write-Output " $($_.Value)`" } Write-Output " ... 共 $($matches.Count) 个" } } Write-Output "`n`n尝试查找 URL:" $urlPattern = "https?://[^\s<>`"]+[a-zA-Z0-9._/-]{10,}" $urls = [regex]::Matches($text, $urlPattern, [regex]::Options(!, [regex]::IgnoreCase)) if ($urls.Count -gt 0) { Write-Output "找到的 URLs:" $urls | Select-Object -First 10 | ForEach-Object { Write-Output " $($_.Value)`" if ($urls.Count -gt 10) { Write-Output " ... 共 $($urls.Count) 个 URL" } } else { Write-Output "未找到 URL" }