为excel工作表添加图片时,我们常会重新调整图片的大小和位置来让图片和整个文档内容协调并美观。本文将介绍如何使用spire.xls 来设置excel 工作表中图片的大小和位置。
添加图片到新的excel文档中时,设置位置和大小:
c#
//加载一个workbook实例并获取worksheet
workbook workbook = new workbook();
worksheet sheet = workbook.worksheets[0];
//添加第一张图片
excelpicture picture = sheet.pictures.add(1, 1, "logo.png");
//设置图片大小
picture.width = 150;
picture.height = 150;
//设置图片位置
picture.left = 20;
picture.top = 100;
//添加第二张图片并设置大小和位置
excelpicture picture2 = sheet.pictures.add(4, 1, "xls.png");
picture2.width = 150;
picture2.height = 150;
picture2.left = 20;
picture2.top = 300;
//保存文档
workbook.savetofile("output.xlsx", fileformat.version2013);
vb.net
'加载一个workbook实例并获取worksheet
dim workbook as new workbook()
dim sheet as worksheet = workbook.worksheets(0)
'添加第一张图片
dim picture as excelpicture = sheet.pictures.add(1, 1, "logo.png")
'设置图片大小
picture.width = 150
picture.height = 150
'设置图片位置
picture.left = 20
picture.top = 100
'添加第二张图片并设置大小和位置
dim picture2 as excelpicture = sheet.pictures.add(4, 1, "xls.png")
picture2.width = 150
picture2.height = 150
picture2.left = 20
picture2.top = 300
'保存文档
workbook.savetofile("output.xlsx", fileformat.version2013)
调整excel文档中已有的图片位置和大小:
c#
//加载含有图片的excel文档
workbook workbook = new workbook();
workbook.loadfromfile("output.xlsx");
//获取第一个sheet
worksheet sheet = workbook.worksheets[0];
//获取第一张图片
excelpicture picture = sheet.pictures[0];
//设置图片大小
picture.width = 75;
picture.height = 75;
//设置图片位置
picture.left = 200;
picture.top = 300;
//保存文档
workbook.savetofile("resetexcelimage.xlsx", fileformat.version2013);
vb.net
'加载含有图片的excel文档
dim workbook as new workbook()
workbook.loadfromfile("output.xlsx")
'获取第一个sheet
dim sheet as worksheet = workbook.worksheets(0)
'获取第一张图片
dim picture as excelpicture = sheet.pictures(0)
'设置图片大小
picture.width = 75
picture.height = 75
'设置图片位置
picture.left = 200
picture.top = 300
'保存文档
workbook.savetofile("resetexcelimage.xlsx", fileformat.version2013)