Excel中可通过【开发工具】菜单栏下插入表单控件,如文本框、单选按钮、复选框、组合框等等,插入后的控件可执行设置控件格式,如大小、是否锁定、位置、可选文字、数据源区域、单元格链接等。当Excel中已插入上述控件,需要读取时,也可以使用本文中的方法来读取。下面,将通过C#及VB.NET代码示例展示如何来获取Excel文档中的表单控件。以下是读取的方法及步骤,供参考。


【引用dll】

方法1:在程序中手动引入Spire.Xls.dll文件;将Free Spire.XLS for .NET下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.Xls.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。

方法2:通过​​NuGet​​安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.XLS”,点击“安装”。等待程序安装完成。

(2)将以下内容复制到PM控制台安装。

Install-Package FreeSpire.XLS -Version 12.2.0


【代码示例】

C#

using System;
using Spire.Xls;
using Spire.Xls.Core;

namespace GetFormControl
{
class Program
{
static void Main(string[] args)
{
//创建Workbook类的实例,加载Excel文档
Workbook wb = new Workbook();
wb.LoadFromFile("test.xlsx");

//获取第1张工作表
Worksheet sheet = wb.Worksheets[0];

//获取TextBox
string textbox = sheet.TextBoxes[0].Text;
Console.WriteLine("TextBox: "+ textbox);
Console.ReadLine();

//获取Radio Button
for (int i = 0; i < sheet.RadioButtons.Count; i++)
{
IRadioButton radioButton = sheet.RadioButtons[i];
string name = radioButton.CheckState.ToString();
string text = radioButton.Text;
bool islocked = radioButton.IsLocked;
Console.WriteLine("RadioButtons: " + name + text + " 是否锁定:" + islocked.ToString());
Console.ReadLine();
}

//获取Combo Box控件中的选中的值(注:非列表中所有选项值)
string value = sheet.ComboBoxes[0].SelectedValue;
Console.WriteLine("ComboBoxes: " + value);
Console.ReadLine();

//获取Checkbox
for (int z = 0; z < sheet.CheckBoxes.Count; z++)
{
ICheckBox checkBox = sheet.CheckBoxes[z];
string text = checkBox.Text;
string name = checkBox.CheckState.ToString();
string alternativetext = checkBox.AlternativeText;
Console.WriteLine("CheckBoxes: " + text + name + alternativetext);
Console.ReadLine();
}

//获取SpinnerShape
for (int j = 0; j < sheet.SpinnerShapes.Count; j++)
{
ISpinnerShape spinnerShape = sheet.SpinnerShapes[j];
string rangeAddress = spinnerShape.LinkedCell.RangeAddress;
int currentValue = spinnerShape.CurrentValue;
Console.WriteLine("SpinnerShapes: RangeAddress is " + rangeAddress + "\n"+"CurrentValue:" + currentValue);
Console.ReadLine();
}

}
}
}

vb.net

Imports Spire.Xls
Imports Spire.Xls.Core

Namespace GetFormControl
Class Program
Private Shared Sub Main(args As String())
'创建Workbook类的实例,加载Excel文档
Dim wb As New Workbook()
wb.LoadFromFile("AddControls.xlsx")

'获取第1张工作表
Dim sheet As Worksheet = wb.Worksheets(0)

'获取TextBox
Dim textbox As String = sheet.TextBoxes(0).Text
Console.WriteLine(Convert.ToString("TextBox: ") & textbox)
Console.ReadLine()

'获取Radio Button
For i As Integer = 0 To sheet.RadioButtons.Count - 1
Dim radioButton As IRadioButton = sheet.RadioButtons(i)
Dim name As String = radioButton.CheckState.ToString()
Dim text As String = radioButton.Text
Dim islocked As Boolean = radioButton.IsLocked
Console.WriteLine((Convert.ToString(Convert.ToString("RadioButtons: ") & name) & text) + " 是否锁定:" + islocked.ToString())
Console.ReadLine()
Next

'获取Combo Box控件中的选中的值(注:非列表中所有选项值)
Dim value As String = sheet.ComboBoxes(0).SelectedValue
Console.WriteLine(Convert.ToString("ComboBoxes: ") & value)
Console.ReadLine()

'获取Checkbox
For z As Integer = 0 To sheet.CheckBoxes.Count - 1
Dim checkBox As ICheckBox = sheet.CheckBoxes(z)
Dim text As String = checkBox.Text
Dim name As String = checkBox.CheckState.ToString()
Dim alternativetext As String = checkBox.AlternativeText
Console.WriteLine(Convert.ToString(Convert.ToString(Convert.ToString("CheckBoxes: ") & text) & name) & alternativetext)
Console.ReadLine()
Next

'获取SpinnerShape
For j As Integer = 0 To sheet.SpinnerShapes.Count - 1
Dim spinnerShape As ISpinnerShape = sheet.SpinnerShapes(j)
Dim rangeAddress As String = spinnerShape.LinkedCell.RangeAddress
Dim currentValue As Integer = spinnerShape.CurrentValue
Console.WriteLine((Convert.ToString("SpinnerShapes: RangeAddress is ") & rangeAddress) + vbLf + "CurrentValue:" + currentValue)
Console.ReadLine()
Next

End Sub
End Class
End Namespace

控件读取效果:

C#/VB.NET 获取Excel中的表单控件_控件

C#/VB.NET 获取Excel中的表单控件_C#_02


—END—