Skip to content

Commit 0c59e58

Browse files
990137-ExcelToPdfSample
1 parent 158b475 commit 0c59e58

File tree

13 files changed

+44
-94
lines changed
  • Getting Started
    • ASP.NET Core/Convert Excel to PDF/Convert Excel to PDF/Controllers
    • ASP.NET MVC/Convert Excel to PDF/Convert-Excel-to-PDF/Controllers
    • ASP.NET WebForms/Convert Excel to PDF/Convert-Excel-to-PDF
    • Azure App Service/Convert-Excel-to-PDF/Convert-Excel-to-PDF/Controllers
    • Blazor/Server Side/Convert Excel to PDF/Convert-Excel-to-PDF/Data
    • Console
      • .NET Framework/ConvertExcelToPDF/ConvertExcelToPDF
      • .NET/ConvertExcelToPDF/ConvertExcelToPDF
    • Docker/Ubuntu/Convert Excel to PDF/Convert-Excel-to-PDF
    • Linux/Convert Excel to PDF/Convert Excel to PDF
    • Mac/Convert Excel to PDF/Convert Excel to PDF
    • UWP/Convert Excel to PDF/Convert-Excel-to-PDF
    • WPF/Convert Excel to PDF/Convert-Excel-to-PDF
    • Windows Forms/Convert Excel to PDF/Convert-Excel-to-PDF

13 files changed

+44
-94
lines changed

Getting Started/ASP.NET Core/Convert Excel to PDF/Convert Excel to PDF/Controllers/HomeController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public IActionResult ConvertExcelToPdf()
3030
application.DefaultVersion = ExcelVersion.Xlsx;
3131

3232
//load an existing file
33-
FileStream excelStream = new FileStream("Data/InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
34-
IWorkbook workbook = application.Workbooks.Open(excelStream);
33+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
3534

3635
//Initialize XlsIO renderer.
3736
XlsIORenderer renderer = new XlsIORenderer();

Getting Started/ASP.NET MVC/Convert Excel to PDF/Convert-Excel-to-PDF/Controllers/HomeController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public ActionResult ConvertExcelToPdf()
3333
application.DefaultVersion = ExcelVersion.Xlsx;
3434

3535
//Load an existing file
36-
FileStream excelStream = new FileStream(Server.MapPath("~/App_Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
37-
IWorkbook workbook = application.Workbooks.Open(excelStream);
36+
IWorkbook workbook = application.Workbooks.Open(Server.MapPath("~/App_Data/InputTemplate.xlsx"));
3837

3938
//Initialize ExcelToPdfConverter
4039
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);

Getting Started/ASP.NET WebForms/Convert Excel to PDF/Convert-Excel-to-PDF/MainPage.aspx.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ protected void OnButtonClicked(object sender, EventArgs e)
1818
application.DefaultVersion = ExcelVersion.Xlsx;
1919

2020
//Load an existing file
21-
FileStream excelStream = new FileStream(Server.MapPath("~/App_Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
22-
IWorkbook workbook = application.Workbooks.Open(excelStream);
21+
IWorkbook workbook = application.Workbooks.Open(Server.MapPath("~/App_Data/InputTemplate.xlsx"));
2322

2423
//Initialize ExcelToPdfConverter
2524
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);

Getting Started/Azure App Service/Convert-Excel-to-PDF/Convert-Excel-to-PDF/Controllers/HomeController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public IActionResult CreateDocument()
2626
{
2727
IApplication application = excelEngine.Excel;
2828
application.DefaultVersion = ExcelVersion.Xlsx;
29-
FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
30-
IWorkbook workbook = application.Workbooks.Open(excelStream);
29+
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
3130

3231
//Initialize XlsIO renderer.
3332
XlsIORenderer renderer = new XlsIORenderer();

Getting Started/Blazor/Server Side/Convert Excel to PDF/Convert-Excel-to-PDF/Data/ExcelService.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,24 @@ public MemoryStream ConvertExceltoPDF()
1414
{
1515
IApplication application = excelEngine.Excel;
1616

17-
using (FileStream sourceStreamPath = new FileStream(@"wwwroot/InputTemplate.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
18-
{
19-
// Open the workbook.
20-
IWorkbook workbook = application.Workbooks.Open(sourceStreamPath);
17+
// Open the workbook.
18+
IWorkbook workbook = application.Workbooks.Open(@"wwwroot/InputTemplate.xlsx");
2119

22-
// Instantiate the Excel to PDF renderer.
23-
XlsIORenderer renderer = new XlsIORenderer();
20+
// Instantiate the Excel to PDF renderer.
21+
XlsIORenderer renderer = new XlsIORenderer();
2422

25-
//Convert Excel document into PDF document
26-
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
23+
//Convert Excel document into PDF document
24+
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
2725

28-
//Create the MemoryStream to save the converted PDF.
29-
MemoryStream pdfStream = new MemoryStream();
26+
//Create the MemoryStream to save the converted PDF.
27+
MemoryStream pdfStream = new MemoryStream();
3028

31-
//Save the converted PDF document to MemoryStream.
32-
pdfDocument.Save(pdfStream);
33-
pdfStream.Position = 0;
29+
//Save the converted PDF document to MemoryStream.
30+
pdfDocument.Save(pdfStream);
31+
pdfStream.Position = 0;
3432

35-
return pdfStream;
36-
37-
}
33+
return pdfStream;
3834
}
39-
4035
}
4136
}
4237
}

Getting Started/Console/.NET Framework/ConvertExcelToPDF/ConvertExcelToPDF/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public static void Main(string[] args)
1313
{
1414
IApplication application = excelEngine.Excel;
1515
application.DefaultVersion = ExcelVersion.Xlsx;
16-
FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
17-
IWorkbook workbook = application.Workbooks.Open(excelStream);
16+
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
1817

1918
//Initialize ExcelToPdfConverter
2019
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
@@ -30,4 +29,4 @@ public static void Main(string[] args)
3029
}
3130
}
3231
}
33-
}
32+
}

Getting Started/Console/.NET/ConvertExcelToPDF/ConvertExcelToPDF/Program.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@ public static void Main(string[] args)
1515
application.DefaultVersion = ExcelVersion.Xlsx;
1616

1717
//Load existing Excel file
18-
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Sample.xlsx"), FileMode.Open, FileAccess.Read);
19-
IWorkbook workbook = application.Workbooks.Open(inputStream);
18+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Sample.xlsx"));
2019

2120
//Convert to PDF
2221
XlsIORenderer renderer = new XlsIORenderer();
2322
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
2423

25-
//Create the MemoryStream to save the converted PDF
26-
MemoryStream pdfStream = new MemoryStream();
27-
2824
#region Save
2925
//Saving the workbook
30-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.pdf"), FileMode.Create, FileAccess.Write);
31-
pdfDocument.Save(outputStream);
26+
pdfDocument.Save(Path.GetFullPath("Output/Sample.pdf"));
3227
#endregion
33-
34-
//Dispose streams
35-
outputStream.Dispose();
36-
inputStream.Dispose();
3728
}
3829
}
3930
}

Getting Started/Docker/Ubuntu/Convert Excel to PDF/Convert-Excel-to-PDF/Program.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@ static void Main(string[] args)
1515
{
1616
IApplication application = excelEngine.Excel;
1717
application.DefaultVersion = ExcelVersion.Xlsx;
18-
FileStream excelStream = new FileStream("Data/InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
19-
IWorkbook workbook = application.Workbooks.Open(excelStream);
18+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
2019

2120
//Initialize XlsIO renderer.
2221
XlsIORenderer renderer = new XlsIORenderer();
2322

2423
//Convert Excel document into PDF document
2524
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
2625

27-
//Create the FileStream to save the converted PDF.
28-
FileStream pdfStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite);
29-
pdfDocument.Save(pdfStream);
26+
//Save the converted PDF.
27+
pdfDocument.Save("Output.pdf");
3028
}
3129
}
3230
}
33-
}
34-
35-
36-
37-
31+
}

Getting Started/Linux/Convert Excel to PDF/Convert Excel to PDF/Program.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,16 @@ static void Main(string[] args)
1414
application.DefaultVersion = ExcelVersion.Xlsx;
1515

1616
//Load existing Excel file
17-
FileStream inputStream = new FileStream("Data/Sample.xlsx", FileMode.Open, FileAccess.Read);
18-
IWorkbook workbook = application.Workbooks.Open(inputStream);
17+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Sample.xlsx"));
1918

2019
//Convert to PDF
2120
XlsIORenderer renderer = new XlsIORenderer();
2221
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
2322

24-
//Create the MemoryStream to save the converted PDF.
25-
MemoryStream pdfStream = new MemoryStream();
26-
2723
#region Save
2824
//Saving the workbook
29-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.pdf"), FileMode.Create, FileAccess.Write);
30-
pdfDocument.Save(outputStream);
25+
pdfDocument.Save(Path.GetFullPath("Output/Sample.pdf"));
3126
#endregion
32-
33-
//Dispose streams
34-
outputStream.Dispose();
35-
inputStream.Dispose();
36-
3727
}
3828
}
3929
}

Getting Started/Mac/Convert Excel to PDF/Convert Excel to PDF/Program.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,16 @@ static void Main(string[] args)
1414
application.DefaultVersion = ExcelVersion.Xlsx;
1515

1616
//Load existing Excel file
17-
FileStream inputStream = new FileStream("Data/Sample.xlsx", FileMode.Open, FileAccess.Read);
18-
IWorkbook workbook = application.Workbooks.Open(inputStream);
17+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Sample.xlsx"));
1918

2019
//Convert to PDF
2120
XlsIORenderer renderer = new XlsIORenderer();
2221
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
2322

24-
//Create the MemoryStream to save the converted PDF.
25-
MemoryStream pdfStream = new MemoryStream();
26-
2723
#region Save
2824
//Saving the workbook
29-
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.pdf"), FileMode.Create, FileAccess.Write);
30-
pdfDocument.Save(outputStream);
25+
pdfDocument.Save(Path.GetFullPath("Output/Sample.pdf"));
3126
#endregion
32-
33-
//Dispose streams
34-
outputStream.Dispose();
35-
inputStream.Dispose();
36-
3727
}
3828
}
3929
}

0 commit comments

Comments
 (0)