Skip to content

Commit 945014c

Browse files
authored
Merge pull request #1643 from syncfusion-content/988804-ModifyStreams
988804-Modify file streams of C# [Cross-platform] in XlsIO UG
2 parents 644cc10 + 9f579ac commit 945014c

File tree

9 files changed

+69
-241
lines changed

9 files changed

+69
-241
lines changed

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Appearance.md

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2222
{
2323
IApplication application = excelEngine.Excel;
2424
application.DefaultVersion = ExcelVersion.Xlsx;
25-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
26-
IWorkbook workbook = application.Workbooks.Open(inputStream);
25+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
2726
IWorksheet worksheet = workbook.Worksheets[0];
2827
IChart chart = worksheet.Charts[0];
2928

@@ -50,13 +49,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
5049
chartFillImpl2.FillType = ExcelFillType.SolidColor;
5150
chartFillImpl2.ForeColor = Color.FromArgb(143, 170, 220); ;
5251

53-
//Saving the workbook as streams
54-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
55-
workbook.SaveAs(outputStream);
56-
57-
//Dispose streams
58-
outputStream.Dispose();
59-
inputStream.Dispose();
52+
//Saving the workbook
53+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
6054
}
6155
{% endhighlight %}
6256

@@ -147,8 +141,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
147141
{
148142
IApplication application = excelEngine.Excel;
149143
application.DefaultVersion = ExcelVersion.Xlsx;
150-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
151-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
144+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
152145
IWorksheet worksheet = workbook.Worksheets[0];
153146
IChart chart = worksheet.Charts[0];
154147

@@ -183,13 +176,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
183176
chartFillImpl2.ForeColor = Color.White;
184177
chartFillImpl2.Pattern = ExcelGradientPattern.Pat_5_Percent;
185178

186-
//Saving the workbook as stream
187-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
188-
workbook.SaveAs(outputStream);
189-
190-
//Dispose streams
191-
outputStream.Dispose();
192-
inputStream.Dispose();
179+
//Saving the workbook
180+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
193181
}
194182
{% endhighlight %}
195183

@@ -295,8 +283,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
295283
{
296284
IApplication application = excelEngine.Excel;
297285
application.DefaultVersion = ExcelVersion.Xlsx;
298-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
299-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
286+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
300287
IWorksheet worksheet = workbook.Worksheets[0];
301288
IChart chart = worksheet.Charts[0];
302289

@@ -337,13 +324,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
337324
chartFillImpl2.GradientStops.Add(gradientStopImpl3);
338325
chartFillImpl2.GradientStops.Add(gradientStopImpl4);
339326

340-
//Saving the workbook as stream
341-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
342-
workbook.SaveAs(outputStream);
343-
344-
//Dispose streams
345-
outputStream.Dispose();
346-
inputStream.Dispose();
327+
//Saving the workbook
328+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
347329
}
348330
{% endhighlight %}
349331

@@ -460,8 +442,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
460442
{
461443
IApplication application = excelEngine.Excel;
462444
application.DefaultVersion = ExcelVersion.Xlsx;
463-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
464-
IWorkbook workbook = application.Workbooks.Open(inputStream);
445+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
465446
IWorksheet worksheet = workbook.Worksheets[0];
466447
IChart chart = worksheet.Charts[0];
467448

@@ -493,15 +474,12 @@ using (ExcelEngine excelEngine = new ExcelEngine())
493474
serie1.SerieFormat.Fill.UserPicture(image2, "Image");
494475
serie2.SerieFormat.Fill.UserPicture(image2, "Image");
495476

496-
//Saving the workbook as stream
497-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
498-
workbook.SaveAs(outputStream);
477+
//Saving the workbook
478+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
499479

500480
//Dispose streams
501-
outputStream.Dispose();
502481
imageStream1.Dispose();
503482
imageStream2.Dispose();
504-
inputStream.Dispose();
505483
}
506484
{% endhighlight %}
507485

@@ -587,8 +565,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
587565
{
588566
IApplication application = excelEngine.Excel;
589567
application.DefaultVersion = ExcelVersion.Xlsx;
590-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
591-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
568+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"), ExcelOpenType.Automatic);
592569
IWorksheet worksheet = workbook.Worksheets[0];
593570
IChart chart = worksheet.Charts[0];
594571

@@ -607,13 +584,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
607584
//Set no fill to series
608585
serie1.SerieFormat.Fill.Visible = false;
609586

610-
//Saving the workbook as stream
611-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
612-
workbook.SaveAs(outputStream);
613-
614-
//Dispose streams
615-
outputStream.Dispose();
616-
inputStream.Dispose();
587+
//Saving the workbook
588+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
617589
}
618590
{% endhighlight %}
619591

@@ -979,12 +951,8 @@ The following code example illustrates how to apply 3D settings such as rotation
979951

980952
#region Save
981953
//Saving the workbook
982-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
983-
workbook.SaveAs(outputStream);
954+
workbook.SaveAs(Path.GetFullPath("Output/Chart.xlsx"));
984955
#endregion
985-
986-
//Dispose streams
987-
outputStream.Dispose();
988956
}
989957
{% endhighlight %}
990958

@@ -1118,8 +1086,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
11181086
{
11191087
IApplication application = excelEngine.Excel;
11201088
application.DefaultVersion = ExcelVersion.Xlsx;
1121-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
1122-
IWorkbook workbook = application.Workbooks.Open(inputStream);
1089+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
11231090
IWorksheet worksheet = workbook.Worksheets[0];
11241091

11251092
//Adding pie chart in the worksheet
@@ -1146,13 +1113,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
11461113

11471114
#region Save
11481115
//Saving the workbook
1149-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
1150-
workbook.SaveAs(outputStream);
1116+
workbook.SaveAs(Path.GetFullPath("Output/Chart.xlsx"));
11511117
#endregion
1152-
1153-
//Dispose streams
1154-
outputStream.Dispose();
1155-
inputStream.Dispose();
11561118
}
11571119
{% endhighlight %}
11581120

@@ -1218,8 +1180,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
12181180
{
12191181
IApplication application = excelEngine.Excel;
12201182
application.DefaultVersion = ExcelVersion.Xlsx;
1221-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
1222-
IWorkbook workbook = application.Workbooks.Open(inputStream);
1183+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
12231184
IWorksheet worksheet = workbook.Worksheets[0];
12241185

12251186
//Adding chart in the workbook
@@ -1244,13 +1205,10 @@ using (ExcelEngine excelEngine = new ExcelEngine())
12441205

12451206
#region Save
12461207
//Saving the workbook
1247-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
1248-
workbook.SaveAs(outputStream);
1208+
workbook.SaveAs(Path.GetFullPath("Output/Chart.xlsx"));
12491209
#endregion
12501210

12511211
//Dispose streams
1252-
outputStream.Dispose();
1253-
inputStream.Dispose();
12541212
imageStream.Dispose();
12551213
}
12561214
{% endhighlight %}
@@ -1371,12 +1329,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
13711329

13721330
#region Save
13731331
//Saving the workbook
1374-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
1375-
workbook.SaveAs(outputStream);
1332+
workbook.SaveAs(Path.GetFullPath("Output/Chart.xlsx"));
13761333
#endregion
1377-
1378-
//Dispose streams
1379-
outputStream.Dispose();
13801334
}
13811335
{% endhighlight %}
13821336

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Area.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
8686
{
8787
IApplication application = excelEngine.Excel;
8888
application.DefaultVersion = ExcelVersion.Xlsx;
89-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
90-
IWorkbook workbook = application.Workbooks.Open(inputStream);
89+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
9190
IWorksheet sheet = workbook.Worksheets[0];
9291
IChartShape chart = sheet.Charts[0];
9392

@@ -105,13 +104,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
105104
chartArea.Fill.BackColor = Color.FromArgb(205, 217, 234);
106105
chartArea.Fill.ForeColor = Color.White;
107106

108-
//Saving the workbook as stream
109-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
110-
workbook.SaveAs(outputStream);
111-
112-
//Dispose streams
113-
outputStream.Dispose();
114-
inputStream.Dispose();
107+
//Saving the workbook
108+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
115109
}
116110
{% endhighlight %}
117111

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Axis.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
341341
{
342342
IApplication application = excelEngine.Excel;
343343
application.DefaultVersion = ExcelVersion.Xlsx;
344-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
345-
IWorkbook workbook = application.Workbooks.Open(inputStream);
344+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
346345
IWorksheet sheet = workbook.Worksheets[0];
347346
IChartShape chart = sheet.Charts[0];
348347

@@ -410,13 +409,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
410409
//Hiding minor gridlines
411410
chart.PrimaryValueAxis.HasMinorGridLines = false;
412411

413-
//Saving the workbook as stream
414-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
415-
workbook.SaveAs(outputStream);
416-
417-
//Dispose streams
418-
outputStream.Dispose();
419-
inputStream.Dispose();
412+
//Saving the workbook
413+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
420414
}
421415
{% endhighlight %}
422416

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Data-Labels.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
158158
{
159159
IApplication application = excelEngine.Excel;
160160
application.DefaultVersion = ExcelVersion.Xlsx;
161-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
162-
IWorkbook workbook = application.Workbooks.Open(inputStream);
161+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
163162
IWorksheet worksheet = workbook.Worksheets[0];
164163
IChartShape chart = worksheet.Charts[0];
165164

@@ -191,13 +190,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
191190
IChartDataLabels dataLabel = chart.Series[0].DataPoints.DefaultDataPoint.DataLabels;
192191
(dataLabel as ChartDataLabelsImpl).NumberFormat = "#,##0.00";
193192

194-
//Saving the workbook as stream
195-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
196-
workbook.SaveAs(outputStream);
197-
198-
//Dispose streams
199-
outputStream.Dispose();
200-
inputStream.Dispose();
193+
//Saving the workbook
194+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
201195
}
202196
{% endhighlight %}
203197

@@ -345,12 +339,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
345339

346340
#region Save
347341
//Saving the workbook
348-
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
349-
workbook.SaveAs(outputStream);
342+
workbook.SaveAs("Output.xlsx");
350343
#endregion
351-
352-
//Dispose streams
353-
outputStream.Dispose();
354344
}
355345
{% endhighlight %}
356346

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Legend.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
228228
{
229229
IApplication application = excelEngine.Excel;
230230
application.DefaultVersion = ExcelVersion.Xlsx;
231-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
232-
IWorkbook workbook = application.Workbooks.Open(inputStream);
231+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
233232
IWorksheet worksheet = workbook.Worksheets[0];
234233
IChartShape chart = worksheet.Charts[0];
235234

@@ -264,11 +263,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
264263
//Set Legend without overlapping the chart
265264
chart.Legend.IncludeInLayout = true;
266265

267-
//Saving the workbook as stream
268-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
269-
workbook.SaveAs(outputStream);
270-
outputStream.Dispose();
271-
inputStream.Dispose();
266+
//Saving the workbook
267+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
272268
}
273269
{% endhighlight %}
274270

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Plot-Area.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
130130
{
131131
IApplication application = excelEngine.Excel;
132132
application.DefaultVersion = ExcelVersion.Xlsx;
133-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
134-
IWorkbook workbook = application.Workbooks.Open(inputStream);
133+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
135134
IWorksheet sheet = workbook.Worksheets[0];
136135
IChartShape chart = sheet.Charts[0];
137136

@@ -152,13 +151,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
152151
//Set the position
153152
chartPlotArea.Layout.Left = 5;
154153

155-
//Saving the workbook as stream
156-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
157-
workbook.SaveAs(outputStream);
158-
159-
//Dispose streams
160-
outputStream.Dispose();
161-
inputStream.Dispose();
154+
//Saving the workbook
155+
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
162156
}
163157
{% endhighlight %}
164158

Document-Processing/Excel/Excel-Library/NET/Charts/Chart-Series.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ using (ExcelEngine engine = new ExcelEngine())
151151
{
152152
IApplication application = engine.Excel;
153153
application.DefaultVersion = ExcelVersion.Xlsx;
154-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
155-
IWorkbook workbook = application.Workbooks.Open(inputStream);
154+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
156155
IWorksheet sheet = workbook.Worksheets[0];
157156

158157
//Create a Chart
@@ -185,13 +184,8 @@ using (ExcelEngine engine = new ExcelEngine())
185184

186185
#region Save
187186
//Saving the workbook
188-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Chart.xlsx"), FileMode.Create, FileAccess.Write);
189-
workbook.SaveAs(outputStream);
187+
workbook.SaveAs(Path.GetFullPath("Output/Chart.xlsx"));
190188
#endregion
191-
192-
//Dispose streams
193-
outputStream.Dispose();
194-
inputStream.Dispose();
195189
}
196190
{% endhighlight %}
197191

0 commit comments

Comments
 (0)