如何通过powershell导出1048,576行以上的CSV文件

时间:2022-02-18 21:42:42

As you know Excel has got 1048574 rows. How can I export more than one million rows below my code ? if max rows is reached then how can I continue over new worksheet in Loop ? how to adapted it? I'm googling some after I find nice powershell functions it.

Excel有1048574行。如何在代码下面导出100多万行?如果到达最大行,那么如何在循环中继续处理新的工作表呢?如何适应吗?找到powershell函数后,我在谷歌上搜索一下。

[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OOXML.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXmlFormats.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\ICSharpCode.SharpZipLib.dll")

$wb = New-Object NPOI.XSSF.UserModel.XSSFWorkbook;
$ws = $wb.CreateSheet("Company_NTFS_Permissions");
$wr = $ws.CreateRow(0);



$wr.createCell(0).setCellValue("Folder Path");
$wr.createCell(1).setCellValue("Users/Groups");
$wr.createCell(2).setCellValue("Permissions");
$wr.createCell(3).setCellValue("AccessControlType");
$wr.createCell(4).setCellValue("Permissions Inherited")



$dirToAudit = Get-ChildItem -Path "C:\inetpub" -recurse | Where { $_.psIsContainer -eq $true }

$intRow = 1

foreach ($dir in $dirToAudit)
{
    $colACL = Get-Acl -Path $dir.FullName

    foreach ($acl in $colACL)
    {
        $fileNameRow = $ws.CreateRow($intRow)
        $fileNameRow.CreateCell(0).SetCellValue($dir.FullName)
        $intRow++

        foreach ($accessRight in $acl.Access)
        {
            $values = $ws.CreateRow($intRow)
            $values.CreateCell(1).SetCellValue($($AccessRight.IdentityReference).ToString())
            $values.CreateCell(2).SetCellValue($($AccessRight.FileSystemRights).ToString())
            $values.CreateCell(3).SetCellValue($($AccessRight.AccessControlType).ToString())
            $values.CreateCell(4).SetCellValue($($acl.AreAccessRulesProtected).ToString())
            $intRow++

        }
    }

}
$fs = new-object System.IO.FileStream("C:\DRIVERS\test.xlsx",[System.IO.FileMode]'Create',[System.IO.FileAccess]'Write')
$wb.Write($fs);
$fs.Close()

1 个解决方案

#1


1  

Use your existing knowledge of the current row number to determine when the max row is reached, then move to a new sheet.

使用当前行号的现有知识来确定何时到达最大行,然后移动到新表。

[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OOXML.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXmlFormats.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\ICSharpCode.SharpZipLib.dll")

$wb = New-Object NPOI.XSSF.UserModel.XSSFWorkbook;
$ws = $wb.CreateSheet("Company_NTFS_Permissions");
$wr = $ws.CreateRow(0);



$wr.createCell(0).setCellValue("Folder Path");
$wr.createCell(1).setCellValue("Users/Groups");
$wr.createCell(2).setCellValue("Permissions");
$wr.createCell(3).setCellValue("AccessControlType");
$wr.createCell(4).setCellValue("Permissions Inherited")



$dirToAudit = Get-ChildItem -Path "C:\inetpub" -recurse | Where { $_.psIsContainer -eq $true }

$maxRow = 1048576
$intRow = 1
$intNextSheet = 2

foreach ($dir in $dirToAudit)
{
    $colACL = Get-Acl -Path $dir.FullName

    foreach ($acl in $colACL)
    {
        $fileNameRow = $ws.CreateRow($intRow)
        $fileNameRow.CreateCell(0).SetCellValue($dir.FullName)
        $intRow++

        if ($intRow -eq $maxRow)
        {
            $ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
            $intNextSheet++
            $intRow = 0
        }

        foreach ($accessRight in $acl.Access)
        {
            $values = $ws.CreateRow($intRow)
            $values.CreateCell(1).SetCellValue($($AccessRight.IdentityReference).ToString())
            $values.CreateCell(2).SetCellValue($($AccessRight.FileSystemRights).ToString())
            $values.CreateCell(3).SetCellValue($($AccessRight.AccessControlType).ToString())
            $values.CreateCell(4).SetCellValue($($acl.AreAccessRulesProtected).ToString())
            $intRow++

            if ($intRow -eq $maxRow)
            {
                $ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
                $intNextSheet++
                $intRow = 0
            }
        }
    }

}
$fs = new-object System.IO.FileStream("C:\DRIVERS\test.xlsx",[System.IO.FileMode]'Create',[System.IO.FileAccess]'Write')
$wb.Write($fs);
$fs.Close()

The important bit here:

这里重要的一点:

if ($intRow -eq $maxRow)
{
    $ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
    $intNextSheet++
    $intRow = 0
}

This runs after each time $intRow gets incremented, checking whether the $maxRow has been reached. If so, it moves to a new sheet with a numbered name and restarts from the first row.

每次递增$intRow时,它都会运行,检查是否已经到达$maxRow。如果是,它将移动到具有编号名称的新表,并从第一行重新开始。

#1


1  

Use your existing knowledge of the current row number to determine when the max row is reached, then move to a new sheet.

使用当前行号的现有知识来确定何时到达最大行,然后移动到新表。

[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OOXML.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXml4Net.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\NPOI.OpenXmlFormats.dll")
[Reflection.Assembly]::LoadFrom("C:\DRIVERS\NPOI 2.2.1 binary package\Release\Net40\ICSharpCode.SharpZipLib.dll")

$wb = New-Object NPOI.XSSF.UserModel.XSSFWorkbook;
$ws = $wb.CreateSheet("Company_NTFS_Permissions");
$wr = $ws.CreateRow(0);



$wr.createCell(0).setCellValue("Folder Path");
$wr.createCell(1).setCellValue("Users/Groups");
$wr.createCell(2).setCellValue("Permissions");
$wr.createCell(3).setCellValue("AccessControlType");
$wr.createCell(4).setCellValue("Permissions Inherited")



$dirToAudit = Get-ChildItem -Path "C:\inetpub" -recurse | Where { $_.psIsContainer -eq $true }

$maxRow = 1048576
$intRow = 1
$intNextSheet = 2

foreach ($dir in $dirToAudit)
{
    $colACL = Get-Acl -Path $dir.FullName

    foreach ($acl in $colACL)
    {
        $fileNameRow = $ws.CreateRow($intRow)
        $fileNameRow.CreateCell(0).SetCellValue($dir.FullName)
        $intRow++

        if ($intRow -eq $maxRow)
        {
            $ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
            $intNextSheet++
            $intRow = 0
        }

        foreach ($accessRight in $acl.Access)
        {
            $values = $ws.CreateRow($intRow)
            $values.CreateCell(1).SetCellValue($($AccessRight.IdentityReference).ToString())
            $values.CreateCell(2).SetCellValue($($AccessRight.FileSystemRights).ToString())
            $values.CreateCell(3).SetCellValue($($AccessRight.AccessControlType).ToString())
            $values.CreateCell(4).SetCellValue($($acl.AreAccessRulesProtected).ToString())
            $intRow++

            if ($intRow -eq $maxRow)
            {
                $ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
                $intNextSheet++
                $intRow = 0
            }
        }
    }

}
$fs = new-object System.IO.FileStream("C:\DRIVERS\test.xlsx",[System.IO.FileMode]'Create',[System.IO.FileAccess]'Write')
$wb.Write($fs);
$fs.Close()

The important bit here:

这里重要的一点:

if ($intRow -eq $maxRow)
{
    $ws = $wb.CreateSheet("Company_NTFS_Permissions" + $intNextSheet);
    $intNextSheet++
    $intRow = 0
}

This runs after each time $intRow gets incremented, checking whether the $maxRow has been reached. If so, it moves to a new sheet with a numbered name and restarts from the first row.

每次递增$intRow时,它都会运行,检查是否已经到达$maxRow。如果是,它将移动到具有编号名称的新表,并从第一行重新开始。