We have discussed before about converting workbook to PDF. However, in this section, we will show you a neat solution to convert the specific worksheet to PDF with C# and VB.NET in workbook. Apply Spire.Xls for .NET in your application and you can turn worksheet into PDF easily without changing the layout of worksheet.
In the following sections, we will demonstrate how to convert worksheet to PDF.
Step 1: Initialize a new instance of Workbook class and load the sample Excel file.
Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx");
Step 2: Get its first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Convert the selected worksheet to PDF and save to file.
sheet.SaveToPdf("toPDF.pdf");
Step 4: Launch the file.
System.Diagnostics.Process.Start("toPDF.pdf");
Effective screenshot:
Full codes:
[C#]
using Spire.Xls; namespace Excel_Worksheet_to_PDF { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; sheet.SaveToPdf("toPDF.pdf"); System.Diagnostics.Process.Start("toPDF.pdf"); } } }
[VB.NET]
Imports Spire.Xls Namespace Excel_Worksheet_to_PDF Class Program Private Shared Sub Main(args As String()) Dim workbook As New Workbook() workbook.LoadFromFile("Sample.xlsx") Dim sheet As Worksheet = workbook.Worksheets(0) sheet.SaveToPdf("toPDF.pdf") System.Diagnostics.Process.Start("toPDF.pdf") End Sub End Class End Namespace