Automating PowerPoint Creation from Excel Using C#

Automating the creation of PowerPoint presentations from Excel data can save a significant amount of time, especially when dealing with large datasets. Using C# in combination with the Microsoft Office Interop Libraries allows us to programmatically generate PowerPoint slides based on the content of an Excel file. In this guide, we’ll walk through the process of building a C# application that converts rows from an Excel spreadsheet into PowerPoint slides.

Requirements

  • Visual Studio (or another C# development environment)
  • Microsoft Office Interop Libraries for Excel and PowerPoint
  • An Excel spreadsheet containing the data

Setting Up the Project

  1. Create a New C# Console Application in Visual Studio.
  • Open Visual Studio and create a new Console App project.
  1. Add Microsoft Office Interop Libraries:
  • Right-click on the project in the Solution Explorer and select Manage NuGet Packages.
  • Search for and install the following packages:
    • Microsoft.Office.Interop.Excel
    • Microsoft.Office.Interop.PowerPoint

These libraries enable the application to communicate with Excel and PowerPoint.

C# Code to Automate PowerPoint Creation

Here’s the complete C# code to generate a PowerPoint presentation from an Excel spreadsheet:

using System;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;

class Program
{
    static void Main(string[] args)
    {
        // Step 1: Open Excel and load the workbook
        Excel.Application excelApp = new Excel.Application();
        string excelPath = @"C:\path\to\your\spreadsheet.xlsx";
        Excel.Workbook workbook = excelApp.Workbooks.Open(excelPath);
        Excel._Worksheet worksheet = (Excel._Worksheet)workbook.Sheets[1];
        Excel.Range range = worksheet.UsedRange;

        // Step 2: Create a new PowerPoint presentation
        PowerPoint.Application pptApp = new PowerPoint.Application();
        PowerPoint.Presentation presentation = pptApp.Presentations.Add(Office.MsoTriState.msoTrue);

        // Step 3: Loop through the rows of the Excel sheet
        for (int i = 1; i <= range.Rows.Count; i++)
        {
            // Extract the content of the first column to use as the slide title
            string slideTitle = Convert.ToString((range.Cells[i, 1] as Excel.Range).Value2);

            // Add a new slide to the PowerPoint presentation
            PowerPoint.Slide slide = presentation.Slides.Add(i, PowerPoint.PpSlideLayout.ppLayoutText);
            slide.Shapes[1].TextFrame.TextRange.Text = slideTitle;

            // Add content from other columns as the body of the slide
            for (int j = 2; j <= range.Columns.Count; j++)
            {
                string slideContent = Convert.ToString((range.Cells[i, j] as Excel.Range).Value2);
                slide.Shapes[2].TextFrame.TextRange.Text += slideContent + Environment.NewLine;
            }
        }

        // Step 4: Save the PowerPoint presentation
        string pptPath = @"C:\path\to\your\presentation.pptx";
        presentation.SaveAs(pptPath);
        presentation.Close();
        pptApp.Quit();

        // Step 5: Clean up Excel
        workbook.Close(false);
        excelApp.Quit();
        Marshal.ReleaseComObject(workbook);
        Marshal.ReleaseComObject(worksheet);
        Marshal.ReleaseComObject(excelApp);
    }
}

Code Breakdown

  • Excel Automation:
  • The code opens an Excel workbook and retrieves data from the first worksheet using Microsoft.Office.Interop.Excel. The UsedRange property captures all the data in the sheet.
  • PowerPoint Automation:
  • A new PowerPoint presentation is created using Microsoft.Office.Interop.PowerPoint. For each row in the Excel sheet, a new slide is added to the PowerPoint file. The data from the first column is used as the slide title, and the content from subsequent columns becomes the slide body text.
  • Iteration Through Rows:
  • The code loops through each row of the Excel sheet, generating a new slide for each. The first column’s value becomes the slide title, and the other columns’ data form the slide content.
  • Saving the Presentation:
  • After generating all the slides, the presentation is saved to a specified file path.
  • Resource Cleanup:
  • COM objects for Excel and PowerPoint are explicitly released to avoid memory leaks.

Running the Code

Once the code is complete:

  1. Run the application.
  2. It will generate a PowerPoint file at the designated location, with each row of the Excel sheet turned into a slide.

Conclusion

By leveraging C# with Microsoft’s Office Interop libraries, you can automate the creation of PowerPoint presentations directly from Excel data. This method is highly efficient for turning large datasets into professional slides, especially useful in corporate or educational settings.

Edvaldo Guimrães Filho Avatar

Published by

Categories:

Leave a comment