We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert a CSV or Excel file into PowerPoint presentation

Platform: ASP.NET Web Forms |
Control: 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,

 

  1. Export the data from CSV or excel file to a System.Data.DataTable.
  2. Insert the data from DataTable to the tables in PowerPoint slides.

 

The below assemblies are needed to compile the below code examples.

 

  1. Syncfusion.Compression.Base.dll
  2. Syncfusion.Presentation.Base.dll
  3. 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.

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile