You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell中将单条查询结果拆分导出为多个CSV文件

PowerShell拆分超百万行交易记录方案

默认你已将170万条查询结果导出为带表头的CSV格式文件,本方案会自动保留表头到每个拆分文件,拆分后单文件数据行数最大为999999,加上表头刚好100万行,符合Excel单表行数限制。

可直接运行的脚本

# 请修改以下三个参数为你自己的实际路径/配置
$sourceCsvPath = "C:\Users\XXX\Desktop\原始交易记录.csv"
$outputFolderPath = "C:\Users\XXX\Desktop\拆分结果"
$maxDataRowsPerFile = 999999

# 读取原始文件全部数据
$allRecords = Import-Csv -Path $sourceCsvPath -Encoding UTF8
$totalCount = $allRecords.Count
$currentStartIndex = 0
$fileSerialNumber = 1

# 循环拆分写入
while ($currentStartIndex -lt $totalCount) {
    $currentEndIndex = [Math]::Min($currentStartIndex + $maxDataRowsPerFile - 1, $totalCount - 1)
    $outputFilePath = Join-Path $outputFolderPath ("交易记录_拆分{0}.csv" -f $fileSerialNumber)
    # 截取当前批次数据
    $currentBatch = $allRecords[$currentStartIndex..$currentEndIndex]
    # 导出为CSV,自动保留表头
    $currentBatch | Export-Csv -Path $outputFilePath -Encoding UTF8 -NoTypeInformation
    Write-Host "已生成拆分文件:$outputFilePath ,包含数据行数:$($currentBatch.Count)"
    $currentStartIndex = $currentEndIndex + 1
    $fileSerialNumber++
}

操作步骤

  • 提前将你的170万条查询结果导出为UTF-8编码的CSV格式文件,避免后续打开乱码
  • 修改脚本前三个参数的取值,确保$outputFolderPath对应的文件夹已经提前创建
  • 按下Win+X选择「Windows PowerShell」,粘贴修改后的脚本执行即可
  • 170万条数据场景下会自动生成2个拆分文件,都可直接用Excel正常打开

补充说明

如果需要直接导出为xlsx格式而非CSV,可先执行Install-Module -Name ImportExcel -Scope CurrentUser安装官方开源Excel处理模块,将脚本中的Export-Csv替换为Export-Excel即可,参数逻辑完全一致。
如果导出的CSV用Excel打开出现乱码,可将脚本中的-Encoding UTF8替换为-Encoding Default适配你系统的默认编码。

内容的提问来源于stack exchange,提问作者Mano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 15:57:04