本文将介绍如何使用Spire.XLS for .NET转换Excel中的形状和SmartArt图形到图片。
原Excel文档:
C#
using Spire.Xls;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace Convert_Shapes_and_SmartArt_to_Image
{
class Program
{
static void Main(string[] args)
{
//创建Workbook对象
Workbook workbook = new Workbook();
//加载Excel文档
workbook.LoadFromFile("Sample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
//创建SaveShapeTypeOption对象
SaveShapeTypeOption shapelist = new SaveShapeTypeOption();
//保存工作表中的形状和SmartArt图形到图片
List images = sheet.SaveShapesToImage(shapelist);
//保存图片到指定路径
int index = 0;
foreach (Image img in images)
{
img.Save("Image/" + "toImage" + index + ".Png", ImageFormat.Png);
index++;
}
}
}
}
VB.NET
Imports Spire.Xls
Imports System.Collections.Generic
Imports System.Drawing.Imaging
Namespace Convert_Shapes_and_SmartArt_to_Image
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'创建Workbook对象
Dim workbook As Workbook = New Workbook()
'加载Excel文档
workbook.LoadFromFile("Sample.xlsx")
'获取第一个工作表
Dim sheet As Worksheet = workbook.Worksheets(0)
'创建SaveShapeTypeOption对象
Dim shapelist As SaveShapeTypeOption = New SaveShapeTypeOption()
'保存工作表中的形状和SmartArt图形到图片
Dim images As List(Of Bitmap) = sheet.SaveShapesToImage(shapelist)
'保存图片到指定路径
Dim index As Integer = 0
For Each img As Image In images
img.Save("Image/" & "toImage" & index & ".Png", ImageFormat.Png)
index += 1
Next
End Sub
End Class
End Namespace
转换结果: