PowerShell EPPlus无法更改单元格的背景颜色

问题描述 投票:0回答:3

我正在使用EPPlus创建Excel文档。它工作正常,但我无法更改单元格的背景颜色。我使用以下代码:

Worksheet.Cells[row,column].Style.Fill.BackgroundColor.rgb = (255,0,0)

每次我执行脚本时都会说:

“rgb”是一个只读属性

当我尝试设置

Worksheet.Cells.Style.Fill.BackgroundColor = xxx

我犯了同样的错误:

“BackgroundColor”是只读属性

我没有找到更多选项,你可能会改变颜色或将属性更改为可写...有没有人有想法?

excel powershell epplus
3个回答
0
投票

试试我的PowerShell Excel模块,它包装EPPlus并使交互变得非常简单。

https://www.powershellgallery.com/packages/ImportExcel/

enter image description here

$xlfile = "$env:TEMP\test.xlsx"
rm $xlfile -ErrorAction Ignore

$pkg = ps | select company, Handles| Export-Excel $xlfile -PassThru

$ws = $pkg.Workbook.Worksheets["Sheet1"]

Set-Format -WorkSheet $ws -Range "B2:B2" -BackgroundColor Red
Set-Format -WorkSheet $ws -Range "B5:B5" -BackgroundColor Green

Close-ExcelPackage $pkg -Show

0
投票

你设置这样的颜色。

Worksheet.Cells[row,column].Style.Fill.PatternType = ExcelFillStyle.Solid;
Worksheet.Cells[row,column].Style.Fill.BackgroundColor.SetColor(Color.Red);

0
投票

如果您通过Add-Type直接将EPPlus dll用于Powershell,则可以使用以下代码

$ExcelPackage = New-Object OfficeOpenXml.ExcelPackage

$Sheet1 = $ExcelPackage.Workbook.Worksheets.Add("Sheet1")
$Sheet1.Cells["A1"].Style.Fill.PatternType = 1        # 1 denotes solid color
$Sheet1.Cells["A1"].Style.Fill.BackgroundColor.SetColor([System.Drawing.Color]::FromArgb(147,205,221))
© www.soinside.com 2019 - 2024. All rights reserved.