原創|其它|編輯:郝浩|2012-01-05 20:52:38.000|閱讀 4062 次
概述:有時候,你需要將URL中的圖片插入到Excel文件中。事實上,要做到這一點并非難事。你只需要將圖像數據提取出來,下載到圖像數據流中,然后,你便可以利用Aspose.Cells APIs將圖像數據流中的圖像插入到工作表中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
有時候,你需要將URL中的圖片插入到Excel文件中。事實上,要做到這一點并非難事。你只需要將圖像數據提取出來,下載到圖像數據流中,然后,你便可以利用Aspose.Cells APIs將圖像數據流中的圖像插入到工作表中。具體實現方法如下:
[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 = "//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.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("d:\\test\\webimagebook.xls");
}
catch (Exception ex)
{
//Write the error message on the console
Console.WriteLine(ex.Message);
}
[VB.NET]
'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 = "//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.Drawing.PictureCollection = 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
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網