我有(班级)。具有1800个计数和每个对象具有90个属性。当我用90个属性花费越来越多的时间时。如何解决这个问题

 Dim cellIntStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
 cellIntStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#")

 Dim cellDateStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
 cellDateStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat(Format
                                                     ("dd-MMM-yyyy"))
 For Each mReport As Report In dtExcel
        row = sheet1.CreateRow(iRow)
        j = 0
        For Each prop As PropertyInfo In props
            Dim value As Object = prop.GetValue(mReport, Nothing)
            If IsInt(value) Then
                CreateRow(row, j, CType(value, Integer), cellIntStyle)
            ElseIf IsDate(value) Then
                CreateRow(row, j, String.Format("{0:dd-MMM-yyyy}", 
                                          value), cellDateStyle)
            Else
                CreateRow(row, j, value)
            End If
            j = j + 1
        Next

        iRow = iRow + 1 // Coming here taking so long... how to make it fast.
    Next



    Private Sub CreateRow(ByRef row As HSSFRow, ByVal colId As Integer, 
                                          ByVal value As String)
        row.CreateCell(colId).SetCellValue(value)
    End Sub
    Private Sub CreateRow(ByRef row As HSSFRow, ByVal colId As Integer, 
                                ByVal value As Integer, 
                                ByVal cellStyle As HSSFCellStyle)
        Dim cell As HSSFCell = row.CreateCell(colId)
        cell.SetCellValue(value)
        cell.CellStyle = cellStyle
    End Sub
    Private Sub CreateRow(ByRef row As HSSFRow, ByVal colId As Integer, 
                                                ByVal value As String, 
                                      ByVal cellStyle As HSSFCellStyle)
        Dim cell As HSSFCell = row.CreateCell(colId)
        cell.SetCellValue(value)
        cell.CellStyle = cellStyle
    End Sub
有帮助吗?

解决方案

从代码剪切中很难说出您的工作,但是使用PropertyInfo是一个很大的线索,即进行了一些反射(以及使用GetValue)。

此外,所有内容都是通过类型对象的值进行汇合,这是旧VB6变体的.NET等效物。

所有的打字都将使您付出代价。

取而代之的是,如果有办法将道具列表纳入某种已经键入的对象,因此您可以避免所有ISINT,ISDATE等,呼叫和GetValue呼叫,您应该看到速度相当不错的提高。

那是我先看的地方。

其他提示

除了“迭代更少的数据”之外,我在这里看不到简单的解决方案。您必须进行多少工作,例如(记录*fields_per_record);鉴于您提供的数字,您会卡住的内部循环〜162,000次。

购买更快的处理器并添加更多内存。

首先,您的代码是一团糟。凹痕尚不清楚, iRow 逐渐增加是在一个内部时在循环中 For Each 关于报告。我怀疑您一遍又一遍地迭代相同的数据。

其次,在每一行,您一遍又一遍地测试列具有哪些类型。现在,从那以后 props 未分配到列表内部,一个相当大的优化是删除 IsIntIsDate 通过以某种方式缓存他们拥有哪些类型的测试。

另外,如果您可以在对属性进行循环之前先对所有单元进行一次对所有单元格进行分配,那么您可能会获得良好的性能!

但是,正如其他人指出的那样, 没有 这将改变您的代码的复杂性。它将永远保持 O(number of rows * number of cells), ,这意味着当您将数据量加倍时,您应该期望将计算时间加倍。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top