Carica un immagine web da un URL in un foglio di calcolo di Excel

Caricare un’immagine da un URL in un foglio di lavoro di Excel

API Aspose.Cells for .NET fornisce un modo semplice e facile per caricare le immagini dagli URL nei fogli di lavoro di Excel. Questo articolo spiega come scaricare i dati dell’immagine in uno stream e quindi inserirli nel foglio di lavoro utilizzando l’API di Aspose.Cells. Utilizzando questo metodo, le immagini diventano parte del file di Excel e non vengono scaricate ogni volta che il foglio di lavoro viene aperto.

Codice di esempio

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// 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:// Www.aspose.com/Images/aspose-logo.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.Drawing.PictureCollection pictures = sheet.Pictures;
// Insert the picture from the stream to B2 cell
pictures.Add(1, 1, objImage);
// Save the excel file
wb.Save(dataDir+ "webimagebook.out.xlsx");
}
catch (Exception ex)
{
// Write the error message on the console
Console.WriteLine(ex.Message);
}