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)