huangning
2026-07-18 c8c5a62d62b9ad05cfa0b5e026496b56a735a18f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
$src = $args[0]
if (-not $src) { throw 'missing xlsx path' }
$sheetFile = 'sheet1.xml'
$tmp = Join-Path $env:TEMP ('crm_xlsx_' + [guid]::NewGuid())
New-Item -ItemType Directory -Path $tmp | Out-Null
$zip = Join-Path $tmp 'book.zip'
$dir = Join-Path $tmp 'unzipped'
Copy-Item -LiteralPath $src -Destination $zip
Expand-Archive -LiteralPath $zip -DestinationPath $dir -Force
 
[xml]$sstXml = Get-Content (Join-Path $dir 'xl\sharedStrings.xml') -Encoding UTF8
$shared = @()
foreach ($si in $sstXml.sst.si) {
  $nodes = $si.SelectNodes('.//*[local-name()="t"]')
  $texts = @()
  foreach ($n in $nodes) { $texts += $n.InnerText }
  $shared += ($texts -join '')
}
 
function Get-ColIndex($ref) {
  $letters = ($ref -replace '[0-9]', '')
  $n = 0
  foreach ($ch in $letters.ToCharArray()) {
    $n = $n * 26 + ([int][char]$ch - [int][char]'A' + 1)
  }
  return $n
}
 
function Get-CellText($cell) {
  if ($cell.t -eq 's') {
    $idx = [int]$cell.v
    if ($idx -ge 0 -and $idx -lt $shared.Count) { return $shared[$idx] }
    return ''
  }
  if ($cell.t -eq 'inlineStr') { return [string]$cell.is.t }
  return [string]$cell.v
}
 
[xml]$sx = Get-Content (Join-Path $dir "xl\worksheets\$sheetFile") -Encoding UTF8
foreach ($row in @($sx.worksheet.sheetData.row | Select-Object -First 12)) {
  $vals = New-Object string[] 29
  foreach ($cell in $row.c) {
    $i = (Get-ColIndex $cell.r) - 1
    if ($i -ge 0 -and $i -lt 29) { $vals[$i] = (Get-CellText $cell) }
  }
  Write-Output (($vals | ForEach-Object { if ($_) { $_ } else { '' } }) -join "`t")
}
 
Remove-Item -LiteralPath $tmp -Recurse -Force