如何使用Powershell将下面的CSV文件转换为JSON?

时间:2019-08-14 04:39:04

标签: powershell

如何从下面将Xlsx转换为JSON?我想在JSON文件中提取这样的数据:

{"POV":["DC01","DC05",...,"Input"],
"Years": ["FY19", "FY19",..., "FY19"],
"Columns":[["Jan", "Feb",...,"Apr"]],
"Rows":[{"Account":["Meals"],"Data":["5000","6000","5000","7000"]}

真正在PowerShell(Example)中挣扎:

$Excel = New-Object -Com Excel.Application
$wb = $Excel.Workbooks.Open("C:\Users\Blam\OneDrive - Huron Consulting Group\Desktop\Projects\Bio_Rad\EPBCS - Copy\EPBCS - Copy\Test.xlsx")

$WorkBook = $objExcel.Workbooks.Open($strPath)
$Worksheet = $Worksheet.sheet.item("Test")
$intRowMax = ($worksheet.UsedRange.Rows).count

1 个答案:

答案 0 :(得分:1)

Import-Csv .\Test.csv | ConvertTo-Json | Add-Content -Path "result.json"

我的Excel:

enter image description here

这是JSON结果

[
    {
        "Jan":  "6000",
        "Feb":  "2342",
        "Mar":  "5424"
    },
    {
        "Jan":  "6001",
        "Feb":  "2343",
        "Mar":  "5425"
    },
    {
        "Jan":  "6002",
        "Feb":  "2344",
        "Mar":  "5426"
    },
    {
        "Jan":  "6003",
        "Feb":  "2345",
        "Mar":  "5427"
    },
    {
        "Jan":  "6004",
        "Feb":  "2346",
        "Mar":  "5428"
    },
    {
        "Jan":  "6005",
        "Feb":  "2347",
        "Mar":  "5429"
    },
    {
        "Jan":  "6006",
        "Feb":  "2348",
        "Mar":  "5430"
    },
    {
        "Jan":  "6007",
        "Feb":  "2349",
        "Mar":  "5431"
    }
]