NativeExcel2 for .NET Developer's Reference

Images

Example

This example demonstrates how to add picture into worksheet

Output file: console-images.xls

[C#]
using System;
using NativeExcel;

namespace Console_Images
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = "console-images.xls";
            CreateWorkbook(FileName);
            OpenWorkbookWithExcel(FileName);
        }

        static void CreateWorkbook(string FileName) {
            //Create workbook
            IWorkbook book = NativeExcel.Factory.CreateWorkbook();
            //Add sheet
            IWorksheet sheet = book.Worksheets.Add();
            //Change name of worksheet
            sheet.Name = "Sheet with image";
            //select upper-left corner ("B3") for image
            sheet.Cells[3,2].Select();
            //Add picture into sheet
            sheet.Shapes.AddPicture("image1.jpg");
            //Save workbook
            book.SaveAs(FileName);
        }

        static void OpenWorkbookWithExcel(string FileName){
            try {
                System.Diagnostics.Process.Start(FileName);
            } catch {
                Console.WriteLine(FileName + " created in application folder");
            }
        }
    }
}
[Visual Basic]
imports System
imports System.Drawing
imports NativeExcel

Module Console_Images

    Sub Main()
        Dim FileName As String = "console-images.xls"
        CreateWorkbook(FileName)
        OpenWorkbookWithExcel(FileName)
    End Sub

    Sub CreateWorkbook(FileName As String)
        'Create workbook
        Dim book As IWorkbook = NativeExcel.Factory.CreateWorkbook()
        'Add sheet
        Dim sheet As IWorksheet = book.Worksheets.Add()
        'Change name of worksheet
        sheet.Name = "Sheet with image"
        'select upper-left corner ("B3") for image
        sheet.Cells(3,2).Select()
        'Add picture into sheet
        sheet.Shapes.AddPicture("image1.jpg")
        'Save workbook
        book.SaveAs(FileName)
    End Sub

    Sub OpenWorkbookWithExcel(FileName As String)
        Try
           System.Diagnostics.Process.Start(FileName)
        Catch
           Console.WriteLine(FileName + " created in application folder")
        End Try  
    End Sub

End Module