A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Thanks Jiachen Li. This code is very helpful but it uses Office.Interop.Excel. I need to run my script on a server machine and I have been told that Excel might not be available on that server. Office.Interop.Excel requires that Excel is installed on the machine. I searched for free packages and the following two packages are working for me now: EPPlus FreeSpire.XLS Here is my code that I tested and it worked:
Imports System.IO
Imports OfficeOpenXml
Imports Spire.Xls
Sub Main()
Dim excelFilePath As String = "D:\testing2\SampleExcelFile.xlsx"
' Specify the path where you want to save the output PDF file
Dim pdfFilePath As String = "D:\testing2\SamplePdfFile.pdf"
' Create a new Excel package with EPPlus
Using package As New ExcelPackage()
' Add a new worksheet to the workbook
Dim worksheet = package.Workbook.Worksheets.Add("Sheet1")
' Write sample values to the worksheet
worksheet.Cells("A1").Value = "Name"
worksheet.Cells("B1").Value = "Age"
worksheet.Cells("A2").Value = "John"
worksheet.Cells("B2").Value = 25
worksheet.Cells("A3").Value = "Jane"
worksheet.Cells("B3").Value = 100
' Save the Excel package to a file
package.SaveAs(New FileInfo(excelFilePath))
End Using
Dim Workbook As New Spire.Xls.Workbook()
Workbook.LoadFromFile(excelFilePath)
Workbook.SaveToFile(pdfFilePath, Spire.Xls.FileFormat.PDF)
End Sub