Skip to content

Commit 6f1c745

Browse files
authored
Merge pull request #1676 from syncfusion-content/988874-ModifyStreams1
UG documentation 988874: Modify file streams of C# [Cross-platform] in XlsIO UG - Working with Pivot Charts and What If Analysis
2 parents 9475b92 + fec4792 commit 6f1c745

File tree

2 files changed

+33
-78
lines changed

2 files changed

+33
-78
lines changed

Document-Processing/Excel/Excel-Library/NET/What-If-Analysis.md

Lines changed: 30 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2424
IApplication application = excelEngine.Excel;
2525
application.DefaultVersion = ExcelVersion.Xlsx;
2626

27-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), FileMode.Open, FileAccess.Read);
28-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
29-
inputStream.Dispose();
30-
27+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), ExcelOpenType.Automatic);
3128
IWorksheet worksheet = workbook.Worksheets[0];
3229

3330
// Access the collection of scenarios in the worksheet
@@ -51,12 +48,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
5148

5249
#region Save
5350
//Saving the workbook
54-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CreateScenarios.xlsx"), FileMode.Create, FileAccess.Write);
55-
workbook.SaveAs(outputStream);
51+
workbook.SaveAs(Path.GetFullPath("Output/CreateScenarios.xlsx"));
5652
#endregion
57-
58-
//Dispose streams
59-
outputStream.Dispose();
6053
}
6154
{% endhighlight %}
6255

@@ -138,8 +131,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
138131
{
139132
IApplication application = excelEngine.Excel;
140133
application.DefaultVersion = ExcelVersion.Xlsx;
141-
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
142-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
134+
135+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
143136
IWorksheet worksheet = workbook.Worksheets[0];
144137

145138
IScenarios scenarios = worksheet.Scenarios;
@@ -149,10 +142,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
149142
//Modify the scenario
150143
scenario1.ModifyScenario(scenario2.ChangingCells, scenario2.Values);
151144

152-
//Saving the workbook as stream
153-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
154-
workbook.SaveAs(stream);
155-
stream.Dispose();
145+
//Saving the workbook
146+
workbook.SaveAs("Output.xlsx");
156147
}
157148
{% endhighlight %}
158149

@@ -207,8 +198,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
207198
{
208199
IApplication application = excelEngine.Excel;
209200
application.DefaultVersion = ExcelVersion.Xlsx;
210-
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
211-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
201+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
212202
IWorksheet worksheet = workbook.Worksheets[0];
213203

214204
IScenarios scenarios = worksheet.Scenarios;
@@ -217,10 +207,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
217207
//Delete the scenario
218208
scenario1.Delete();
219209

220-
//Saving the workbook as stream
221-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
222-
workbook.SaveAs(stream);
223-
stream.Dispose();
210+
//Saving the workbook
211+
workbook.SaveAs("Output.xlsx");
224212
}
225213
{% endhighlight %}
226214

@@ -274,18 +262,15 @@ using (ExcelEngine excelEngine = new ExcelEngine())
274262
{
275263
IApplication application = excelEngine.Excel;
276264
application.DefaultVersion = ExcelVersion.Xlsx;
277-
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
278-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
265+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
279266
IWorksheet worksheet1 = workbook.Worksheets[0];
280267
IWorksheet worksheet2 = workbook.Worksheets[1];
281268

282269
//Merge the second worksheet scenario into first worksheet.
283270
worksheet1.Scenarios.Merge(worksheet2);
284271

285-
//Saving the workbook as stream
286-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
287-
workbook.SaveAs(stream);
288-
stream.Dispose();
272+
//Saving the workbook
273+
workbook.SaveAs("Output.xlsx");
289274
}
290275
{% endhighlight %}
291276

@@ -334,8 +319,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
334319
{
335320
IApplication application = excelEngine.Excel;
336321
application.DefaultVersion = ExcelVersion.Xlsx;
337-
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
338-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
322+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
339323
IWorksheet worksheet = workbook.Worksheets[0];
340324

341325
IScenarios scenarios = worksheet.Scenarios;
@@ -359,10 +343,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
359343
//Create Summary
360344
worksheet.Scenarios.CreateSummary(worksheet.Range["L7"]);
361345

362-
//Saving the workbook as stream
363-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
364-
workbook.SaveAs(stream);
365-
stream.Dispose();
346+
//Saving the workbook
347+
workbook.SaveAs("Output.xlsx");
366348
}
367349
{% endhighlight %}
368350

@@ -447,10 +429,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
447429
IApplication application = excelEngine.Excel;
448430
application.DefaultVersion = ExcelVersion.Xlsx;
449431

450-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), FileMode.Open, FileAccess.Read);
451-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
452-
inputStream.Dispose();
453-
432+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), ExcelOpenType.Automatic);
454433
IWorksheet worksheet = workbook.Worksheets[0];
455434

456435
//Access the collection of scenarios in the worksheet
@@ -467,12 +446,9 @@ using (ExcelEngine excelEngine = new ExcelEngine())
467446

468447
newSheet.Name = scenarios[pos].Name;
469448

470-
//Saving the new workbook as a stream
471-
using (FileStream stream = new FileStream(Path.GetFullPath(@"Output/" + scenarios[pos].Name + ".xlsx"), FileMode.Create, FileAccess.ReadWrite))
472-
{
473-
newBook.SaveAs(stream);
474-
}
475-
449+
//Saving the new workbook
450+
newBook.SaveAs(Path.GetFullPath(@"Output/" + scenarios[pos].Name + ".xlsx"));
451+
476452
//To restore the cell values from the previous scenario results
477453
scenarios["Current % of Change"].Show();
478454
scenarios["Current Quantity"].Show();
@@ -533,8 +509,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
533509
{
534510
IApplication application = excelEngine.Excel;
535511
application.DefaultVersion = ExcelVersion.Xlsx;
536-
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
537-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
512+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
538513
IWorksheet worksheet = workbook.Worksheets[0];
539514

540515
IScenarios scenarios = worksheet.Scenarios;
@@ -543,10 +518,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
543518
//Set the name of the scenario
544519
scenario.Name = "Current Quantity";
545520

546-
//Saving the workbook as stream
547-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
548-
workbook.SaveAs(stream);
549-
stream.Dispose();
521+
//Saving the workbook
522+
workbook.SaveAs("Output.xlsx");
550523
}
551524
{% endhighlight %}
552525

@@ -599,9 +572,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
599572
IApplication application = excelEngine.Excel;
600573
application.DefaultVersion = ExcelVersion.Xlsx;
601574

602-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), FileMode.Open, FileAccess.Read);
603-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
604-
inputStream.Dispose();
575+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), ExcelOpenType.Automatic);
605576

606577
IWorksheet worksheet = workbook.Worksheets[0];
607578

@@ -616,13 +587,9 @@ using (ExcelEngine excelEngine = new ExcelEngine())
616587

617588
#region Save
618589
//Saving the workbook
619-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/HideScenario.xlsx"), FileMode.Create, FileAccess.Write);
620-
workbook.SaveAs(outputStream);
590+
workbook.SaveAs(Path.GetFullPath("Output/HideScenario.xlsx"));
621591
#endregion
622592

623-
//Dispose streams
624-
outputStream.Dispose();
625-
626593
}
627594
{% endhighlight %}
628595

@@ -678,9 +645,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
678645
IApplication application = excelEngine.Excel;
679646
application.DefaultVersion = ExcelVersion.Xlsx;
680647

681-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), FileMode.Open, FileAccess.Read);
682-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
683-
inputStream.Dispose();
648+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/WhatIfAnalysisTemplate.xlsx"), ExcelOpenType.Automatic);
684649

685650
IWorksheet worksheet = workbook.Worksheets[0];
686651

@@ -695,13 +660,9 @@ using (ExcelEngine excelEngine = new ExcelEngine())
695660

696661
#region Save
697662
//Saving the workbook
698-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ProtectScenario.xlsx"), FileMode.Create, FileAccess.Write);
699-
workbook.SaveAs(outputStream);
663+
workbook.SaveAs(Path.GetFullPath("Output/ProtectScenario.xlsx"));
700664
#endregion
701665

702-
//Dispose streams
703-
outputStream.Dispose();
704-
705666
}
706667
{% endhighlight %}
707668

@@ -756,9 +717,8 @@ The comment associated with that particular scenario can be generated using the
756717
using (ExcelEngine excelEngine = new ExcelEngine())
757718
{
758719
IApplication application = excelEngine.Excel;
759-
application.DefaultVersion = ExcelVersion.Xlsx;
760-
FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
761-
IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
720+
application.DefaultVersion = ExcelVersion.Xlsx;
721+
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic);
762722
IWorksheet worksheet = workbook.Worksheets[0];
763723

764724
IScenarios scenarios = worksheet.Scenarios;
@@ -767,10 +727,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
767727
//Set the comment value
768728
scenario.Comment = "Scenario has been created";
769729

770-
//Saving the workbook as stream
771-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
772-
workbook.SaveAs(stream);
773-
stream.Dispose();
730+
//Saving the workbook
731+
workbook.SaveAs("Output.xlsx");
774732
}
775733
{% endhighlight %}
776734

Document-Processing/Excel/Excel-Library/NET/Working-with-Pivot-Charts.md

Lines changed: 3 additions & 6 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.Excel2013;
25-
FileStream fileStream = new FileStream("PivotTable.xlsx", FileMode.Open, FileAccess.Read);
26-
IWorkbook workbook = application.Workbooks.Open(fileStream);
25+
IWorkbook workbook = application.Workbooks.Open("PivotTable.xlsx");
2726
IWorksheet worksheet = workbook.Worksheets[0];
2827
IPivotTable pivotTable = worksheet.PivotTables[0];
2928

@@ -38,10 +37,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
3837

3938
string fileName = "PivotChart.xlsx";
4039

41-
//Saving the workbook as stream
42-
FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
43-
workbook.SaveAs(stream);
44-
stream.Dispose();
40+
//Saving the workbook
41+
workbook.SaveAs(fileName);
4542
}
4643
{% endhighlight %}
4744

0 commit comments

Comments
 (0)