Sometimes, you do require inserting a picture from a URL into an Excel file. You may do it quite easily. You just need to extract & download image data into stream and then you may use Aspose.Cells APIs to insert image (from the stream) into the worksheet. Following is a simple way to achieve this:
[C#]
//Define memory stream object
System.IO.MemoryStream objImage;
//Define web client object
System.Net.WebClient objwebClient;
//Define a string which will hold the web image url
string sURL = "http://files.myopera.com/Mickeyjoe_irl/albums/38458/abc.jpg";
try
{
//Instantiate the web client object
objwebClient = new System.Net.WebClient();
//Now, extract data into memory stream downloading the image data into the array of bytes
objImage = new System.IO.MemoryStream(objwebClient.DownloadData(sURL));
//Create a new workbook
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
//Get the first worksheet in the book
Aspose.Cells.Worksheet sheet = wb.Worksheets[0];
//Get the first worksheet pictures collection
Aspose.Cells.Pictures pictures = sheet.Pictures;
//Insert the picture from the stream to B2 cell
pictures.Add(1,1,objImage);
//Save the excel file
wb.Save("d:\\test\\webimagebook.xls");
}
catch (Exception ex )
{
//Write the error message on the console
Console.WriteLine(ex.Message);
}
[VB]
'Define memory stream object
Dim objImage As System.IO.MemoryStream
'Define web client object
Dim objwebClient As System.Net.WebClient
'Define a string which will hold the web image url
Dim sURL As String = "http://files.myopera.com/Mickeyjoe_irl/albums/38458/abc.jpg"
Try
'Instantiate the web client object
objwebClient = New System.Net.WebClient
'Now, extract data into memory stream downloading the image data into the array of bytes
objImage = New System.IO.MemoryStream(objwebClient.DownloadData(sURL))
'Create a new workbook
Dim wb As Aspose.Cells.Workbook = New Aspose.Cells.Workbook
'Get the first worksheet in the book
Dim sheet As Aspose.Cells.Worksheet = wb.Worksheets(0)
'Get the first worksheet pictures collection
Dim pictures As Aspose.Cells.Pictures = sheet.Pictures
'Insert the picture from the stream to B2 cell
pictures.Add(1, 1, objImage)
'Save the excel file
wb.Save("d:\test\webimagebook.xls")
Catch ex As Exception
'Write the error message on the console
Console.WriteLine(ex.Message)
End Try