Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Created June 15, 2016 11:44
Show Gist options
  • Select an option

  • Save XPlantefeve/2a1aa32dfab856021a2a57f217fda9d1 to your computer and use it in GitHub Desktop.

Select an option

Save XPlantefeve/2a1aa32dfab856021a2a57f217fda9d1 to your computer and use it in GitHub Desktop.
Barebone excel creation + CSV import method. Basis for further work.
#region ExcelFunctions
function Get-NormalizedPath ( $Path ) {
if ( ( Split-Path $Path ) -match '^(\.|)$' ) {
$Path = Join-Path -Path ( Get-Location ) -ChildPath ( Split-Path -Path $Path -Leaf )
}
return $Path
}
$MethodImportCSVtoSheet = {
Param (
[string]$Path,
[string]$SheetName = $null,
[int]$Sheet = $null
)
#
if ( $Sheet ) { $oSheet = $this.excel.Worksheets.Item($Sheet) }
else { $oSheet = $this.excel.ActiveSheet }
$FullPath = Resolve-Path -Path $Path | select -ExpandProperty ProviderPath
$oSheet.Activate()
if ( $SheetName ) { $oSheet.Name = $SheetName }
else { $oSheet.Name = Split-Path -Path $FullPath -Leaf }
$TxtConnector = ("TEXT;${FullPath}")
$CellRef = $oSheet.Range('A1')
$Connector = $oSheet.QueryTables.add($TxtConnector,$CellRef)
$oSheet.QueryTables.Item($Connector.Name)."TextFileCommaDelimiter" = $True
$oSheet.QueryTables.Item($Connector.Name).TextFileParseType = 1
[void] $oSheet.QueryTables.Item($Connector.Name).Refresh()
[void] $oSheet.QueryTables.Item($Connector.Name).delete()
$oSheet.Rows.Item(1).Font.Bold = $True
[void] $oSheet.Rows.Item(1).AutoFilter()
[void] $oSheet.UsedRange.EntireColumn.AutoFit()
[void] $oSheet.Activate()
$oSheet.Application.ActiveWindow.SplitRow = 1
$oSheet.Application.ActiveWindow.FreezePanes = $True
#>
}
$MethodSaveAs = {
Param (
[string]$Path,
[string]$Format = 51
)
$Path = Get-NormalizedPath -Path $Path
# If ( Test-Path -Path $Path ) { Remove-Item $Path }
$cachedValue = $this.excel.DisplayAlerts
$this.excel.DisplayAlerts = $false
$this.excel.ActiveWorkbook.SaveAs( $Path , $Format )
$this.excel.DisplayAlerts = $cachedValue
}
$MethodQuit = {
$this.excel.quit()
while ( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($this.excel) -gt 0 ) { <#dummy#> }
}
Function New-ExcelDocument {
[CmdletBinding()]
Param
(
[Parameter()]
[int]$Sheets = 1,
[switch]$Visible
)
$document = New-Object -TypeName psobject
Add-Member -InputObject $document -MemberType NoteProperty -Name excel -Value ( New-Object -ComObject excel.application )
$document.excel.Visible = $Visible
$cacheSheetsInNewWorkbook = $document.excel.Application.SheetsInNewWorkbook
$document.excel.Application.SheetsInNewWorkbook = 1
[void] $document.excel.Workbooks.Add()
$document.excel.Application.SheetsInNewWorkbook = $cacheSheetsInNewWorkbook
Add-Member -InputObject $document -MemberType ScriptMethod -Name ImportCSV -Value $MethodImportCSVtoSheet
Add-Member -InputObject $document -MemberType ScriptMethod -Name SaveAs -Value $MethodSaveAs
Add-Member -InputObject $document -MemberType ScriptMethod -Name Quit -Value $MethodQuit
return $document
}
#endregion
$test = New-ExcelDocument -Sheets 1 -Visible
$test.ImportCSV('test.csv','testouille')
$test.SaveAs('test.xlsx')
$test.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment