Search code examples
c#excelspreadsheetlight

How can I insert a picture into an Excel spreadsheet with Spreadsheet Light from a link rather than an image file?


It's easy to add a picture to a sheet with Spreadsheet Light like this:

SLPicture logoPic = new SLPicture(@"C:\Platypus\DuckbillsUnlimited.png");
logoPic.SetPosition(0, 13);
sl.InsertPicture(logoPic);

...but I want to use an image at an URL, not from a file. How is this accomplished?


Solution

  • I tried using the image URL directly in the constructor of SLPicture, however that is not supported. You can use a workaround as follows:

    1. Download the image file to a temporary location.
    2. Use the downloaded file from the temporary location.

    Modification to your sample code can be shown as follows:

    WebClient client = new WebClient();
    client.DownloadFile(new Uri(url), @"C:\Platypus\DuckbillsUnlimited.png");
    
    SLPicture pic = new SLPicture(@"C:\Platypus\DuckbillsUnlimited.png");
    logoPic.SetPosition(0, 13);
    sl.InsertPicture(logoPic);
    

    Not sure if there are other approaches but this definitely works!! Open for other suggestions!