Skip to content

Commit afab47b

Browse files
authored
Merge pull request #265 from SyncfusionExamples/990137-ExcelToHtmlSample
990137-Modify file streams of C# [Cross-platform] in XlsIO UG
2 parents 82c2fd5 + 001e433 commit afab47b

File tree

8 files changed

+24
-72
lines changed
  • Convert Excel to JSON
    • Range to JSON with Schema/.NET/Range to JSON with Schema/Range to JSON with Schema
    • Range to JSON without Schema/.NET/Range to JSON without Schema/Range to JSON without Schema
    • Workbook to JSON with Schema/.NET/Workbook to JSON with Schema/Workbook to JSON with Schema
    • Workbook to JSON without Schema/.NET/Workbook to JSON without Schema/Workbook to JSON without Schema
    • Worksheet to JSON with Schema/.NET/Worksheet to JSON with Schema/Worksheet to JSON with Schema
    • Worksheet to JSON without Schema/.NET/Worksheet to JSON without Schema/Worksheet to JSON without Schema
  • Excel to HTML/Excel to HTML/.NET/Excel to HTML/Excel to HTML
  • Excel to ODS/Excel to ODS/.NET/Excel to ODS/Excel to ODS

8 files changed

+24
-72
lines changed

Convert Excel to JSON/Range to JSON with Schema/.NET/Range to JSON with Schema/Range to JSON with Schema/Program.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,20 @@ static void Main(string[] args)
1212
{
1313
IApplication application = excelEngine.Excel;
1414
application.DefaultVersion = ExcelVersion.Xlsx;
15-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
16-
IWorkbook workbook = application.Workbooks.Open(inputStream);
15+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
1716
IWorksheet worksheet = workbook.Worksheets[0];
1817

1918
//Custom range
2019
IRange range = worksheet.Range["A1:F100"];
2120

2221
#region save as JSON
23-
//Saves the workbook to a JSON filestream, as schema by default
24-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Excel-Range-To-JSON-as-schema-default.json"), FileMode.Create, FileAccess.ReadWrite);
25-
workbook.SaveAsJson(outputStream, range);
22+
//Saves the workbook to JSON, as schema by default
23+
workbook.SaveAsJson(Path.GetFullPath("Output/Excel-Range-To-JSON-as-schema-default.json"), range);
2624

27-
//Saves the workbook to a JSON filestream as schema
28-
FileStream stream1 = new FileStream("Output/Excel-Range-To-JSON-as-schema.json", FileMode.Create, FileAccess.ReadWrite);
29-
workbook.SaveAsJson(stream1, range, true);
25+
//Saves the workbook to JSON as schema
26+
workbook.SaveAsJson(Path.GetFullPath("Output/Excel-Range-To-JSON-as-schema.json"), range, true);
3027
#endregion
3128

32-
//Dispose streams
33-
outputStream.Dispose();
34-
stream1.Dispose();
35-
inputStream.Dispose();
36-
3729
#region Open JSON
3830
//Open default JSON
3931

Convert Excel to JSON/Range to JSON without Schema/.NET/Range to JSON without Schema/Range to JSON without Schema/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,17 @@ static void Main(string[] args)
1212
{
1313
IApplication application = excelEngine.Excel;
1414
application.DefaultVersion = ExcelVersion.Xlsx;
15-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
16-
IWorkbook workbook = application.Workbooks.Open(inputStream);
15+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
1716
IWorksheet worksheet = workbook.Worksheets[0];
1817

1918
//Custom range
2019
IRange range = worksheet.Range["A1:F100"];
2120

2221
#region save as JSON
23-
//Saves the workbook to a JSON filestream, as schema by default
24-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Excel-Range-To-JSON-filestream-without-schema.json"), FileMode.Create, FileAccess.ReadWrite);
25-
workbook.SaveAsJson(outputStream, range, false);
22+
//Saves the workbook to JSON, as schema by default
23+
workbook.SaveAsJson(Path.GetFullPath("Output/Excel-Range-To-JSON-filestream-without-schema.json"), range, false);
2624
#endregion
2725

28-
//Dispose streams
29-
outputStream.Dispose();
30-
inputStream.Dispose();
31-
3226
#region Open JSON
3327
//Open default JSON
3428
#endregion

Convert Excel to JSON/Workbook to JSON with Schema/.NET/Workbook to JSON with Schema/Workbook to JSON with Schema/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@ static void Main(string[] args)
1212
{
1313
IApplication application = excelEngine.Excel;
1414
application.DefaultVersion = ExcelVersion.Xlsx;
15-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
16-
IWorkbook workbook = application.Workbooks.Open(inputStream);
15+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
1716
IWorksheet worksheet = workbook.Worksheets[0];
1817

19-
//Saves the workbook to a JSON filestream as schema
20-
FileStream jsonWithSchema = new FileStream(Path.GetFullPath("Output/Excel-Workbook-To-JSON-as-schema.json"), FileMode.Create, FileAccess.ReadWrite);
21-
workbook.SaveAsJson(jsonWithSchema, true);
22-
23-
//Dispose streams
24-
jsonWithSchema.Dispose();
25-
inputStream.Dispose();
18+
//Saves the workbook to JSON as schema
19+
workbook.SaveAsJson(Path.GetFullPath("Output/Excel-Workbook-To-JSON-as-schema.json"), true);
2620
}
2721
}
2822
}

Convert Excel to JSON/Workbook to JSON without Schema/.NET/Workbook to JSON without Schema/Workbook to JSON without Schema/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ static void Main(string[] args)
1212
{
1313
IApplication application = excelEngine.Excel;
1414
application.DefaultVersion = ExcelVersion.Xlsx;
15-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
16-
IWorkbook workbook = application.Workbooks.Open(inputStream);
15+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
1716
IWorksheet worksheet = workbook.Worksheets[0];
1817

1918
#region save as JSON
20-
//Saves the workbook to a JSON file without schema
21-
FileStream outputStream = new FileStream("Output/Workbook-To-JSON-without-schema.json", FileMode.Create);
22-
workbook.SaveAsJson(outputStream,false);
19+
//Saves the workbook to JSON file without schema
20+
workbook.SaveAsJson(Path.GetFullPath(@"Output/Workbook-To-JSON-without-schema.json"),false);
2321
#endregion
2422

25-
//Dispose streams
26-
outputStream.Dispose();
27-
inputStream.Dispose();
28-
2923
#region Open JSON
3024
//Open default JSON
3125
#endregion

Convert Excel to JSON/Worksheet to JSON with Schema/.NET/Worksheet to JSON with Schema/Worksheet to JSON with Schema/Program.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,17 @@ static void Main(string[] args)
1212
{
1313
IApplication application = excelEngine.Excel;
1414
application.DefaultVersion = ExcelVersion.Xlsx;
15-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
16-
IWorkbook workbook = application.Workbooks.Open(inputStream);
15+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
1716
IWorksheet worksheet = workbook.Worksheets[0];
1817

1918
#region save as JSON
20-
//Saves the workbook to a JSON filestream, as schema by default
21-
FileStream outputStream = new FileStream("Output/Excel-Worksheet-To-JSON-filestream-as-schema-default.json", FileMode.Create);
22-
workbook.SaveAsJson(outputStream, worksheet);
19+
//Saves the workbook to JSON, as schema by default
20+
workbook.SaveAsJson(Path.GetFullPath(@"Output/Excel-Worksheet-To-JSON-filestream-as-schema-default.json"), worksheet);
2321

24-
//Saves the workbook to a JSON filestream as schema
25-
FileStream stream1 = new FileStream("Output/Excel-Worksheet-To-JSON-filestream-as-schema.json", FileMode.Create, FileAccess.ReadWrite);
26-
workbook.SaveAsJson(stream1, worksheet, true);
22+
//Saves the workbook to JSON as schema
23+
workbook.SaveAsJson(Path.GetFullPath(@"Output/Excel-Worksheet-To-JSON-filestream-as-schema.json"), worksheet, true);
2724
#endregion
2825

29-
//Dispose streams
30-
outputStream.Dispose();
31-
stream1.Dispose();
32-
inputStream.Dispose();
33-
3426
#region Open JSON
3527
//Open default JSON
3628

Convert Excel to JSON/Worksheet to JSON without Schema/.NET/Worksheet to JSON without Schema/Worksheet to JSON without Schema/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ static void Main(string[] args)
1212
{
1313
IApplication application = excelEngine.Excel;
1414
application.DefaultVersion = ExcelVersion.Xlsx;
15-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
16-
IWorkbook workbook = application.Workbooks.Open(inputStream);
15+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
1716
IWorksheet worksheet = workbook.Worksheets[0];
1817

1918
#region save as JSON
20-
//Saves the workbook to a JSON filestream, as schema by default
21-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Excel-Worksheet-To-JSON-filestream-without-schema.json"), FileMode.Create, FileAccess.ReadWrite);
22-
workbook.SaveAsJson(outputStream, worksheet,false);
19+
//Saves the workbook to JSON, as schema by default
20+
workbook.SaveAsJson(Path.GetFullPath("Output/Excel-Worksheet-To-JSON-filestream-without-schema.json"), worksheet,false);
2321
#endregion
2422

25-
//Dispose streams
26-
outputStream.Dispose();
27-
inputStream.Dispose();
28-
2923
#region Open JSON
3024
//Open default JSON
3125
#endregion

Excel to HTML/Excel to HTML/.NET/Excel to HTML/Excel to HTML/Program.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ static void Main(string[] args)
2424

2525
#region Save as HTML
2626
//Saving the workbook
27-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.html"), FileMode.Create, FileAccess.Write);
28-
workbook.SaveAsHtml(outputStream, saveOptions);
27+
workbook.SaveAsHtml(Path.GetFullPath("Output/Output.html"), saveOptions);
2928
#endregion
30-
31-
//Dispose streams
32-
outputStream.Dispose();
3329
}
3430
}
3531
}

Excel to ODS/Excel to ODS/.NET/Excel to ODS/Excel to ODS/Program.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ static void Main(string[] args)
5050

5151
#region Save
5252
//Saving the workbook
53-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/ExcelToODS.ods"), FileMode.Create, FileAccess.Write);
54-
workbook.SaveAs(outputStream, ExcelSaveType.SaveAsODS);
53+
workbook.SaveAs("Output.ods");
5554
#endregion
56-
57-
//Dispose streams
58-
outputStream.Dispose();
5955
}
6056
}
6157
}

0 commit comments

Comments
 (0)