spire.xls支持丰富的图片处理功能,如添加、旋转、设置超链接、提取和删除等。本文将从中选取3种常用的添加、提取和删除功能进行介绍。
添加图片
c#
//创建excel文档
workbook workbook = new workbook();
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//添加文本并设置字体格式
sheet.range["a1"].text = "this excel document is created by spire.xls for .net";
excelfont font = sheet.range["a1"].style.font;
font.fontname = "comic sans ms";
font.size = 15;
font.color = color.darkolivegreen;
font.isbold = true;
font.isitalic = true;
sheet.rows[0].rowheight = 21.75;
//添加图片
sheet.pictures.add(3, 1, @"c:\users\administrator\desktop\sunflower.jpg");
//保存文档
workbook.savetofile("添加图片.xlsx", excelversion.version2013);
vb.net
'创建excel文档
dim workbook as new workbook()
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'添加文本并设置字体格式
sheet.range("a1").text = "this excel document is created by spire.xls for .net"
dim font as excelfont = sheet.range("a1").style.font
font.fontname = "comic sans ms"
font.size = 15
font.color = color.darkolivegreen
font.isbold = true
font.isitalic = true
sheet.rows(0).rowheight = 21.75
'添加图片
sheet.pictures.add(3, 1, "c:\users\administrator\desktop\sunflower.jpg")
'保存文档
workbook.savetofile("添加图片.xlsx", excelversion.version2013)
提取图片
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile(@"添加图片.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//获取工作表中的第一张图片
excelpicture picture = sheet.pictures[0];
//将图片保存到指定路径
picture.picture.save(@"image\image.png", imageformat.png);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("添加图片.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取工作表中的第一张图片
dim picture as excelpicture = sheet.pictures(0)
'将图片保存到指定路径
picture.picture.save("image\image.png", imageformat.png)
删除图片
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile(@"添加图片.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//获取工作表中的第一张图片
excelpicture picture = sheet.pictures[0];
//删除图片
picture.remove();
//保存文档
workbook.savetofile("删除图片.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("添加图片.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取工作表中的第一张图片
dim picture as excelpicture = sheet.pictures(0)
'删除图片
picture.remove()
'保存文档
workbook.savetofile("删除图片.xlsx", excelversion.version2013)