彩票走势图

使用Aspose.Cells将web图像插入Excel 工作表中

原创|其它|编辑:郝浩|2012-01-05 20:52:38.000|阅读 4009 次

概述:有时候,你需要将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@pclwef.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP