将网络图片从URL加载到Excel工作表
Contents
[
Hide
]
从URL加载图像到Excel工作表
Aspose.Cells for .NET API提供了一种简单易行的方法,可以从URL中加载图像到Excel工作表中。本文介绍了使用Aspose.Cells API将图像数据下载到流中,然后将其插入工作表的方法。使用这种方法,图像将成为Excel文件的一部分,并且每次打开工作表时都不会重新下载。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
可能存在情况,您总是希望从URL获取更新后的图像。要实现这一点,可以按照从Web地址插入链接图片文章中给出的说明。通过遵循这种方法,图像在每次打开工作表时都从URL加载。