Autofit xuất khẩu powershell excel

Mọi người đều muốn tạo ra các báo cáo bóng bẩy. Bạn muốn gây ấn tượng với quản lý?

Đây là Get-Process, Get-Servicedirectory listing tất cả trong một định dạng bảng tính Excel dưới dạng báo cáo, ngoài ra, nó có tính tương tác

Autofit xuất khẩu powershell excel

Hàm Export-Excel PowerShell hoạt động giống như cách bạn mong đợi Out-GridView hoạt động, ngoại trừ, bạn nhận được một trang tính Excel khi hoàn tất

Đọc tiếp để biết cách thực hiện dễ dàng và tính năng phần thưởng ở cuối

Mã PowerShell

Để kết hợp điều này lại với nhau, bạn cần mô-đun PowerShell của tôi từ Thư viện PowerShell. Nếu bạn muốn xem kết quả, bạn cũng cần cài đặt Microsoft Excel

Ghi chú. Bạn có thể tạo cùng một báo cáo này mà không cần cài đặt Excel. Đó là một tính năng tuyệt vời nếu bạn muốn chạy quy trình thu thập dữ liệu không cần giám sát hoặc trên máy chủ. Sau đó, bạn có thể gửi kết quả qua email hoặc lưu trữ trên tệp chia sẻ

Cài đặt mô-đun PowerShell từ thư viện

Install-Module -Name ImportExcel

Bây giờ bạn đã sẵn sàng để chạy tập lệnh và nhận kết quả

Đầu ra của Get-Process, Get-Service

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
0 được chuyển sang Excel, đến cùng một sổ làm việc
$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
1. Bằng cách không chỉ định
$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
2, Export-Excel sẽ chèn dữ liệu vào cùng một trang tính, trong trường hợp này là mặc định
$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
4

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show

Sử dụng tham số

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
5, bạn cho biết nơi bắt đầu chèn dữ liệu của Export-Excel

Tham số

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
7 khá mạnh, nó cho phép bạn đặt tên cho khu vực bạn vừa xuất và thiết lập nó dưới dạng Bảng Excel. Điều đó cung cấp cho bạn các mũi tên thả xuống và sọc vằn, cho phép lọc dữ liệu tương tác

Công tắc

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
8 thay đổi kích thước các cột của bảng tính để phù hợp với dữ liệu trong đó

Lần thu thập dữ liệu cuối cùng,

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
0, bạn làm điều gì đó hơi khác một chút. Sử dụng Install-Module -Name ImportExcel0, nó trả về một đối tượng đại diện cho sổ làm việc Excel đã được tạo. Bạn lưu nó trong biến Install-Module -Name ImportExcel1

Sử dụng Install-Module -Name ImportExcel1, bạn có thể truy cập các thuộc tính sổ làm việc, trang tính, v.v. Ở đây bạn đặt Install-Module -Name ImportExcel3 vào bảng tính

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
4 Install-Module -Name ImportExcel5

Bây giờ bạn có thể chèn dữ liệu bằng hàm Install-Module -Name ImportExcel6, cùng với việc đặt kích thước phông chữ và thuộc tính in đậm

Đừng quên sử dụng Install-Module -Name ImportExcel7, điều này sẽ lưu các thay đổi của bạn vào đĩa. Công tắc Install-Module -Name ImportExcel8 tự động khởi chạy Excel với tệp Excel mới được tạo

Tôi khuyến khích bạn khám phá mô-đun Excel PowerShell, đây mới chỉ là bề nổi của những gì có thể. Kiểm tra tất cả các ví dụ về mô-đun

Tính năng tiền thưởng

Tham số

$xlfile = "$env:TEMP\PSreports.xlsx"
Remove-Item $xlfile -ErrorAction SilentlyContinue

# Get-Process
Get-Process | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 2 -TableName ReportProcess

# Get-Service
Get-Service | Select -First 5 |
    Export-Excel $xlfile -AutoSize -StartRow 11 -TableName ReportService

# Directory Listing
$excel = Get-ChildItem $env:HOMEPATH\Documents\WindowsPowerShell |
    Select PSDRive, PSIsC*, FullName, *time* |
    Export-Excel $xlfile -AutoSize -StartRow 20 -TableName ReportFiles -PassThru

# Get the sheet named Sheet1
$ws = $excel.Workbook.Worksheets['Sheet1']

# Create a hashtable with a few properties
# that you'll splat on Set-Format
$xlParams = @{WorkSheet=$ws;Bold=$true;FontSize=18;AutoSize=$true}

# Create the headings in the Excel worksheet
Set-Format -Range A1  -Value "Report Process" @xlParams
Set-Format -Range A10 -Value "Report Service" @xlParams
Set-Format -Range A19 -Value "Report Files"   @xlParams

# Close and Save the changes to the Excel file
# Launch the Excel file using the -Show switch
Close-ExcelPackage $excel -Show
7 mạnh mẽ, nó cho phép bạn đặt tên cho khu vực bạn vừa xuất và thiết lập khu vực đó dưới dạng Bảng Excel. Điều đó cung cấp cho bạn các mũi tên thả xuống và sọc vằn, cho phép lọc dữ liệu tương tác. Ngoài ra, tính năng lọc được áp dụng cho từng thuộc tính đã được xuất sang Excel

Ở đây bạn có thể thấy mục 1, nhấp vào mũi tên thả xuống bên cạnh Get-Process0 trong phần Get-Process1, nó sẽ hiện ra hộp thoại. Trong hộp thoại, bạn có thể sắp xếp dữ liệu hoặc lọc dữ liệu theo nhiều cách. Bạn cũng nhận được một danh sách tất cả các giá trị trong cột đó cho bảng đó và bạn có thể kiểm tra tất cả các giá trị mà bạn muốn xem

Autofit xuất khẩu powershell excel

Kết thúc

Một lần nữa, tôi khuyến khích bạn khám phá mô-đun Excel PowerShell, đây mới chỉ là bề nổi của những gì có thể và kết quả cuối cùng là tuyệt vời để trình bày thông tin phức tạp cho chính bạn, đồng nghiệp hoặc cấp quản lý. Kiểm tra tất cả các ví dụ về mô-đun