Skip to content

Commit c410ebc

Browse files
authored
Merge pull request #1628 from syncfusion-content/988804-ModifyStreams
988804-Modify file streams of C# [Cross-platform] in XlsIO UG
2 parents d5af16c + 06628b3 commit c410ebc

File tree

9 files changed

+127
-653
lines changed

9 files changed

+127
-653
lines changed

Document-Processing/Excel/Excel-Library/NET/Cells-Manipulation/Filtering.md

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2727
{
2828
IApplication application = excelEngine.Excel;
2929
application.DefaultVersion = ExcelVersion.Xlsx;
30-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
31-
IWorkbook workbook = application.Workbooks.Open(inputStream);
30+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
3231
IWorksheet worksheet = workbook.Worksheets[0];
3332

3433
#region Filter
@@ -48,13 +47,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
4847

4948
#region Save
5049
//Saving the workbook
51-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Filter.xlsx"), FileMode.Create, FileAccess.Write);
52-
workbook.SaveAs(outputStream);
50+
workbook.SaveAs(Path.GetFullPath("Output/Filter.xlsx"));
5351
#endregion
54-
55-
//Dispose streams
56-
outputStream.Dispose();
57-
inputStream.Dispose();
5852
}
5953
{% endhighlight %}
6054

@@ -120,8 +114,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
120114
{
121115
IApplication application = excelEngine.Excel;
122116
application.DefaultVersion = ExcelVersion.Xlsx;
123-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
124-
IWorkbook workbook = application.Workbooks.Open(inputStream);
117+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
125118
IWorksheet worksheet = workbook.Worksheets[0];
126119

127120
#region Custom Filter
@@ -142,13 +135,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
142135

143136
#region Save
144137
//Saving the workbook
145-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CustomFilter.xlsx"), FileMode.Create, FileAccess.Write);
146-
workbook.SaveAs(outputStream);
138+
workbook.SaveAs(Path.GetFullPath("Output/CustomFilter.xlsx"));
147139
#endregion
148-
149-
//Dispose streams
150-
outputStream.Dispose();
151-
inputStream.Dispose();
152140
}
153141
{% endhighlight %}
154142

@@ -218,8 +206,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
218206
{
219207
IApplication application = excelEngine.Excel;
220208
application.DefaultVersion = ExcelVersion.Xlsx;
221-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
222-
IWorkbook workbook = application.Workbooks.Open(inputStream);
209+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
223210
IWorksheet worksheet = workbook.Worksheets[0];
224211

225212
#region Combination Filter
@@ -241,13 +228,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
241228

242229
#region Save
243230
//Saving the workbook
244-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CombinationFilter.xlsx"), FileMode.Create, FileAccess.Write);
245-
workbook.SaveAs(outputStream);
231+
workbook.SaveAs(Path.GetFullPath("Output/CombinationFilter.xlsx"));
246232
#endregion
247-
248-
//Dispose streams
249-
outputStream.Dispose();
250-
inputStream.Dispose();
251233
}
252234
{% endhighlight %}
253235

@@ -315,8 +297,7 @@ The following code example illustrates how to apply Dynamic filter.
315297
{
316298
IApplication application = excelEngine.Excel;
317299
application.DefaultVersion = ExcelVersion.Xlsx;
318-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
319-
IWorkbook workbook = application.Workbooks.Open(inputStream);
300+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
320301
IWorksheet worksheet = workbook.Worksheets[0];
321302

322303
#region Dynamic Filter
@@ -332,13 +313,8 @@ The following code example illustrates how to apply Dynamic filter.
332313

333314
#region Save
334315
//Saving the workbook
335-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/DynamicFilter.xlsx"), FileMode.Create, FileAccess.Write);
336-
workbook.SaveAs(outputStream);
316+
workbook.SaveAs(Path.GetFullPath("Output/DynamicFilter.xlsx"));
337317
#endregion
338-
339-
//Dispose streams
340-
outputStream.Dispose();
341-
inputStream.Dispose();
342318
}
343319
{% endhighlight %}
344320

@@ -398,8 +374,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
398374
{
399375
IApplication application = excelEngine.Excel;
400376
application.DefaultVersion = ExcelVersion.Xlsx;
401-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
402-
IWorkbook workbook = application.Workbooks.Open(inputStream);
377+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
403378
IWorksheet worksheet = workbook.Worksheets[0];
404379

405380
#region Cell Color Filter
@@ -415,13 +390,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
415390

416391
#region Save
417392
//Saving the workbook
418-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CellColorFilter.xlsx"), FileMode.Create, FileAccess.Write);
419-
workbook.SaveAs(outputStream);
393+
workbook.SaveAs(Path.GetFullPath("Output/CellColorFilter.xlsx"));
420394
#endregion
421-
422-
//Dispose streams
423-
outputStream.Dispose();
424-
inputStream.Dispose();
425395
}
426396
{% endhighlight %}
427397

@@ -479,8 +449,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
479449
{
480450
IApplication application = excelEngine.Excel;
481451
application.DefaultVersion = ExcelVersion.Xlsx;
482-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
483-
IWorkbook workbook = application.Workbooks.Open(inputStream);
452+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
484453
IWorksheet worksheet = workbook.Worksheets[0];
485454

486455
#region Font Color Filter
@@ -496,13 +465,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
496465

497466
#region Save
498467
//Saving the workbook
499-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/FontColorFilter.xlsx"), FileMode.Create, FileAccess.Write);
500-
workbook.SaveAs(outputStream);
468+
workbook.SaveAs(Path.GetFullPath("Output/FontColorFilter.xlsx"));
501469
#endregion
502-
503-
//Dispose streams
504-
outputStream.Dispose();
505-
inputStream.Dispose();
506470
}
507471
{% endhighlight %}
508472

@@ -562,8 +526,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
562526
{
563527
IApplication application = excelEngine.Excel;
564528
application.DefaultVersion = ExcelVersion.Xlsx;
565-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
566-
IWorkbook workbook = application.Workbooks.Open(inputStream);
529+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
567530
IWorksheet worksheet = workbook.Worksheets[0];
568531

569532
#region Icon Filter
@@ -579,13 +542,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
579542

580543
#region Save
581544
//Saving the workbook
582-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/IconFilter.xlsx"), FileMode.Create, FileAccess.Write);
583-
workbook.SaveAs(outputStream);
545+
workbook.SaveAs(Path.GetFullPath("Output/IconFilter.xlsx"));
584546
#endregion
585-
586-
//Dispose streams
587-
outputStream.Dispose();
588-
inputStream.Dispose();
589547
}
590548
{% endhighlight %}
591549

@@ -655,8 +613,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
655613
{
656614
IApplication application = excelEngine.Excel;
657615
application.DefaultVersion = ExcelVersion.Xlsx;
658-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
659-
IWorkbook workbook = application.Workbooks.Open(inputStream);
616+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
660617
IWorksheet worksheet = workbook.Worksheets[0];
661618

662619
#region Advanced Filter
@@ -670,13 +627,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
670627

671628
#region Save
672629
//Saving the workbook
673-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/AdvancedFilter.xlsx"), FileMode.Create, FileAccess.Write);
674-
workbook.SaveAs(outputStream);
630+
workbook.SaveAs(Path.GetFullPath("Output/AdvancedFilter.xlsx"));
675631
#endregion
676-
677-
//Dispose streams
678-
outputStream.Dispose();
679-
inputStream.Dispose();
680632
}
681633
{% endhighlight %}
682634

@@ -732,8 +684,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
732684
{
733685
IApplication application = excelEngine.Excel;
734686
application.DefaultVersion = ExcelVersion.Xlsx;
735-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
736-
IWorkbook workbook = application.Workbooks.Open(inputStream);
687+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
737688
IWorksheet worksheet = workbook.Worksheets[0];
738689

739690
#region Accessing Filter
@@ -781,8 +732,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
781732
}
782733
#endregion
783734

784-
//Dispose streams
785-
inputStream.Dispose();
735+
workbook.SaveAs("Output.xlsx");
786736
}
787737
{% endhighlight %}
788738

@@ -903,8 +853,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
903853
{
904854
IApplication application = excelEngine.Excel;
905855
application.DefaultVersion = ExcelVersion.Xlsx;
906-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.xlsx"), FileMode.Open, FileAccess.Read);
907-
IWorkbook workbook = application.Workbooks.Open(inputStream);
856+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx"));
908857
IWorksheet worksheet = workbook.Worksheets[0];
909858

910859
//Access sort fields from AutoFilters
@@ -931,13 +880,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
931880
sorter.Sort();
932881

933882
#region Save
934-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
935-
workbook.SaveAs(outputStream);
883+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
936884
#endregion
937-
938-
//Dispose streams
939-
outputStream.Dispose();
940-
inputStream.Dispose();
941885
}
942886
{% endhighlight %}
943887

Document-Processing/Excel/Excel-Library/NET/Cells-Manipulation/Find-and-Replace.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2828
{
2929
IApplication application = excelEngine.Excel;
3030
application.DefaultVersion = ExcelVersion.Xlsx;
31-
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
32-
IWorkbook workbook = application.Workbooks.Open(fileStream);
31+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
3332
IWorksheet worksheet = workbook.Worksheets[0];
3433

3534
//Searches for the given string within the text of worksheet
@@ -86,10 +85,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
8685
cell.CellStyle.Color = Syncfusion.Drawing.Color.FromArgb(255, 0, 128, 128);
8786
}
8887

89-
//Saving the workbook as stream
90-
FileStream stream = new FileStream(Path.GetFullPath(@"Output/Find.xlsx"), FileMode.Create, FileAccess.ReadWrite);
91-
workbook.SaveAs(stream);
92-
stream.Dispose();
88+
//Saving the workbook
89+
workbook.SaveAs(Path.GetFullPath(@"Output/Find.xlsx"));
9390
}
9491
{% endhighlight %}
9592

@@ -177,8 +174,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
177174
{
178175
IApplication application = excelEngine.Excel;
179176
application.DefaultVersion = ExcelVersion.Xlsx;
180-
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
181-
IWorkbook workbook = application.Workbooks.Open(fileStream);
177+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
182178
IWorksheet worksheet = workbook.Worksheets[0];
183179

184180
//Replaces the given string with another string
@@ -196,11 +192,9 @@ using (ExcelEngine excelEngine = new ExcelEngine())
196192
//Replaces the given string with Array
197193
worksheet.Replace("Central", new string[] { "Central", "East" }, true);
198194

199-
//Saving the workbook as stream
200-
FileStream stream = new FileStream(Path.GetFullPath("Output/Replace.xlsx"), FileMode.Create, FileAccess.ReadWrite);
195+
//Saving the workbook
201196
workbook.Version = ExcelVersion.Xlsx;
202-
workbook.SaveAs(stream);
203-
stream.Dispose();
197+
workbook.SaveAs(Path.GetFullPath("Output/Replace.xlsx"));
204198
}
205199
{% endhighlight %}
206200

0 commit comments

Comments
 (0)