printing an xlsx to pdf in csharp

To print an xlsx file to a pdf using C#, you first need to install the Microsoft.Office.Interop.Excel package. You can do this in Visual Studio under Tools > NuGet Package Manager > Manage NuGet Packages for Solution.... Look for Microsoft.Office.Interop.Excel and click Install.

Once installed, you can use the following code to print the xlsx file to pdf:

main.cs
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;

public void ConvertToPDF(string inputFile, string outputFolder)
{
    Application excelApplication = new Application();
    Workbook excelWorkbook = excelApplication.Workbooks.Open(inputFile);
    excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFolder);
    excelWorkbook.Close();
    excelApplication.Quit();
}
455 chars
13 lines

In this code, inputFile represents the path of the xlsx file you want to print to a pdf, and outputFolder is the folder where you want the pdf file to be saved.

This code opens the xlsx file in Excel, exports it as a pdf using the ExportAsFixedFormat method with the XlFixedFormatType.xlTypePDF parameter, and then saves the pdf file to the specified output folder. Finally, it closes the workbook and quits Excel.

gistlibby LogSnag