Showing posts with label Excel. Show all posts
Showing posts with label Excel. Show all posts
Password-Protect an Excel Spreadsheet

Password-Protect an Excel Spreadsheet

July 04, 2011
Adds a password (%reTG54w) to a Microsoft Excel spreadsheet.PowerShell$comments = @' Script name: Set-ExcelPassword.ps1 Created on: Friday, June 29, 2007 Author: Kent Finkle Purpose: How can I use Windows Powershell to Password-Protect an Excel Spreadsheet? '@ #----------------------------------------------------- function Release-Ref ($ref) { ([System.Runtime.InteropServices.Marshal]::ReleaseComObject( [System.__ComObject]$ref) -gt 0) [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers()  } #----------------------------------------------------- $xlNormal = -4143   $xl = new-object -comobject excel.application $xl.Visible = $True $xl.DisplayAlerts = $False   $wb = $xl.Workbooks.Add()  $ws = $wb.Worksheets.Item(1) $ws.Cells.Item(1, 1).Value() = get-date  $a = $wb.SaveAs("C:\Scripts\Test.xls",$xlNormal,"%reTG54w") $a = $xl.Quit()   $a = Release-Ref($ws) $a = Release-Ref($wb) $a = Release-Ref($xl) Verified...
Deletes every other row in a Microsoft Excel worksheet.

Deletes every other row in a Microsoft Excel worksheet.

July 04, 2011
Deletes every other row in a Microsoft Excel worksheet.PowerShell$comments = @' Script name: Delete-EveryOtherRow.ps1 Created on: Sunday, September 02, 2007 Author: Kent Finkle Purpose: How can I use Windows Powershell to delete every other row on an Excel worksheet? #http://support.microsoft.com/kb/213610/en-us '@ #----------------------------------------------------- function Release-Ref ($ref) { ([System.Runtime.InteropServices.Marshal]::ReleaseComObject( [System.__ComObject]$ref) -gt 0) [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers()  } #----------------------------------------------------- $xl = new-object -comobject excel.application $xl.Visible = $True $wb = $xl.Workbooks.Add()  $ws = $wb.Worksheets.Item("Sheet1")  $2d = new-object 'object[,]' 20,1  # Fill an array so we can put the numbers into Excel all at once. for ($i=0; $i -le 19; $i++) {     $2d[$i,0] = $i + 1 } $r = $ws.Range("A1:A20")  # Put the array into the sheet so we have something to work with. $r.Value() = $2d  $y = $false               # Change this to $True if you want to # delete rows 1, 3, 5, and so on. $i = 1 $r = $ws.UsedRange $cnt = $r.rows.Count # Loop once for every row in the selection. for ($x=1; $x -le $cnt; $x++) {     if ($y -eq $true) {         # ...delete an entire row of cells.         $a = $r.Cells.Item($i).EntireRow.Delete()     }     Else {         # ...increment $i by one so we can cycle through range.         $i++     }     # If ($y is true, make it false; if $y is false, make it true.)     $y = -not($y) } $a = Release-Ref($r) $a = Release-Ref($ws) $a = Release-Ref($wb) $a = Release-Ref($xl) Verified...
Import a Large Text File

Import a Large Text File

July 04, 2011
Imports a text file into Excel even if the number of lines in that file exceeds Excel's total number of rows limitation.Visual...
Copy Data From One Spreadsheet to Another

Copy Data From One Spreadsheet to Another

July 04, 2011
Copies data from one Excel spreadsheet to another.PowerShell$comments = @'  Script name: Copy-ExcelData.ps1  Created on: Wednesday, August 22, 2007  Author: Kent Finkle  Purpose: How can I use Windows Powershell to  Copy Data From One Spreadsheet to Another?  '@  # -----------------------------------------------------  function Release-Ref ($ref) {  ([System.Runtime.InteropServices.Marshal]::ReleaseComObject(  [System.__ComObject]$ref) -gt 0)  [System.GC]::Collect()  [System.GC]::WaitForPendingFinalizers()  }  # -----------------------------------------------------  $xl = new-object -comobject excel.application  $xl.Visible = $True  $wb = $xl.Workbooks.Add()  $ws = $wb.Worksheets.Item("Sheet1")    $2d = new-object 'object[,]' 20,1   for ($i = 0; $i -le 19; $i++) {      for ($j = 0; $j -le 0; $j++) {          $2d[$i,$j] = $i      }  }    $r = $ws.Range("A1:A20")  $r.Value() = $2d  $a = $r.Copy()    $xl2 = new-object -comobject excel.application  $xl2.Visible = $True  $wb2 = $xl2.Workbooks.Add()  $ws2 = $wb2.Worksheets.Item("Sheet1")  $a = $ws2.Paste()    $a = Release-Ref($ws2)  $a = Release-Ref($wb2)  $a = Release-Ref($xl2)  $a = Release-Ref($ws)  $a = Release-Ref($wb)  $a = Release-Ref($xl) Verified...