Skip to content

Instantly share code, notes, and snippets.

@frekac
Last active October 26, 2016 20:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save frekac/865d5b027c63d77d24bd0000a1c5c893 to your computer and use it in GitHub Desktop.
# Add the EPPlus library
Add-Type -Path C:\scripts\EPPlus\EPPlus.dll
# Create the Excel Package
$newFile = "C:\scripts\Sample.xlsx"
[OfficeOpenXml.ExcelPackage]$excelPackage = New-Object OfficeOpenXML.ExcelPackage($newFile)
# Create the Excel WorkBook
[OfficeOpenXml.ExcelWorkbook]$book = $excelPackage.Workbook
# Create a reference to the first worksheet
$dataSheet = [OfficeOpenXml.ExcelWorksheet]$book.Worksheets[1]
# Get value of cell A1 and add information
$newValue = ($dataSheet.Cells["A1"].Value) + " and more info"
$datasheet.Cells["A1"].Value = "$newValue"
# Add data to A2
$dataSheet.Cells["A2"].Value = "A second cell filled with information"
# Change background color/style of cells.
$addStyle = $dataSheet.Cells["A1:E1"].Style.Fill
$addStyle.PatternType = "Solid"
$addStyle.BackgroundColor.SetColor("LightSkyBlue")
# Save the Excel file when referencing a file in the package initiation
$excelPackage.SaveAs($newFile)
# Dispose of the Excel Pacakge
$excelPackage.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment