How to convert a CSV or Excel file into PowerPoint presentation?
You can convert a CSV or an Excel file into PowerPoint presentation by using Essential Presentation and Essential XlsIO libraries. The conversion process includes the below steps,
- Export the data from CSV or excel file to a System.Data.DataTable.
- Insert the data from DataTable to the tables in PowerPoint slides.
The below assemblies are needed to compile the below code examples.
- Syncfusion.Compression.Base.dll
- Syncfusion.Presentation.Base.dll
- Syncfusion.XlsIO.Base.dll
The Essential XlsIO library can be used to export data from Excel or CSV file to a System.Data.DataTable instance. The below code example demonstrates the same.
//Create excel engine to open excel application ExcelEngine excelEngine = new ExcelEngine(); //open the excel application. IApplication application = excelEngine.Excel; //Open existing workbook. IWorkbook workBook = application.Workbooks.Open("fileName", ExcelOpenType.Automatic); //Get the active sheet from workbook. IWorksheet sheet = workBook.ActiveSheet; //Export the values to a DataTable DataTable dataTable = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.None);
Since the content extended outside the slide will not be visible in Presentation Viewers, you have to restrict the number of data rows to be displayed in each slide.
The following code example demonstrates splitting a large DataTable instance into many small DataTable instances according to the specified size.
//Set the number of rows a slide will hold. int RowsPerSlide = 19; //Split the DataTable according to the table limitation in adding number of rows and columns in presentation. List<DataTable> dataTables = SplitDataTable(dataTable, RowsPerSlide);
/// <summary> /// Splits the data table according to the specified size /// </summary> /// <param name="tableToSplit">The DataTable to split</param> /// <param name="countLimit">The specified size to split the table</param> /// <returns>Returns the split tables list</returns> private static List<DataTable> SplitDataTable(DataTable tableToSplit, int countLimit) { List<DataTable> tables = new List<DataTable>(); int count = 0; DataTable copyTable = null; foreach (DataRow dr in tableToSplit.Rows) { if ((count++ % countLimit) == 0) { copyTable = new DataTable(); // Clone the structure of the table. copyTable = tableToSplit.Clone(); // Add the new DataTable to the list. tables.Add(copyTable); } // Import the current row. copyTable.ImportRow(dr); } return tables; }
You can insert the data from DataTable as table in the slides of a PowerPoint presentation. The below code snippets demonstrates the same.
// Create presentation by using Essential Presentation. IPresentation presentation = Presentation.Create(); //Export the CSV to Presentation foreach (DataTable splittedDataTable in dataTables) { ExportToPresentation(presentation, splittedDataTable); } //Save the presentation presentation.Save("Presentation.pptx"); //Open the presentation Process.Start("Presentation.pptx");
/// <summary> /// Export the CSV data to the PowerPoint presentation by exporting the data in CSV file to the table in PowerPoint presentation /// </summary> /// <param name="presentation">The PowerPoint presentation to import the data</param> /// <param name="dataTable">The table which holds the CSV data</param> private static void ExportToPresentation(IPresentation presentation, DataTable dataTable) { //Add slide to the presentation document. ISlide slide = presentation.Slides.Add(SlideLayoutType.Blank); //Add table to the slide. The height of the table will grow according to the content height by default ITable table = slide.Tables.AddTable(dataTable.Rows.Count, dataTable.Columns.Count, 0, 0, slide.SlideSize.Width, 10); //Disable the header row property table.HasHeaderRow = false; int rowIndex = 0; int colIndex = 0; //Add data to the table in presentation from the DataTable foreach (DataRow row in dataTable.Rows) { foreach (object val in row.ItemArray) { ICell cell = table[rowIndex, colIndex]; cell.TextBody.AddParagraph(val.ToString()); colIndex++; } colIndex = 0; rowIndex++; } }
You can find the complete sample here.
Conclusion
I hope you enjoyed learning about how to convert a CSV or Excel file into PowerPoint presentation in WinForms.
You can refer to our WinForms Presentation featuretour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!