Create a new WinForms application and add the necessary assemblies to the project. Add a WorkbookView control to the main application window. Note, that this is not necessary for the conversion – it is done only to display the original XLSX file.
Perform the conversion
Add a button to the main form, set its text to ‘Convert…’ and handle its Click event. In the event handler display an OpenFileDialog to query the user for the input XLSX file, then display a SaveFileDialog to request the target PDF file. Once the two files are specified, proceed with the conversion by creating an ExcelImporter and PdfExporter objects and using their Import and Export methods in succession:
// Import the data var importer = new ExcelImporter(); importer.Import(xlsxPath, workbook1); // Export the worksheet as PDF var exporter = new PdfExporter(); exporter.EnableGridLines = true; exporter.Export(workbook1.Worksheets, pdfPath); // Open the PDF System.Diagnostics.Process.Start(pdfPath);
The xlsxPath and pdfPath variables identify the respective XLSX and PDF file names. The workbook1 variable represents the Workbook displayed on the form. Once the conversion is complete, the PDF file is opened in the default PDF viewer by calling Process.Start.
The following image illustrates the result:
The source code of the project together with all necessary libraries can be downloaded from here:
Click here here to visit the official page of the MindFusion WinForms Spreadsheet control.
We hope you find this tutorial useful and thank you for your interest in MindFusion developer tools.