I’m using the following Powershell script to remove the NumberFormat of the cells in a column for a lot of files so all the fractions will be displayed. The column may have decimal, text or date, etc; only these decimal/currency formatted (with format of 0* or *#*) cells need to be applied
However it’s slow (check/update two or three cells every second). Is there a better/faster way to do it?
$WorkBook = $Excel.Workbooks.Open($fileName)
$WorkSheet = $WorkBook.Worksheets.Item(1)
$cell = $WorkSheet.Cells
$ColumnIndex = 10 # The column may have decimal, text or date, etc.
$i = 2
while ($cell.Item($i, 1).value2 -ne $Null)
# Replace it to find the last row# of column 1 may cut the time in half? How?
{
$c = $cell.Item($i, $ColumnIndex)
if (($c.NumberFormat -like "0*") -or $c.NumberFormat -like "*#*")
{
"$i : $($c.NumberFormat) $($c.value2) "
$c.NumberFormat = $Null
}
$i++
}
Update:
Will the .Net Microsoft.Office.Interop.Excel much faster?
Or convert the files to xlsx format and use System.IO.Package.IO?
I improved the speed after read the comment. Thanks all.
Try to reduce the access of the cells as much as possible. I deleted the output line
"$i : $($c.NumberFormat) $($c.value2) "and changeif (($c.NumberFormat -like "0*") -or $c.NumberFormat -like "*#*")to
$f = $c.NumberFormatif ($f -like "0*" -or $f -like "*#*")I also use
$lastRow = $cell.SpecialCells(11, 1).Rowto get the last row number and change the loop towhile ($i -le $lastRow).$Excel.ScreenUpdating = Falsealso helped reduced some time.