Powershell Interaction with Excel (Highlighting Cells)

I'm using Powershell to compare two, large csv files and output the rows which don't match. Once the two output files are made (one for the differences on each), I bring them up on Excel. Now I want to highlight the mismatching fields for the output to be easily read by my customer. Starting where I open the files in Excel, my current code is:

$excel= New-Object -ComObject "excel.application" $excel.Visible=$true $ocomp1=$excel.Workbooks.Open("C:\Users\aja3885.BUD\Desktop\agclink-data-mining\agclink-data-mining\test1.csv") $ocomp2=$excel.Workbooks.Open("C:\Users\aja3885.BUD\Desktop\agclink-data-mining\agclink-data-mining\test2.csv") $comp1=$ocomp1.worksheets.item(1) $comp2=$ocomp2.worksheets.item(1) $Row=$comp1.UsedRange.rows.count $Column=$comp1.UsedRange.columns.count $comp1.UsedRange.EntireColumn.AutoFit()|out-null $comp2.UsedRange.EntireColumn.AutoFit()|out-null $i=1 for($i=2; $i -le $row; $i++) {if($comp1.cells.item($i,1).value2 -eq 'pntval'){if($comp1.cells.item($i,8).value2 -eq 'VALUTYPE=1'){$comp1.cells.item($i,4).interior.colorindex=3} elseif($comp1.cells.item($i,8).value2 -eq 'VALUTYPE=2'){$comp1.cells.item($i,6).interior.colorindex=3} elseif($comp1.cells.item($i,8).value2 -eq 'VALUTYPE=3'){$comp1.cells.item($i,5).interior.colorindex=3} elseif($comp1.cells.item($i,8).value2 -eq 'VALUTYPE=4'){$comp1.cells.item($i,7).interior.colorindex=3}}}  for($i=2; $i -le $row; $i++) {if($comp2.cells.item($i,1).value2 -eq 'pntval'){if($comp2.cells.item($i,8).value2 -eq 'VALUTYPE=1'){$comp2.cells.item($i,4).interior.colorindex=3} elseif($comp2.cells.item($i,8).value2 -eq 'VALUTYPE=2'){$comp2.cells.item($i,6).interior.colorindex=3} elseif($comp2.cells.item($i,8).value2 -eq 'VALUTYPE=3'){$comp2.cells.item($i,5).interior.colorindex=3} elseif($comp2.cells.item($i,8).value2 -eq 'VALUTYPE=4'){$comp2.cells.item($i,7).interior.colorindex=3}}} 

This code works, but the part which highlights the fields (the for loops on bottom) is cripplingly slow. How can I speed this up?

Also, I just asked this on Overflow, and was redirected here; hope this is the right spot.

Replay

Category: excel Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.523 (s). 12 q(s)