Hi,
I have Performance Issues with the Excel spreadprocessing export. I just migrated an Excel export that used until now the libraries "Component One". The export takes now 30 seconds for 7221 rows and 88 columns, against 5 seconds before. Here is the code:
001. ... Fill the table...002. Using excBook As New Workbook003. 004. excBook.SuspendLayoutUpdate()005. Using (New UpdateScope(006. Function() (excBook.History.IsEnabled = False),007. Function() (excBook.History.IsEnabled = True)))008. 009. Dim excSheet As Worksheet 010. excSheet = excBook.Worksheets.Add()011. excSheet.Name = tbl.TableName012. fill_ExcelSheet(tbl, excSheet)013. 014. End Using015. excBook.ResumeLayoutUpdate()016. 017. 'Save the Excel-File018. Dim formatProvider As Telerik.Windows.Documents.Spreadsheet.FormatProviders.IWorkbookFormatProvider _019. = New Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider()020. 021. Using fsOutput As New FileStream(strFilename, FileMode.Create)022. formatProvider.Export(excBook, fsOutput)023. End Using024. 025. End Using026. 027.Private Shared Function fill_ExcelSheet(ByVal tblDaten As DataTable, ByVal excSheet As Worksheet) As Boolean028. Dim bolOK As Boolean = False029. Dim intCntCols As Integer, intCntRows As Integer030. Dim strColCaption As String031. 032. Try033. 034. 'Set Columns-Description035. For intCntCols = 0 To tblDaten.Columns.Count - 1036. strColCaption = get_AttributCaption(tblDaten.Prefix, tblDaten.Columns(intCntCols).ColumnName)037. excSheet.Cells.Item(0, intCntCols).SetValue(strColCaption)038. Next039. 040. 'Fill the Sheet with data041. excSheet.Cells.Item(1, 0, tblDaten.Rows.Count, tblDaten.Columns.Count).SetFormat(New CellValueFormat("@"))042. For intCntRows = 0 To tblDaten.Rows.Count - 1043. For intCntCols = 0 To tblDaten.Columns.Count - 1044. Dim colName As String = tblDaten.Columns(intCntCols).ColumnName045. If (tblDaten.Columns(colName).DataType Is GetType(Date)) Then046. If Not tblDaten.Rows(intCntRows).IsNull(colName) Then047. If CType(tblDaten.Rows(intCntRows).Item(colName), Date).TimeOfDay.Ticks > 0 Then048. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(CType(tblDaten.Rows(intCntRows).Item(colName), Date).ToString)049. Else050. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(CType(tblDaten.Rows(intCntRows).Item(colName), Date).ToShortDateString)051. End If052. End If053. ElseIf (tblDaten.Columns(colName).DataType Is GetType(System.Guid)) Then054. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(tblDaten.Rows(intCntRows).Item(colName).ToString)055. ElseIf (tblDaten.Columns(colName).DataType Is GetType(String)) Then056. 057. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(tblDaten.Rows(intCntRows).Item(colName).ToString)058. ElseIf (tblDaten.Columns(colName).DataType Is GetType(Integer)) _059. Or (tblDaten.Columns(colName).DataType Is GetType(Int16)) _060. Or (tblDaten.Columns(colName).DataType Is GetType(Single)) _061. Or (tblDaten.Columns(colName).DataType Is GetType(Decimal)) _062. Or (tblDaten.Columns(colName).DataType Is GetType(Double)) Then063. If Not tblDaten.Rows(intCntRows).IsNull(colName) Then064. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(CDbl(tblDaten.Rows(intCntRows).Item(colName)))065. End If066. ElseIf (tblDaten.Columns(colName).DataType Is GetType(Boolean)) Then067. If Not tblDaten.Rows(intCntRows).IsNull(colName) Then068. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(CBool(tblDaten.Rows(intCntRows).Item(colName)))069. End If070. Else071. If Not tblDaten.Rows(intCntRows).IsNull(colName) Then072. excSheet.Cells.Item(intCntRows + 1, intCntCols).SetValue(tblDaten.Rows(intCntRows).Item(colName))073. End If074. End If075. Next076. Application.DoEvents()077. Next078. 079. 'Print properties080. Dim pageSetup As Printing.WorksheetPageSetup = excSheet.WorksheetPageSetup081. pageSetup.PaperType = Telerik.Windows.Documents.Model.PaperTypes.A4082. pageSetup.PageOrientation = Telerik.Windows.Documents.Model.PageOrientation.Landscape083. Dim HeadFootSettings As Printing.HeaderFooterSettings = pageSetup.HeaderFooterSettings084. HeadFootSettings.Header.CenterSection.Text = excSheet.Name085. HeadFootSettings.Footer.CenterSection.Text = DateTime.Now.ToString086. 087. If excSheet.Workbook.Styles("NormalStyle") Is Nothing Then088. Dim normalStyleCells As Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellStyle = excSheet.Workbook.Styles.Add("NormalStyle")089. Dim borderDotCell As New CellBorder(CellBorderStyle.Hair, New ThemableColor(ThemeColorType.Text1))090. Dim borderThinCell As New CellBorder(CellBorderStyle.Thin, New ThemableColor(ThemeColorType.Text1))091. Dim fill As IFill = PatternFill.CreateSolidFill(New ThemableColor(Windows.Media.Colors.White))092. normalStyleCells.BeginUpdate()093. normalStyleCells.TopBorder = borderDotCell094. normalStyleCells.BottomBorder = borderDotCell095. normalStyleCells.LeftBorder = borderThinCell096. normalStyleCells.RightBorder = borderThinCell097. normalStyleCells.FontFamily = New ThemableFontFamily("Arial")098. normalStyleCells.FontSize = UnitHelper.PointToDip(8)099. normalStyleCells.Fill = fill100. normalStyleCells.EndUpdate()101. 102. Dim headerStyleCells As Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellStyle = excSheet.Workbook.Styles.Add("HeaderStyle")103. fill = PatternFill.CreateSolidFill(New ThemableColor(Windows.Media.Color.FromRgb(192, 192, 192)))104. headerStyleCells.CopyPropertiesFrom(normalStyleCells)105. headerStyleCells.BeginUpdate()106. headerStyleCells.IsWrapped = False107. headerStyleCells.TopBorder = borderThinCell108. headerStyleCells.BottomBorder = borderThinCell109. headerStyleCells.Fill = fill110. headerStyleCells.EndUpdate()111. 112. Dim footStyleCells As Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellStyle = excSheet.Workbook.Styles.Add("FooterStyle")113. footStyleCells.CopyPropertiesFrom(normalStyleCells)114. footStyleCells.BottomBorder = borderThinCell115. 116. Dim leftAlignStyleCells As Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellStyle = excSheet.Workbook.Styles.Add("LeftAlignStyle")117. leftAlignStyleCells.HorizontalAlignment = RadHorizontalAlignment.Left118. Dim rightAlignStyleCells As Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellStyle = excSheet.Workbook.Styles.Add("RightAlignStyle")119. rightAlignStyleCells.HorizontalAlignment = RadHorizontalAlignment.Right120. End If121. 122. excSheet.Columns(0, tblDaten.Columns.Count - 1).AutoFitWidth()123. Dim rowHeight As New RowHeight(UnitHelper.PointToDip(11.5), True)124. excSheet.Rows(0, tblDaten.Rows.Count).SetHeight(rowHeight)125. 126. 'Format cells127. For col As Integer = 0 To tblDaten.Columns.Count - 1128. 'Links- oder Rechtsbündig setzen129. Dim strAHorz As String = "LeftAlignStyle"130. Dim typCol As Type = tblDaten.Columns(col).DataType131. If (typCol Is GetType(Integer)) Or (typCol Is GetType(Single)) Or (typCol Is GetType(Decimal)) Or (typCol Is GetType(Double)) Then132. strAHorz = "RightAlignStyle"133. End If134. 135. excSheet.Cells(0, col).SetStyleName("HeaderStyle")136. excSheet.Cells(1, col, tblDaten.Rows.Count - 1, col).SetStyleName("NormalStyle")137. excSheet.Cells(tblDaten.Rows.Count, col).SetStyleName("FooterStyle")138. 139. excSheet.Cells(0, col, tblDaten.Rows.Count, col).SetStyleName(strAHorz)140. Next141. bolOK = True142. Catch ex As Exception143. show_AppErrorMsgBox(ex)144. End Try145. Return bolOK146.End Function
The code that takes the most time is:
- Lines 42 - 77: Fill the sheet with data: 12 seconds
- Line 122: AutoFitWidth(): 6 seconds --> (It's important, cannot be removed)
- Line 22: formatProvider.Export(excBook, fsOutput): 11 seconds --> That's very long !!!
Can you check my code and tell me if there are any improvements, please? So i can unfortunately not use the libraries.
Thanks in advance
