Spire.XLS 支持在Excel中插入文本框,并在文本框中添加文本及图片,对内容进行格式化操作等。同理,对于Excel工作表中已有的文本框内容,也可以通过Spire.XLS读取出来,包括读取文本、读取图片。
测试文档如下:
C#
//创建Workbook类的对象,并加载测试文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");
//获取指定工作表
Worksheet sheet = workbook.Worksheets["Sheet1"];
//遍历工作表中文本框
for( int i = sheet.TextBoxes.Count-1; i >= 0; i--)
{
XlsTextBoxShape shape = sheet.TextBoxes[i] as XlsTextBoxShape;
//提取文本框中的文本
string s = shape.Text;
StringBuilder sb = new StringBuilder();
sb.AppendLine(s);
File.WriteAllText("提取文本.txt", sb.ToString());
//提取文本框中的图片
Image image = shape.Fill.Picture;
image.Save("提取图片.png", ImageFormat.Png);
}
VB.NET
'创建Workbook类的对象,并加载测试文档
Dim workbook As New Workbook()
workbook.LoadFromFile("sample.xlsx")
'获取指定工作表
Dim sheet As Worksheet = workbook.Worksheets("Sheet1")
'遍历工作表中文本框
For i As Integer = sheet.TextBoxes.Count - 1 To 0 Step -1
Dim shape As XlsTextBoxShape = TryCast(sheet.TextBoxes(i), XlsTextBoxShape)
'提取文本框中的文本
Dim s As String = shape.Text
Dim sb As New StringBuilder()
sb.AppendLine(s)
File.WriteAllText("提取文本.txt", sb.ToString())
'提取文本框中的图片
Dim image As Image = shape.Fill.Picture
image.Save("提取图片.png", ImageFormat.Png)
Next
读取结果: