2

我正在使用以下 Powershell 脚本来删除NumberFormat大量文件的列中的单元格,以便显示所有分数。该列可能有小数、文本或日期等;仅需要应用这些十进制/货币格式(格式为0*or *#*)的单元格

但是它很慢(每秒检查/更新两个或三个单元格)。有更好/更快的方法吗?

    $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++
    }

更新:

.NetMicrosoft.Office.Interop.Excel会快得多吗?或者将文件转换为 xlsx 格式并使用System.IO.Package.IO

4

1 回答 1

1

阅读评论后,我提高了速度。谢谢大家。

  1. 尽量减少细胞的访问。我删除了输出行"$i : $($c.NumberFormat) $($c.value2) "并更改

    if (($c.NumberFormat -like "0*") -or $c.NumberFormat -like "*#*")

    $f = $c.NumberFormat
    if ($f -like "0*" -or $f -like "*#*")

  2. 我还$lastRow = $cell.SpecialCells(11, 1).Row用来获取最后一行编号并将循环更改为while ($i -le $lastRow).

  3. $Excel.ScreenUpdating = False also helped reduced some time.
于 2013-01-17T17:56:06.030 回答