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.cs455 chars13 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