|
| 1 | +--- |
| 2 | +title: Resolve performance issue while deleting |Syncfusion. |
| 3 | +description: This page explains how to resolve performance issue when deleting a large number of rows using Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: file-formats |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to resolve performance issue when deleting a large number of rows? |
| 10 | + |
| 11 | +To address the performance issue, rather than deleting the large number of blank rows using the DeleteRow method, copy the row containing values to a new worksheet and then delete the previous worksheet. |
| 12 | + |
| 13 | +The following code illustrates how to resolve performance issue when deleting a large number of rows. |
| 14 | + |
| 15 | +{% tabs %} |
| 16 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 17 | +using(ExcelEngine excelEngine = new ExcelEngine()) |
| 18 | +{ |
| 19 | + IApplication application = excelEngine.Excel; |
| 20 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 21 | + |
| 22 | + //Loads an existing file |
| 23 | + FileStream fileStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read); |
| 24 | + IWorkbook workbook = application.Workbooks.Open(fileStream); |
| 25 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 26 | + IWorksheet newWorksheet = workbook.Worksheets[1]; |
| 27 | + |
| 28 | + IRange usedRange = worksheet.UsedRange; |
| 29 | + int rowIndexSheet1 = 1; |
| 30 | + int rowIndexSheet2 = 1; |
| 31 | + foreach (IRange row in usedRange.Rows) |
| 32 | + { |
| 33 | + RowStorage rowStorage = WorksheetHelper.GetOrCreateRow(worksheet as IInternalWorksheet, rowIndexSheet1 - 1, false); |
| 34 | + if (rowStorage != null) |
| 35 | + { |
| 36 | + // Copy the Entire row to the next sheet |
| 37 | + IRange destinationRow = newWorksheet.Range[rowIndexSheet2, 1]; |
| 38 | + row.EntireRow.CopyTo(destinationRow); |
| 39 | + rowIndexSheet2++; |
| 40 | + } |
| 41 | + rowIndexSheet1++; |
| 42 | + } |
| 43 | + |
| 44 | + //Remove the worksheet |
| 45 | + workbook.Worksheets[0].Remove(); |
| 46 | + |
| 47 | + //Saving the workbook as stream |
| 48 | + FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite); |
| 49 | + workbook.SaveAs(stream); |
| 50 | + stream.Dispose(); |
| 51 | +} |
| 52 | +{% endhighlight %} |
| 53 | + |
| 54 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 55 | +using(ExcelEngine excelEngine = new ExcelEngine()) |
| 56 | +{ |
| 57 | + IApplication application = excelEngine.Excel; |
| 58 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 59 | + |
| 60 | + //Loads an existing file |
| 61 | + IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx"); |
| 62 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 63 | + IWorksheet newWorksheet = workbook.Worksheets[1]; |
| 64 | + |
| 65 | + IRange usedRange = worksheet.UsedRange; |
| 66 | + int rowIndexSheet1 = 1; |
| 67 | + int rowIndexSheet2 = 1; |
| 68 | + foreach (IRange row in usedRange.Rows) |
| 69 | + { |
| 70 | + RowStorage rowStorage = WorksheetHelper.GetOrCreateRow(worksheet as IInternalWorksheet, rowIndexSheet1 - 1, false); |
| 71 | + if (rowStorage != null) |
| 72 | + { |
| 73 | + // Copy the Entire row to the next sheet |
| 74 | + IRange destinationRow = newWorksheet.Range[rowIndexSheet2, 1]; |
| 75 | + row.EntireRow.CopyTo(destinationRow); |
| 76 | + rowIndexSheet2++; |
| 77 | + } |
| 78 | + rowIndexSheet1++; |
| 79 | + } |
| 80 | + |
| 81 | + //Remove the worksheet |
| 82 | + workbook.Worksheets[0].Remove(); |
| 83 | + |
| 84 | + //Saving the workbook as stream |
| 85 | + workbook.SaveAs("Output.xlsx"); |
| 86 | +} |
| 87 | +{% endhighlight %} |
| 88 | + |
| 89 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 90 | +Using excelEngine As New ExcelEngine() |
| 91 | + Dim application As IApplication = excelEngine.Excel |
| 92 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 93 | + |
| 94 | + ' Loads an existing file |
| 95 | + Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx") |
| 96 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 97 | + Dim newWorksheet As IWorksheet = workbook.Worksheets(1) |
| 98 | + |
| 99 | + Dim usedRange As IRange = worksheet.UsedRange |
| 100 | + Dim rowIndexSheet1 As Integer = 1 |
| 101 | + Dim rowIndexSheet2 As Integer = 1 |
| 102 | + For Each row As IRange In usedRange.Rows |
| 103 | + Dim rowStorage As RowStorage = WorksheetHelper.GetOrCreateRow(TryCast(worksheet, IInternalWorksheet), rowIndexSheet1 - 1, False) |
| 104 | + If rowStorage IsNot Nothing Then |
| 105 | + ' Copy the Entire row to the next sheet |
| 106 | + Dim destinationRow As IRange = newWorksheet.Range(rowIndexSheet2, 1) |
| 107 | + row.EntireRow.CopyTo(destinationRow) |
| 108 | + rowIndexSheet2 += 1 |
| 109 | + End If |
| 110 | + rowIndexSheet1 += 1 |
| 111 | + Next |
| 112 | + |
| 113 | + ' Remove the worksheet |
| 114 | + workbook.Worksheets(0).Remove() |
| 115 | + |
| 116 | + ' Saving the workbook |
| 117 | + workbook.SaveAs("Output.xlsx") |
| 118 | +End Using |
| 119 | +{% endhighlight %} |
| 120 | +{% endtabs %} |
0 commit comments