Skip to content

Commit 06628b3

Browse files
988804-ModifyStreams
1 parent c181147 commit 06628b3

File tree

2 files changed

+25
-88
lines changed

2 files changed

+25
-88
lines changed

Document-Processing/Excel/Excel-Library/NET/Import-Export/Export-from-Excel.md

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ 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

2928
//Read data from the worksheet and Export to the DataTable
30-
DataTable customersTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames | ExcelExportDataTableOptions.ComputedFormulaValues);
31-
32-
//Dispose streams
33-
inputStream.Dispose();
29+
DataTable customersTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames | ExcelExportDataTableOptions.ComputedFormulaValues);
3430
}
3531

3632
//XlsIO supports binding of exported data table to data grid in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms alone.
@@ -96,8 +92,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
9692
{
9793
IApplication application = excelEngine.Excel;
9894
application.DefaultVersion = ExcelVersion.Excel2016;
99-
FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
100-
IWorkbook workbook = application.Workbooks.Open(inputStream);
95+
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
10196
IWorksheet worksheet = workbook.Worksheets[0];
10297

10398
//Event to choose an action while exporting data from Excel to data table.
@@ -106,10 +101,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
106101
//Read data from the worksheet and Export to the DataTable
107102
DataTable customersTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
108103

109-
//Saving the workbook as stream
110-
FileStream stream = new FileStream("ExportToDT.xlsx", FileMode.Create, FileAccess.ReadWrite);
111-
workbook.SaveAs(stream);
112-
stream.Dispose();
104+
//Saving the workbook
105+
workbook.SaveAs("ExportToDT.xlsx");
113106
}
114107

115108
//XlsIO supports binding of exported data table to data grid in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms alone
@@ -223,15 +216,11 @@ using (ExcelEngine excelEngine = new ExcelEngine())
223216
{
224217
IApplication application = excelEngine.Excel;
225218
application.DefaultVersion = ExcelVersion.Xlsx;
226-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
227-
IWorkbook workbook = application.Workbooks.Open(inputStream);
219+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
228220
IWorksheet worksheet = workbook.Worksheets[0];
229221

230222
//Export worksheet data into Collection Objects
231223
List<Report> collectionObjects = worksheet.ExportData<Report>(1, 1, 10, 3);
232-
233-
//Dispose streams
234-
inputStream.Dispose();
235224
}
236225
{% endhighlight %}
237226

@@ -369,8 +358,7 @@ namespace Worksheet_to_Nested_Class
369358
{
370359
IApplication application = excelEngine.Excel;
371360
application.DefaultVersion = ExcelVersion.Xlsx;
372-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
373-
IWorkbook workbook = application.Workbooks.Open(inputStream);
361+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
374362
IWorksheet worksheet = workbook.Worksheets[0];
375363

376364
//Map column headers in worksheet with class properties.
@@ -383,9 +371,6 @@ namespace Worksheet_to_Nested_Class
383371

384372
//Export worksheet data into nested class Objects.
385373
List<Customer> nestedClassObjects = worksheet.ExportData<Customer>(1, 1, 10, 5, mappingProperties);
386-
387-
//Dispose streams
388-
inputStream.Dispose();
389374
}
390375
}
391376
}

Document-Processing/Excel/Excel-Library/NET/Import-Export/Import-to-Excel.md

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ N> XlsIO supports importing of data from data table to worksheet in Windows Form
3232

3333
#region Save
3434
//Saving the workbook
35-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportDataTable.xlsx"), FileMode.Create, FileAccess.Write);
36-
workbook.SaveAs(outputStream);
35+
workbook.SaveAs(Path.GetFullPath("Output/ImportDataTable.xlsx"));
3736
#endregion
38-
39-
//Dispose streams
40-
outputStream.Dispose();
4137
}
4238
{% endhighlight %}
4339

@@ -104,12 +100,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
104100

105101
#region Save
106102
//Saving the workbook
107-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportDataColumn.xlsx"), FileMode.Create, FileAccess.Write);
108-
workbook.SaveAs(outputStream);
103+
workbook.SaveAs(Path.GetFullPath("Output/ImportDataColumn.xlsx"));
109104
#endregion
110-
111-
//Dispose streams
112-
outputStream.Dispose();
113105
}
114106
{% endhighlight %}
115107

@@ -176,12 +168,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
176168

177169
#region Save
178170
//Saving the workbook
179-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportDataView.xlsx"), FileMode.Create, FileAccess.Write);
180-
workbook.SaveAs(outputStream);
171+
workbook.SaveAs(Path.GetFullPath("Output/ImportDataView.xlsx"));
181172
#endregion
182-
183-
//Dispose streams
184-
outputStream.Dispose();
185173
}
186174
{% endhighlight %}
187175

@@ -242,12 +230,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
242230

243231
#region Save
244232
//Saving the workbook
245-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportCollectionObjects.xlsx"), FileMode.Create, FileAccess.Write);
246-
workbook.SaveAs(outputStream);
233+
workbook.SaveAs(Path.GetFullPath("Output/ImportCollectionObjects.xlsx"));
247234
#endregion
248-
249-
//Dispose streams
250-
outputStream.Dispose();
251235
}
252236
{% endhighlight %}
253237

@@ -444,12 +428,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
444428

445429
#region Save
446430
//Saving the workbook
447-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportDataOptions.xlsx"), FileMode.Create, FileAccess.Write);
448-
workbook.SaveAs(outputStream);
431+
workbook.SaveAs(Path.GetFullPath("Output/ImportDataOptions.xlsx"));
449432
#endregion
450-
451-
//Dispose streams
452-
outputStream.Dispose();
453433
}
454434
{% endhighlight %}
455435

@@ -690,21 +670,16 @@ namespace Layout_Options
690670

691671
#region Save
692672
//Saving the workbook
693-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportData.xlsx"), FileMode.Create, FileAccess.Write);
694-
workbook.SaveAs(outputStream);
673+
workbook.SaveAs(Path.GetFullPath("Output/ImportData.xlsx"));
695674
#endregion
696-
697-
//Dispose streams
698-
outputStream.Dispose();
699675
}
700676
//Helper method to load data from XML file and add them in collections.
701677
private static IList<Brand> GetVehicleDetails()
702678
{
703679
XmlSerializer deserializer = new XmlSerializer(typeof(BrandObjects));
704680

705681
//Read data from XML file.
706-
FileStream stream = new FileStream(Path.GetFullPath(@"Data/ExportData.xml"), FileMode.Open, FileAccess.Read);
707-
TextReader textReader = new StreamReader(stream);
682+
TextReader textReader = new StreamReader(Path.GetFullPath(@"Data/ExportData.xml"));
708683
BrandObjects brands = (BrandObjects)deserializer.Deserialize(textReader);
709684

710685
//Initialize parent collection to add data from XML file.
@@ -1370,21 +1345,16 @@ namespace Grouping_Options
13701345

13711346
#region Save
13721347
//Saving the workbook
1373-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportData.xlsx"), FileMode.Create, FileAccess.Write);
1374-
workbook.SaveAs(outputStream);
1348+
workbook.SaveAs(Path.GetFullPath("Output/ImportData.xlsx"));
13751349
#endregion
1376-
1377-
//Dispose streams
1378-
outputStream.Dispose();
13791350
}
13801351
//Helper method to load data from XML file and add them in collections.
13811352
private static IList<Brand> GetVehicleDetails()
13821353
{
13831354
XmlSerializer deserializer = new XmlSerializer(typeof(BrandObjects));
13841355

1385-
//Read data from XML file.
1386-
FileStream stream = new FileStream(Path.GetFullPath(@"Data/ExportData.xml"), FileMode.Open, FileAccess.Read);
1387-
TextReader textReader = new StreamReader(stream);
1356+
//Read data from XML file.
1357+
TextReader textReader = new StreamReader(Path.GetFullPath(@"Data/ExportData.xml"));
13881358
BrandObjects brands = (BrandObjects)deserializer.Deserialize(textReader);
13891359

13901360
//Initialize parent collection to add data from XML file.
@@ -1967,12 +1937,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
19671937

19681938
#region Save
19691939
//Saving the workbook
1970-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ImportData.xlsx"), FileMode.Create, FileAccess.Write);
1971-
workbook.SaveAs(outputStream);
1940+
workbook.SaveAs(Path.GetFullPath("Output/ImportData.xlsx"));
19721941
#endregion
1973-
1974-
//Dispose streams
1975-
outputStream.Dispose();
19761942
}
19771943
{% endhighlight %}
19781944

@@ -2206,12 +2172,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
22062172

22072173
#region Save
22082174
//Saving the workbook
2209-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ArrayToWorksheet.xlsx"), FileMode.Create, FileAccess.Write);
2210-
workbook.SaveAs(outputStream);
2175+
workbook.SaveAs(Path.GetFullPath("Output/ArrayToWorksheet.xlsx"));
22112176
#endregion
2212-
2213-
//Dispose streams
2214-
outputStream.Dispose();
22152177
}
22162178
{% endhighlight %}
22172179

@@ -2440,18 +2402,12 @@ namespace ImportHtml
24402402
IWorksheet worksheet = workbook.Worksheets[0];
24412403

24422404
//Imports HTML table into the worksheet from first row and first column
2443-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.html"), FileMode.Open, FileAccess.ReadWrite);
2444-
worksheet.ImportHtmlTable(inputStream, 1, 1);
2405+
worksheet.ImportHtmlTable(Path.GetFullPath(@"Data/InputTemplate.html"), 1, 1);
24452406

24462407
#region Save
24472408
//Saving the workbook
2448-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/HTMLTabletoWorksheet.xlsx"), FileMode.Create, FileAccess.Write);
2449-
workbook.SaveAs(outputStream);
2409+
workbook.SaveAs(Path.GetFullPath("Output/HTMLTabletoWorksheet.xlsx"));
24502410
#endregion
2451-
2452-
//Dispose streams
2453-
outputStream.Dispose();
2454-
inputStream.Dispose();
24552411
}
24562412
}
24572413
}
@@ -2556,13 +2512,11 @@ using (ExcelEngine excelEngine = new ExcelEngine())
25562512
FileStream inputStream = new FileStream("../../../Data/XmlFile.xml", FileMode.Open, FileAccess.Read);
25572513
worksheet.ImportXml(inputStream, 1, 6);
25582514

2559-
//Saving the workbook as stream
2560-
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
2561-
workbook.SaveAs(outputStream);
2515+
//Saving the workbook
2516+
workbook.SaveAs("Output.xlsx");
25622517

25632518
//Dispose stream
25642519
inputStream.Dispose();
2565-
outputStream.Dispose();
25662520
}
25672521
{% endhighlight %}
25682522

@@ -2617,13 +2571,11 @@ using (ExcelEngine excelEngine = new ExcelEngine())
26172571
FileStream inputStream = new FileStream("../../../Data/XmlFile.xml", FileMode.Open, FileAccess.Read);
26182572
workbook.XmlMaps.Add(inputStream);
26192573

2620-
//Saving the workbook as stream
2621-
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
2622-
workbook.SaveAs(outputStream);
2574+
//Saving the workbook
2575+
workbook.SaveAs("Output.xlsx");
26232576

26242577
//Dispose stream
26252578
inputStream.Dispose();
2626-
outputStream.Dispose();
26272579
}
26282580
{% endhighlight %}
26292581

0 commit comments

Comments
 (0)