Inbody数据处理

AuWay
发布于 2025-02-27 / 31 阅读
0
0

Inbody数据处理

对Inbody下机原始数据进行预处理,增加数据处理效率。

下面是具体的代码信息:

# Set console encoding to UTF-8 to prevent character corruption
chcp 65001 | Out-Null
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# Import Windows.Forms to support file selection dialog
Add-Type -AssemblyName System.Windows.Forms

# Open file selection dialog to choose an Excel file
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Filter = "Excel Files (*.xlsx;*.xls)|*.xlsx;*.xls"
$openFileDialog.Title = "Select an Excel file to process"

if ($openFileDialog.ShowDialog() -ne [System.Windows.Forms.DialogResult]::OK) {
    Write-Host "No file selected. Script exiting."
    exit
}

# Get input file path
$excelPath = $openFileDialog.FileName
$excelDir = [System.IO.Path]::GetDirectoryName($excelPath)   # Get directory of the input file
$excelName = [System.IO.Path]::GetFileNameWithoutExtension($excelPath)  # Get filename (without extension)

# Generate output file path in the same directory with a modified name
$outputPath = Join-Path $excelDir "$excelName`_processed.xlsx"

# Force terminate all Excel processes to prevent file lock
Get-Process -Name Excel -ErrorAction SilentlyContinue | Stop-Process -Force

# Initialize Excel COM object
try {
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $false
    $excel.DisplayAlerts = $false

    # Open source workbook
    $workbook = $excel.Workbooks.Open($excelPath)
    $sheet = $workbook.Sheets.Item(1)

    # Create a new workbook
    $newWorkbook = $excel.Workbooks.Add()
    $newSheet = $newWorkbook.Sheets.Item(1)

    # Column operation configuration
    $columnsOrder = @(1,2,3,4,5,17,23,26,32,39,50,55,64,60,56,57,58,59)
    $afterColumns = @(2,5,17,23,26,32)
    $beforeColumns = @(23,26)
    $newColIndex = 1

    foreach ($colIndex in $columnsOrder) {
        # Insert empty column before
        if ($beforeColumns -contains $colIndex) {
            $newSheet.Columns($newColIndex).Insert()
            $newColIndex++
        }

        # Copy data column
        $sheet.Columns($colIndex).Copy()
        $newSheet.Activate()
        $newSheet.Columns($newColIndex).PasteSpecial(-4163)  # xlPasteValues
        $newColIndex++

        # Insert empty column after
        if ($afterColumns -contains $colIndex) {
            $newSheet.Columns($newColIndex).Insert()
            $newColIndex++
        }
    }

    # Save and exit
    $workbook.Close($false)
    $newWorkbook.SaveAs($outputPath)
    Write-Host "Processing complete. File saved at: $outputPath"
}
catch {
    Write-Host ("Operation failed: " + $_.Exception.Message) -ForegroundColor Red
}
finally {
    # Close Excel objects
    if ($newWorkbook -ne $null) { $newWorkbook.Close() }
    if ($workbook -ne $null) { $workbook.Close() }
    if ($excel -ne $null) { $excel.Quit() }

    # Release COM objects
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($newSheet) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($newWorkbook) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

    # Force garbage collection
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
}
# 上述代码运行时可以弹出对话框,选择原始inbody下机数据进行处理,处理后的文件自动保存在原文件统一文件夹中,修改名称为*_processed.xlsx

(可选1)在windows环境下运行该ps1(这里命名为:excel_c.ps1)文件还需要对应的run.bat文件,下面是run.bat的代码

@echo off
powershell -ExecutionPolicy Bypass -File "%~dp0\excel_c.ps1"
pause

(可选2)将excel_c.ps1文件转换为对应exe文件,这样可以直接双击打开,下面是封装的操作:

  1. 以管理员身份运行PowerShell
    (右键点击PowerShell图标 → 选择"以管理员身份运行")

  2. 放宽执行策略(仅本次会话有效)

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
  3. 重新导入模块

    Import-Module ps2exe
  4. 运行转换命令

    ps2exe "C:\Users\jeffr\Desktop\inbody数据处理\excel_c.ps1" ".\excel_c.exe"


评论