Eric Sjöström Jennerstrand

Litium E-commerce, C#, .NET MAUI, Javascript, React, Azure, Git. .NET, Node and more!
C Sharp

Create an Excel File With a Picture in C#

So how do you create an excel file with a picture in C#? It is a actually fairly simple. If you use the correct tools! By using Microsoft Office 16.0 Object Library you can add a image to your excel file with 1 simple line. And by using the Microsoft library to create your excel file, you can add rows and columns really easy aswell.

NOTE: This requires you to have Microsoft Office on your computer and on your production and build environments.

So the code below is all you need to create an excel with a picture in C#, in combination with adding the Com reference Microsoft Office 16.0 Object Library from the Reference Manager.
You can do this by right clicking your project in Visual Studio, select add and select Reference.

// Add the Com reference "Microsoft Office 16.0 Object Library".
// Right click your project => Add => Reference... => COM => Microsoft Office 16.0 Object Library
private void CreateExcelWithImage()
{
    // Set up.
    object missingValue = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Excel.Application excelApp = new Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(missingValue);
    Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.Worksheets.get_Item(1);

    // Add the picture on line 1-4.
    excelWorkSheet.Shapes.AddPicture("C:\\Users\\ESJ\\Pictures\\Test.PNG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 500, 60);

    // Example of adding text under the picture.
    excelWorkSheet.Cells[5, 1] = "http://www.Jennerstrand.se";
    excelWorkSheet.Cells[6, 1] = "Some Text for you";

    excelWorkBook.SaveAs("C:\\Users\\Jennerstrand\\Pictures\\excel-with-image.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.excelWorkBookDefault, missingValue, missingValue, missingValue, missingValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missingValue, missingValue, missingValue, missingValue, missingValue);
    excelWorkBook.Close(true, missingValue, missingValue);

    excelApp.Quit();

    ReleaseObject(excelApp);
    ReleaseObject(excelWorkBook);
    ReleaseObject(excelWorkSheet);
}

private static void ReleaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
    }
    finally
    {
        GC.Collect();
    }
}
Reference Manager - Microsoft Office 16.0 Object Library - Create an Excel File With a Picture in C#
Reference Manager – Microsoft Office 16.0 Object Library

So this guide about how to create and excel file with a picture in C# is mostly how to get you going, if you need any more help, please leave a comment.

And once again: This requires you to have Microsoft Office on your computer and on your production and build environmens.

Download your file

Do you want to download your file as well? Check out this article!
https://www.jennerstrand.se/download-a-file-in-csharp-from-a-cshtml-file/

Do you need a CSV file instead of an Excel file?

Check out this article, it’s super easy!
https://www.jennerstrand.se/write-lines-to-a-text-file-in-c-sharp/


Check out these simple easy to read articles about add object to List, reverse for loops and for loops if you want som extra reading:
https://www.jennerstrand.se/add-object-to-list-in-c-sharp.
https://www.jennerstrand.se/reverse-for-loop-in-c-sharp.
https://www.jennerstrand.se/for-loop-in-c-sharp-simple.

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>