Eurosign gives strange result - excel

I've a problem when I'm assigning currency format to a specific range in a Excel sheet.
The code in powershell I use to set the format is below :
$LC_ws_tab.range("B1 :B5").NumberFormat = "_(€* #.##0,00_);_(€* (#.##0,00);_(€* ""-""??_);_(#_)"
The thing i find strange is that - iff I run the code I see the screenshot below (euro sign = â,-) .
But when I run the code a second time via "run selected code" when excel document is still open there is a euro sign.
Does anyone know why I've this result?
The complete sample code u see below:
function Excel_new {
$object = New-Object -ComObject "Excel.Application"
$object | Add-Member -MemberType ScriptMethod -Name FullQuit -Value {Excel_FullQuit}
$object
}
function Excel_FullQuit {
while ( $this.Workbooks.Count -gt 0 ) {
$this.Workbooks.Item(1).Close()
}
$this.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($this)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Stop-Process -processname EXCEL
}
$Excel = Excel_new
$Excel.Visible = $true
$Excel.displayalerts = $false
$path = "C:\somefolder\test.xlsx"
$LC_wb = $Excel.Workbooks.Open($path)
$LC_ws_tab = $LC_wb.Worksheets.Add()
$LC_ws_tab.name = "test"
# $LC_ws_tab.range("A1 :A5").NumberFormat = "#.##0,00 € "
# $LC_ws_tab.range("B1 :B5").NumberFormat = "€ #.##0,00"
# $LC_ws_tab.range("C1 :C5").NumberFormat = "0,00 % "
$LC_ws_tab.Cells.Item( 1 , 1 ) = 49999
$LC_ws_tab.Cells.Item( 1 , 2 ) = 1234.879
$LC_ws_tab.Cells.Item( 1 , 3 ) = 1234.879
$LC_ws_tab.range("B1 :B5").NumberFormat = "_(€* #.##0,00_);_(€* (#.##0,00);_(€* ""-""??_);_(#_)"

Related

Find cell value begins or contains using wildcards with powershell

i have a trouble findiong a value in an excel sheet.
i want to get the cell number that contains a value with a string, but i need to use wildcars.
the cell that i am searching begins with B- and i am using this code:
$excel = New-Object -ComObject excel.application
$destinationPath = "C:\Users\john\Desktop\wb.xlsx"
$workbook = $excel.Workbooks.Open($destinationPath)
$sheet1 = $workbook.WorkSheets.item("Sheet1")
$sheet1.activate()
$range = $sheet1.Range("A:A").EntireColumn
$s = $range.find("B-")
write-host "Range found: " $s.address().tostring()
this returns me the first cell that contains B- .... but i need to know the first cell that begins with this. So i think i must use wildcards but i don't know how.
please, could anyone help me to achieve this??
thank you!! BR.
Here a working solution which is able to use regex as well:
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $True
$excel.DisplayAlerts = $False
$workbook = $excel.Workbooks.Open( "C:\Users\john\Desktop\wb.xlsx", [System.Type]::Missing, $false )
$worksheet = $workbook.WorkSheets.item("Sheet1")
[void]$worksheet.activate()
$searchRange = $worksheet.Range("A:A").EntireColumn
$searchFor = '^.*B\-.*$'
$foundCell = $null
foreach( $cell in $searchRange.Cells ) {
if( $cell.Value2 -match $searchFor ) {
$foundCell = $cell
break
}
}
if( $foundCell ) {
$foundCell.Value2
}
[void]$workbook.Close()
[void]$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

Reading value from Excel sheet

$filepath = "C:\Users\Desktop\New folder\Tangent.xlsx"
$sheetname = "sheet"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($filepath)
$WorkBook.sheets | Select-Object -Property Name
$WorkSheet = $WorkBook.Sheets.Item($sheetname)
$myObj = [PSCustomObject][ordered]#{
john = $WorkSheet.Range("B1").Text
Rebel = $WorkSheet.Range("B2").Text
MArk = $WorkSheet.Range("B3").Text
Susan = $WorkSheet.Range("B4").Text
Patty = $WorkSheet.Range("B5").Text
}
I have hardcoded all the names into the code which is a weird way of doing it. I want it to read from the Excel directing using command. Can anyone help me please?
Create an empty hashtable and fill it as you iterate over the rows in your Excel sheet, then create the object.
$ht = #{}
$i = 1
while ($WorkSheet.Cells.Item($i, 1).Text) {
$ht[$WorkSheet.Cells.Item($i, 1).Text] = $WorkSheet.Cells.Item($i, 2).Text
$i++
}
$obj = [PSCustomObject]$ht
Untested, as I don't have Excel at hand here.

Need a little help adding a table range from an excel file on sharepoint to an email body with powershell

$if = '\\portal2010.brand.com\sites\HC\2017 count.xlsx'
$excel = New-Object -Com Excel.Application
$Workbook = $Excel.Workbooks.Open($if)
$page = 'HC'
$ws = $Workbook.worksheets | where-object {$_.Name -eq $page}
$range = $ws.Range("V2:AF")
$rows = $range.Rows.Count
$hcTableCopy = $ws.Range("V2:AF$rows").Copy()
$hcTablePaste = $hcTableCopy.PasteSpecial($default, $default, $default, $default, 9, $default, $default)
$SendTo = "e.mail#brand.com"
$SMTPServer = "smtp.brand.com"
$EmailFrom = "e.mail2#brand.com"
$EmailSubject = "Weekly Email"
$Image2 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image002.png'
$Image4 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image004.png'
$Image6 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image006.png'
$Image8 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image008.png'
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$att2 = new-object Net.Mail.Attachment($Image2)
$att2.ContentId = "att2"
$att4 = new-object Net.Mail.Attachment($Image4)
$att4.ContentId = "att4"
$att6 = new-object Net.Mail.Attachment($Image6)
$att6.ContentId = "att6"
$att8 = new-object Net.Mail.Attachment($Image8)
$att8.ContentId = "att8"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = '
<img src="cid:att2" /><br/><br/>
<img src="cid:att4" /><br/><br/>
<img src="cid:att6" /><br/><br/>
<img src="cid:att8" /><br/><br/>
'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Cc.Add($CCTo1)
$Message.Cc.Add($CCTo2)
$Message.Cc.Add($CCTo3)
$Message.Cc.Add($CCTo4)
$Message.Subject = $EmailSubject
$Message.Body = $body + $hcTablePaste
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att2)
$Message.Attachments.Add($att4)
$Message.Attachments.Add($att6)
$Message.Attachments.Add($att8)
$smtp.Send($Message)
$excel.DisplayAlerts = $False
Start-Sleep -s 5
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
The above code is what I have tried so far. I get an error that says it can't open the file from sharepoint at this point and I am not sure the table paste portion of this is going to work right.
I tried modifying the following, but I really don't think I should have to check out and make editable the file just to copy the table range to an email.
foreach ($file in $excelfiles)
{
$workbookpath = $file.fullname
if ($excel.workbooks.canCheckOut($workbookpath)) {
# open the worksheet and check it out
$excelworkbook = $excel.workbooks.Open($workbookpath)
$excelworkbook = $excel.workbooks.CheckOut($workbookpath)
# Don't ask cuz I don't know (yet). You have to open it again.
$excelworkbook = $excel.workbooks.Open($workbookpath)
# Refresh all the pivot tables with the new data.
$excelworkbook.RefreshAll()
# Save and Check it in
$excelworkbook.Save()
$excelworkbook.CheckInWithVersion()
}
}
$excel.quit()
So any help to get the table pasting part would be great. The email send with images just fine other than that.

Powershell work with multiline text in Excel cell

I have a fairly simple request (for me it is quite tough task tbh).
I have two CSV files which I want to convert to Excel so each of these two CSV files would occupy one sheet each.
So far, I have made it work, but I have this small thing I want to correct.
One of the cells in CSV contains multiple text lines, something like this:
This is entry 1
This is entry 2
I would like to have these two entries to be imported into Excel cell the same way it is in CSV, but when I check my Excel file, the second entry is imported into next row:
Row 1 Cell1 - This is entry 1
Row 2 Cell1 - This is entry 2
I don't know if I should work with .NET class worksheet.UsedRange.EntireRow or worksheet.UsedRange.EntireColumn or something else.
I was checking MSDN, but since I am still a noob, I couldn't find anything.
This is the sample of my code:
Function Merge-CSVFiles
{
Param(
$CSVPath = ".\Reports",
$XLOutput=".\final_final_report.xlsx"
)
$csvFiles = Get-ChildItem ("$CSVPath\*") -Include *.csv
$Excel = New-Object -ComObject excel.application
$Excel.visible = $false
$Excel.sheetsInNewWorkbook = $csvFiles.Count
$workbooks = $excel.Workbooks.Add()
$CSVSheet = 1
Foreach ($CSV in $Csvfiles)
{
$worksheets = $workbooks.worksheets
$CSVFullPath = $CSV.FullName
$SheetName = ($CSV.name -split "\.")[0]
$worksheet = $worksheets.Item($CSVSheet)
$worksheet.Name = $SheetName
$TxtConnector = ("TEXT;" + $CSVFullPath)
$CellRef = $worksheet.Range("A1")
$Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef)
$worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True
$worksheet.QueryTables.item($Connector.name).TextFileParseType = 1
$worksheet.QueryTables.item($Connector.name).Refresh()
$worksheet.QueryTables.item($Connector.name).delete()
$worksheet.UsedRange.EntireColumn.AutoFit()
$CSVSheet++
}
$workbooks.SaveAs($XLOutput,51)
$workbooks.Saved = $true
$workbooks.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbooks) | Out-Null
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
I am not sure why the method you are using does that but I was able to reproduce on my system as well. I do some similar operations in one of my scripts that builds a large Excel workbook and I have used the following method with success:
Import the CSV with Import-CSV
Convert it to a multi-dimensional array
Write the array to Value2 of the range object
For example, replace your code that looks like this:
$TxtConnector = ("TEXT;" + $CSVFullPath)
$CellRef = $worksheet.Range("A1")
$Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef)
$worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True
$worksheet.QueryTables.item($Connector.name).TextFileParseType = 1
$worksheet.QueryTables.item($Connector.name).Refresh()
$worksheet.QueryTables.item($Connector.name).delete()
With this instead:
$CsvContents = Import-Csv $CSVFullPath
$MultiArray = (ConvertTo-MultiArray $CsvContents -Headers).Value
$StartRowNum = 1
$StartColumnNum = 1
$EndRowNum = $CsvContents.Count + 1
$EndColumnNum = ($CsvContents | Get-Member | Where-Object { $_.MemberType -eq 'NoteProperty' }).Count
$Range = $worksheet.Range($worksheet.Cells($StartRowNum, $StartColumnNum), $worksheet.Cells($EndRowNum, $EndColumnNum))
$Range.Value2 = $MultiArray
For that to work you will also need the function I use for converting an object to a multi-dimensional array (based off the one posted here but with some slight modifications):
function ConvertTo-MultiArray
{
param (
$InputObject,
[switch]$Headers = $false
)
begin
{
$Objects = #()
[ref]$Array = [ref]$null
}
process
{
$Objects += $InputObject
}
end
{
$Properties = $Objects[0].PSObject.Properties | ForEach-Object{ $_.Name }
$Array.Value = New-Object 'object[,]' ($Objects.Count + 1), $Properties.Count
$ColumnNumber = 0
if ($Headers)
{
$Properties | ForEach-Object{
$Array.Value[0, $ColumnNumber] = $_.ToString()
$ColumnNumber++
}
$RowNumber = 1
}
else
{
$RowNumber = 0
}
$Objects | ForEach-Object{
$Item = $_
$ColumnNumber = 0
$Properties | ForEach-Object{
if ($Item.($_) -eq $null)
{
$Array.Value[$RowNumber, $ColumnNumber] = ""
}
else
{
$Array.Value[$RowNumber, $ColumnNumber] = $Item.($_).ToString()
}
$ColumnNumber++
}
$RowNumber++
}
$Array
}
}

Export as CSV instead of a XLS file

I have a script that places everything nicely into a spread sheet. The problem is, I need it to export as a csv file instead. All the foreach loops are completely baffling me here as far as where to put the export csv functions in the script. If someone could could school me on how to get the fields into a csv file, It would be greatly appreciated.
$date = 0
$date = get-date -format "yyyy-MMM-dd-hhmm"
$date
#New Excel Application
$Excel = New-Object -Com Excel.Application
$Excel.visible = $False
# Create 1 worksheets
$Excel = $Excel.Workbooks.Add()
# Assign each worksheet to a variable and
# name the worksheet.
$Sheet1 = $Excel.Worksheets.Item(1)
$Sheet1.Name = "HH_SERVERS"
#Create Heading for General Sheet
$Sheet1.Cells.Item(1, 1) = "Machine_Name"
$Sheet1.Cells.Item(1, 2) = "OS"
$Sheet1.Cells.Item(1, 3) = "Software"
$Sheet1.Cells.Item(1, 4) = "Vendor"
$Sheet1.Cells.Item(1, 5) = "Version"
$colSheets = ($Sheet1)
foreach ($colorItem in $colSheets)
{
$intRow = 2
$intRowDisk = 2
$intRowSoft = 2
$intRowNet = 2
$WorkBook = $colorItem.UsedRange
$WorkBook.Interior.ColorIndex = 20
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
}
#Auto Fit all sheets in the Workbook
foreach ($colorItem in $colSheets)
{
$WorkBook = $colorItem.UsedRange
$WorkBook.EntireColumn.AutoFit()
clear
}
$Servers = get-content "c:\temp\HH_Servers.txt"
foreach ($Server in $Servers)
{
$GenItems2 = gwmi Win32_OperatingSystem -Comp $Server
$Software = gwmi Win32_Product -Comp $Server
# Populate General Sheet(1) with information
foreach ($objItem in $GenItems2)
{
$Sheet1.Cells.Item($intRow, 2) = $objItem.Caption
}
#Populate Software Sheet
foreach ($objItem in $Software)
{
$Sheet1.Cells.Item($intRowSoft, 1) = $Server
$Sheet1.Cells.Item($intRowSoft, 3) = $objItem.Name
$Sheet1.Cells.Item($intRowSoft, 4) = $objItem.Vendor
$Sheet1.Cells.Item($intRowSoft, 5) = $objItem.Version
$intRowSoft = $intRowSoft + 1
}
}
$outputfile = "c:\temp\" + $date.toString() + "-HH_Server_Software"
$Excel.SaveAs($outputfile)
$Excel.Close()
Write-Host "*******************************" -ForegroundColor Green
Write-Host "The Report has been completed." -ForeGroundColor Green
Write-Host "*******************************" -ForegroundColor Green
# ========================================================================
# END of Script
# ==================
You can't save an entire workbook as CSV. You need to save the individual worksheet instead. The file format value for CSV is 6 (don't remember where I found that out though):
$xlCSV = 6
$outputfile = "c:\temp\" + $date.toString() + "-HH_Server_Software.csv"
$Sheet1.SaveAs($outputfile, $xlCSV)
(Tested on Windows 7 with Excel 2013.)
Thanks to #Matt for a comment with a link to the XLFileFormat Enumerations.

Resources