Skip to content

Commit 46993c0

Browse files
authored
Merge pull request #2051 from syncfusion-content/881697-DeleteRows-Performance
881697 - Add FAQ to resolve performance issue while deleting large number of rows
2 parents cb0e3ae + fd9b8ea commit 46993c0

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

File-Formats-toc.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,7 @@
16871687
<li><a href="/file-formats/xlsio/faqs/how-to-find-and-replace-text-in-hyperlinks">How to find and replace text in hyperlinks</a></li>
16881688
<li><a href="/file-formats/xlsio/faqs/how-to-fix-the-argument-out-of-range-exception-when-accessing-a-large-number-of-rows-and-columns">How to fix the ArgumentOutOfRangeException when accessing a large number of rows and columns?</a></li>
16891689
<li><a href="/file-formats/xlsio/faqs/how-to-set-logarithmic-axis-for-chart-in-excel-document">How to set Logarithmic axis for chart in Excel document?</a></li>
1690+
<li><a href="/file-formats/xlsio/faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows">How to resolve performance issue when deleting a large number of rows?</a></li>
16901691
</ul>
16911692
</li>
16921693
</ul>

File-Formats/XlsIO/FAQ.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ The frequently asked questions in Essential XlsIO are listed below.
7979
* [Why cone chart shows itself as column or bar chart?](faqs/why-cone-chart-shows-itself-as-colum-or-bar-chart)
8080
* [How to vary colors by point for line and column chart?](faqs/how-to-vary-colors-by-point-for-line-and-column-chart)
8181
* [How to upload a file to Azure blob and download as stream?](faqs/how-to-upload-a-file-to-azure-blob-and-download-as-stream)
82-
* [How to find and replace text in hyperlinks](faqs/how-to-find-and-replace-text-in-hyperlinks)
82+
* [How to find and replace text in hyperlinks?](faqs/how-to-find-and-replace-text-in-hyperlinks)
8383
* [How to fix the ArgumentOutOfRangeException when accessing a large number of rows and columns?](faqs/how-to-fix-the-argument-out-of-range-exception-when-accessing-a-large-number-of-rows-and-columns)
84-
* [How to set Logarithmic axis for chart in Excel document](faqs/how-to-set-logarithmic-axis-for-chart-in-excel-document)
84+
* [How to set Logarithmic axis for chart in Excel document?](faqs/how-to-set-logarithmic-axis-for-chart-in-excel-document)
85+
* [How to resolve performance issue when deleting a large number of rows?](faqs/how-to-resolve-performance-issue-when-deleting-a-large-number-of-rows)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)