Skip to content

Commit 1da5fb7

Browse files
authored
Merge pull request #1639 from syncfusion-content/988804-ModifyStreams
988804-Modify file streams of C# [Cross-platform] in XlsIO UG
2 parents c410ebc + 0d1429e commit 1da5fb7

File tree

6 files changed

+25
-99
lines changed

6 files changed

+25
-99
lines changed

Document-Processing/Excel/Excel-Library/NET/Conditional-Formatting/Color-Scales.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
1818
{
1919
IApplication application = excelEngine.Excel;
2020
application.DefaultVersion = ExcelVersion.Xlsx;
21-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
22-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
21+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
2322
IWorksheet worksheet = workbook.Worksheets[0];
2423

2524
//Create color scales for the data in specified range
@@ -42,13 +41,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
4241

4342
#region Save
4443
//Saving the workbook
45-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
46-
workbook.SaveAs(outputStream);
44+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
4745
#endregion
48-
49-
//Dispose streams
50-
outputStream.Dispose();
51-
inputStream.Dispose();
5246
}
5347
{% endhighlight %}
5448

Document-Processing/Excel/Excel-Library/NET/Conditional-Formatting/Data-Bars.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
1818
{
1919
IApplication application = excelEngine.Excel;
2020
application.DefaultVersion = ExcelVersion.Xlsx;
21-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
22-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
21+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
2322
IWorksheet worksheet = workbook.Worksheets[0];
2423

2524
//Create data bars for the data in specified range
@@ -36,13 +35,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
3635

3736
#region Save
3837
//Saving the workbook
39-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
40-
workbook.SaveAs(outputStream);
38+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
4139
#endregion
42-
43-
//Dispose streams
44-
outputStream.Dispose();
45-
inputStream.Dispose();
4640
}
4741
{% endhighlight %}
4842

Document-Processing/Excel/Excel-Library/NET/Conditional-Formatting/Highlight-Cells.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
9090

9191
#region Save
9292
//Saving the workbook
93-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/UniqueandDuplicate.xlsx"), FileMode.Create, FileAccess.Write);
94-
workbook.SaveAs(outputStream);
93+
workbook.SaveAs(Path.GetFullPath("Output/UniqueandDuplicate.xlsx"));
9594
#endregion
96-
97-
//Dispose streams
98-
outputStream.Dispose();
9995
}
10096
{% endhighlight %}
10197

Document-Processing/Excel/Excel-Library/NET/Conditional-Formatting/Icon-Sets.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
1818
{
1919
IApplication application = excelEngine.Excel;
2020
application.DefaultVersion = ExcelVersion.Xlsx;
21-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
22-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
21+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
2322
IWorksheet worksheet = workbook.Worksheets[0];
2423

2524
//Create icon sets for the data in specified range
@@ -38,13 +37,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
3837

3938
#region Save
4039
//Saving the workbook
41-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
42-
workbook.SaveAs(outputStream);
40+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
4341
#endregion
44-
45-
//Dispose streams
46-
outputStream.Dispose();
47-
inputStream.Dispose();
4842
}
4943
{% endhighlight %}
5044

Document-Processing/Excel/Excel-Library/NET/Conditional-Formatting/Top-Bottom.md

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
3030
{
3131
IApplication application = excelEngine.Excel;
3232
application.DefaultVersion = ExcelVersion.Xlsx;
33-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
34-
IWorkbook workbook = application.Workbooks.Open(inputStream);
33+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
3534
IWorksheet worksheet = workbook.Worksheets[0];
3635

3736
//Applying conditional formatting to "N6:N35".
@@ -53,13 +52,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
5352

5453
#region Save
5554
//Saving the workbook
56-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/TopToBottomRank.xlsx"), FileMode.Create, FileAccess.Write);
57-
workbook.SaveAs(outputStream);
55+
workbook.SaveAs(Path.GetFullPath("Output/TopToBottomRank.xlsx"));
5856
#endregion
59-
60-
//Dispose streams
61-
outputStream.Dispose();
62-
inputStream.Dispose();
6357
}
6458
{% endhighlight %}
6559

@@ -189,8 +183,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
189183
{
190184
IApplication application = excelEngine.Excel;
191185
application.DefaultVersion = ExcelVersion.Xlsx;
192-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
193-
IWorkbook workbook = application.Workbooks.Open(inputStream);
186+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
194187
IWorksheet worksheet = workbook.Worksheets[0];
195188

196189
//Applying conditional formatting to "N6:N35".
@@ -215,13 +208,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
215208

216209
#region Save
217210
//Saving the workbook
218-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
219-
workbook.SaveAs(outputStream);
211+
workbook.SaveAs(Path.GetFullPath("Output/Chart.xlsx"));
220212
#endregion
221-
222-
//Dispose streams
223-
outputStream.Dispose();
224-
inputStream.Dispose();
225213
}
226214
{% endhighlight %}
227215

@@ -366,8 +354,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
366354
{
367355
IApplication application = excelEngine.Excel;
368356
application.DefaultVersion = ExcelVersion.Xlsx;
369-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
370-
IWorkbook workbook = application.Workbooks.Open(inputStream);
357+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
371358
IWorksheet worksheet = workbook.Worksheets[0];
372359

373360
//Applying conditional formatting to "M6:M35"
@@ -387,13 +374,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
387374

388375
#region Save
389376
//Saving the workbook
390-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/AboveAndBelowAverage.xlsx"), FileMode.Create, FileAccess.Write);
391-
workbook.SaveAs(outputStream);
377+
workbook.SaveAs(Path.GetFullPath("Output/AboveAndBelowAverage.xlsx"));
392378
#endregion
393-
394-
//Dispose streams
395-
outputStream.Dispose();
396-
inputStream.Dispose();
397379
}
398380
{% endhighlight %}
399381

@@ -469,8 +451,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
469451
{
470452
IApplication application = excelEngine.Excel;
471453
application.DefaultVersion = ExcelVersion.Xlsx;
472-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
473-
IWorkbook workbook = application.Workbooks.Open(inputStream);
454+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
474455
IWorksheet worksheet = workbook.Worksheets[0];
475456

476457
//Applying conditional formatting to "M6:M35"
@@ -493,13 +474,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
493474

494475
#region Save
495476
//Saving the workbook
496-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/AboveAndBelowStandardDeviation.xlsx"), FileMode.Create, FileAccess.Write);
497-
workbook.SaveAs(outputStream);
477+
workbook.SaveAs(Path.GetFullPath("Output/AboveAndBelowStandardDeviation.xlsx"));
498478
#endregion
499-
500-
//Dispose streams
501-
outputStream.Dispose();
502-
inputStream.Dispose();
503479
}
504480
{% endhighlight %}
505481

Document-Processing/Excel/Excel-Library/NET/Working-with-Conditional-Formatting.md

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
150150

151151
#region Save
152152
//Saving the workbook
153-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ConditionalFormat.xlsx"), FileMode.Create, FileAccess.Write);
154-
workbook.SaveAs(outputStream);
153+
workbook.SaveAs(Path.GetFullPath("Output/ConditionalFormat.xlsx"));
155154
#endregion
156-
157-
//Dispose streams
158-
outputStream.Dispose();
159155
}
160156
{% endhighlight %}
161157

@@ -297,16 +293,14 @@ using (ExcelEngine excelEngine = new ExcelEngine())
297293
{
298294
IApplication application = excelEngine.Excel;
299295
application.DefaultVersion = ExcelVersion.Xlsx;
300-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
301-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
296+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
302297
IWorksheet worksheet = workbook.Worksheets[0];
303298

304299
//Read conditional formatting settings
305300
string formatType = worksheet.Range["A1"].ConditionalFormats[0].FormatType.ToString();
306301
string cfOperator = worksheet.Range["A1"].ConditionalFormats[0].Operator.ToString();
307302

308-
//Dispose streams
309-
inputStream.Dispose();
303+
workbook.SaveAs("Output.xlsx");
310304
}
311305
{% endhighlight %}
312306

@@ -359,22 +353,16 @@ using (ExcelEngine excelEngine = new ExcelEngine())
359353
IApplication application = excelEngine.Excel;
360354
application.DefaultVersion = ExcelVersion.Xlsx;
361355

362-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
363-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
356+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
364357
IWorksheet worksheet = workbook.Worksheets[0];
365358

366359
//Removing conditional format for a specified range
367360
worksheet.Range["E5"].ConditionalFormats.Remove();
368361

369362
#region Save
370363
//Saving the workbook
371-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/RemoveConditionalFormat.xlsx"), FileMode.Create, FileAccess.Write);
372-
workbook.SaveAs(outputStream);
364+
workbook.SaveAs(Path.GetFullPath("Output/RemoveConditionalFormat.xlsx"));
373365
#endregion
374-
375-
//Dispose streams
376-
outputStream.Dispose();
377-
inputStream.Dispose();
378366
}
379367
{% endhighlight %}
380368

@@ -422,22 +410,16 @@ using (ExcelEngine excelEngine = new ExcelEngine())
422410
{
423411
IApplication application = excelEngine.Excel;
424412
application.DefaultVersion = ExcelVersion.Xlsx;
425-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
426-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
413+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
427414
IWorksheet worksheet = workbook.Worksheets[0];
428415

429416
//Removing first conditional Format at the specified Range
430417
worksheet.UsedRange.ConditionalFormats.RemoveAt(0);
431418

432419
#region Save
433420
//Saving the workbook
434-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/RemoveConditionalFormat.xlsx"), FileMode.Create, FileAccess.Write);
435-
workbook.SaveAs(outputStream);
421+
workbook.SaveAs(Path.GetFullPath("Output/RemoveConditionalFormat.xlsx"));
436422
#endregion
437-
438-
//Dispose streams
439-
outputStream.Dispose();
440-
inputStream.Dispose();
441423
}
442424
{% endhighlight %}
443425

@@ -483,22 +465,16 @@ using (ExcelEngine excelEngine = new ExcelEngine())
483465
{
484466
IApplication application = excelEngine.Excel;
485467
application.DefaultVersion = ExcelVersion.Xlsx;
486-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
487-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
468+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
488469
IWorksheet worksheet = workbook.Worksheets[0];
489470

490471
//Removing Conditional Formatting Settings From Entire Sheet
491472
worksheet.UsedRange.Clear(ExcelClearOptions.ClearConditionalFormats);
492473

493474
#region Save
494475
//Saving the workbook
495-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/RemoveAll.xlsx"), FileMode.Create, FileAccess.Write);
496-
workbook.SaveAs(outputStream);
476+
workbook.SaveAs(Path.GetFullPath("Output/RemoveAll.xlsx"));
497477
#endregion
498-
499-
//Dispose streams
500-
outputStream.Dispose();
501-
inputStream.Dispose();
502478
}
503479
{% endhighlight %}
504480

@@ -557,12 +533,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
557533

558534
#region Save
559535
//Saving the workbook
560-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ConditionalFormat.xlsx"), FileMode.Create, FileAccess.Write);
561-
workbook.SaveAs(outputStream);
536+
workbook.SaveAs(Path.GetFullPath("Output/ConditionalFormat.xlsx"));
562537
#endregion
563-
564-
//Dispose streams
565-
outputStream.Dispose();
566538
}
567539
{% endhighlight %}
568540

0 commit comments

Comments
 (0)