Skip to content

Commit 60aa0ec

Browse files
988804-ModifyStreams
1 parent c95e299 commit 60aa0ec

File tree

1 file changed

+25
-71
lines changed

1 file changed

+25
-71
lines changed

Document-Processing/Excel/Excel-Library/NET/Working-with-Excel-Tables.md

Lines changed: 25 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,16 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2626
{
2727
IApplication application = excelEngine.Excel;
2828
application.DefaultVersion = ExcelVersion.Xlsx;
29-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
30-
IWorkbook workbook = application.Workbooks.Open(inputStream);
29+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
3130
IWorksheet worksheet = workbook.Worksheets[0];
3231

3332
//Create for the given data
3433
IListObject table = worksheet.ListObjects.Create("Table1", worksheet["A1:C5"]);
3534

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

@@ -89,8 +83,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
8983
{
9084
IApplication application = excelEngine.Excel;
9185
application.DefaultVersion = ExcelVersion.Xlsx;
92-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
93-
IWorkbook workbook = application.Workbooks.Open(inputStream);
86+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
9487
IWorksheet worksheet = workbook.Worksheets[0];
9588

9689
//Accessing first table in the sheet
@@ -101,13 +94,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
10194

10295
#region Save
10396
//Saving the workbook
104-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ReadTable.xlsx"), FileMode.Create, FileAccess.Write);
105-
workbook.SaveAs(outputStream);
97+
workbook.SaveAs(Path.GetFullPath("Output/ReadTable.xlsx"));
10698
#endregion
107-
108-
//Dispose streams
109-
inputStream.Dispose();
110-
outputStream.Dispose();
11199
}
112100
{% endhighlight %}
113101

@@ -163,8 +151,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
163151
{
164152
IApplication application = excelEngine.Excel;
165153
application.DefaultVersion = ExcelVersion.Xlsx;
166-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
167-
IWorkbook workbook = application.Workbooks.Open(inputStream);
154+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
168155
IWorksheet worksheet = workbook.Worksheets[0];
169156

170157
//Creating a table
@@ -175,13 +162,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
175162

176163
#region Save
177164
//Saving the workbook
178-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/FormatTable.xlsx"), FileMode.Create, FileAccess.Write);
179-
workbook.SaveAs(outputStream);
165+
workbook.SaveAs(Path.GetFullPath("Output/FormatTable.xlsx"));
180166
#endregion
181-
182-
//Dispose streams
183-
inputStream.Dispose();
184-
outputStream.Dispose();
185167
}
186168
{% endhighlight %}
187169

@@ -323,12 +305,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
323305

324306
#region Save
325307
//Saving the workbook
326-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/CustomTableStyle.xlsx"), FileMode.Create, FileAccess.Write);
327-
workbook.SaveAs(outputStream);
308+
workbook.SaveAs(Path.GetFullPath("Output/CustomTableStyle.xlsx"));
328309
#endregion
329-
330-
//Dispose streams
331-
outputStream.Dispose();
332310
}
333311
{% endhighlight %}
334312

@@ -540,8 +518,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
540518
{
541519
IApplication application = excelEngine.Excel;
542520
application.DefaultVersion = ExcelVersion.Xlsx;
543-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
544-
IWorkbook workbook = application.Workbooks.Open(inputStream);
521+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
545522
IWorksheet worksheet = workbook.Worksheets[0];
546523

547524
//Creating a table
@@ -552,13 +529,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
552529

553530
#region Save
554531
//Saving the workbook
555-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/InsertTableColumn.xlsx"), FileMode.Create, FileAccess.Write);
556-
workbook.SaveAs(outputStream);
532+
workbook.SaveAs(Path.GetFullPath("Output/InsertTableColumn.xlsx"));
557533
#endregion
558-
559-
//Dispose streams
560-
inputStream.Dispose();
561-
outputStream.Dispose();
562534
}
563535
{% endhighlight %}
564536

@@ -622,8 +594,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
622594
{
623595
IApplication application = excelEngine.Excel;
624596
application.DefaultVersion = ExcelVersion.Xlsx;
625-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
626-
IWorkbook workbook = application.Workbooks.Open(inputStream);
597+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
627598
IWorksheet worksheet = workbook.Worksheets[0];
628599

629600
//Creating a table
@@ -637,13 +608,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
637608

638609
#region Save
639610
//Saving the workbook
640-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/AddTotalRow.xlsx"), FileMode.Create, FileAccess.Write);
641-
workbook.SaveAs(outputStream);
611+
workbook.SaveAs(Path.GetFullPath("Output/AddTotalRow.xlsx"));
642612
#endregion
643-
644-
//Dispose streams
645-
inputStream.Dispose();
646-
outputStream.Dispose();
647613
}
648614
{% endhighlight %}
649615

@@ -742,9 +708,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
742708
//Auto-fits the columns
743709
worksheet.UsedRange.AutofitColumns();
744710

745-
//Saving the workbook as stream
746-
FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write);
747-
workbook.SaveAs(stream);
711+
//Saving the workbook
712+
workbook.SaveAs("Output.xlsx");
748713
}
749714

750715
//Helper method to get data table using OLEDB connection
@@ -911,8 +876,7 @@ using (ExcelEngine excelEngine = new ExcelEngine())
911876
{
912877
IApplication application = excelEngine.Excel;
913878
application.DefaultVersion = ExcelVersion.Excel2016;
914-
FileStream inputStream = new FileStream("ExistingDataSource.xlsx", FileMode.Open, FileAccess.Read);
915-
IWorkbook workbook = application.Workbooks.Open(inputStream);
879+
IWorkbook workbook = application.Workbooks.Open("ExistingDataSource.xlsx");
916880
IWorksheet worksheet = workbook.Worksheets[0];
917881

918882
//Accessing a connection from the workbook
@@ -924,9 +888,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
924888
listObject.Refresh();
925889
}
926890

927-
//Saving the workbook as stream
928-
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write);
929-
workbook.SaveAs(outputStream);
891+
//Saving the workbook
892+
workbook.SaveAs("Output.xlsx");
930893
}
931894
{% endhighlight %}
932895

@@ -988,8 +951,7 @@ The following code example illustrates how to set parameter through **prompt** e
988951
using (ExcelEngine excelEngine = new ExcelEngine())
989952
{
990953
IApplication application = excelEngine.Excel;
991-
FileStream fileStream = new FileStream("QueryTable.xlsx", FileMode.Open, FileAccess.Read);
992-
IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
954+
IWorkbook workbook = application.Workbooks.Open("QueryTable.xlsx", ExcelOpenType.Automatic);
993955
IWorksheet worksheet = workbook.Worksheets[0];
994956

995957
///Get query table from list objects.
@@ -1004,10 +966,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
1004966
//Set parameter value through prompt.
1005967
parameter.SetParam(ExcelParameterType.Prompt, "Prompt");
1006968

1007-
//Saving the workbook as stream
1008-
FileStream stream = new FileStream("PromptParameter.xlsx", FileMode.Create, FileAccess.ReadWrite);
1009-
workbook.SaveAs(stream);
1010-
stream.Dispose();
969+
//Saving the workbook
970+
workbook.SaveAs("PromptParameter.xlsx");
1011971
}
1012972
{% endhighlight %}
1013973

@@ -1089,8 +1049,7 @@ The following code example illustrates how to set parameter through **constant**
10891049
using (ExcelEngine excelEngine = new ExcelEngine())
10901050
{
10911051
IApplication application = excelEngine.Excel;
1092-
FileStream fileStream = new FileStream("QueryTable.xlsx", FileMode.Open, FileAccess.Read);
1093-
IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
1052+
IWorkbook workbook = application.Workbooks.Open("QueryTable.xlsx", ExcelOpenType.Automatic);
10941053
IWorksheet worksheet = workbook.Worksheets[0];
10951054

10961055
///Get query table from list objects.
@@ -1105,10 +1064,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
11051064
//Set constant to the parameter value.
11061065
parameter.SetParam(ExcelParameterType.Constant, 30);
11071066

1108-
//Saving the workbook as stream
1109-
FileStream stream = new FileStream("ConstantParameter.xlsx", FileMode.Create, FileAccess.ReadWrite);
1110-
workbook.SaveAs(stream);
1111-
stream.Dispose();
1067+
//Saving the workbook
1068+
workbook.SaveAs("ConstantParameter.xlsx");
11121069
}
11131070
{% endhighlight %}
11141071

@@ -1175,8 +1132,7 @@ The following code example illustrates how to set parameter type to a specific *
11751132
using (ExcelEngine excelEngine = new ExcelEngine())
11761133
{
11771134
IApplication application = excelEngine.Excel;
1178-
FileStream fileStream = new FileStream("QueryTable.xlsx", FileMode.Open, FileAccess.Read);
1179-
IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
1135+
IWorkbook workbook = application.Workbooks.Open("QueryTable.xlsx", ExcelOpenType.Automatic);
11801136
IWorksheet worksheet = workbook.Worksheets[0];
11811137

11821138
//Get query table from list objects.
@@ -1191,10 +1147,8 @@ using (ExcelEngine excelEngine = new ExcelEngine())
11911147
//Set range to the parameter value.
11921148
parameter.SetParam(ExcelParameterType.Range, worksheet.Range["H1"]);
11931149

1194-
//Saving the workbook as stream
1195-
FileStream stream = new FileStream("RangeParameter.xlsx", FileMode.Create, FileAccess.ReadWrite);
1196-
workbook.SaveAs(stream);
1197-
stream.Dispose();
1150+
//Saving the workbook
1151+
workbook.SaveAs("RangeParameter.xlsx");
11981152
}
11991153
{% endhighlight %}
12001154

0 commit comments

Comments
 (0)