Excel 中可插入表单控件,如文本框、单选按钮、复选框、组合框等等,插入后的控件可执行控件格式,如大小、是否锁定、位置、可选文字、数据源区域、单元格链接等操作。当 Excel 中已插入控件,需要读取时,可以参考本文中使用 Spire.XLS for .NET 来读取的方法。以下是详细方法和步骤。
安装 Spire.XLS for .NET
首先,您需要添加 Spire.XLS for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.XLS读取表单控件
以下是读取控件的方法步骤:
- 创建 Workbook 类的对象,并通过 Workbook.LoadFromFile(string fileName) 方法加载 Excel 工作簿。
- 通过 Workbook.Worksheets[int index] 属性获取指定工作表。
- 通过 Worksheet.TextBoxes[int index] 属性获取文本框,并通过 ITextBox.Text 属性获取文本框中的内容,最后通过 Console.WriteLine() 及 Console.ReadLine() 方法输出读取的文本框内容。
- for 循环遍历工作表中所有 RadioButtons,通过 Worksheet.RadioButtons[int index] 属性 RadioButtons,并通过 IRadioButton.CheckState 属性获取选择状态、IRadioButton.Text 属性获取文本、IRadioButton.IsLocked 属性获取 IRadioButton 控件是否被锁定。最后通过 Console.WriteLine() 及 Console.ReadLine() 方法输出读取内容。
- 通过 Worksheet.ComboBoxes[int index] 获取工作表中的 ComboBoxes,并通过 IComboBoxes.SelectedValue 属性获取当前控件选择的值。最后通过 Console.WriteLine() 及 Console.ReadLine() 方法输出读取的 ComboBoxes 内容。
- for 循环遍历工作表中所有 CheckBoxes,通过 Worksheet.CheckBoxes[int index] 属性 CheckBoxes,并通过 ICheckBox.CheckState 属性获取选择状态、ICheckBox.Text 属性获取文本、ICheckBox.AlternativeText 属性获取可选文本。最后通过 Console.WriteLine() 及 Console.ReadLine() 方法输出读取的 ICheckBox 内容。
- for 循环遍历工作表中所有 SpinnerShapes,通过 Worksheet.SpinnerShapes[int index] 属性 SpinnerShapes,并通过 ISpinnerShape.LinkedCell 属性获取链接的单元格、ISpinnerShape.CurrentValue 属性获取当前值。最后通过 Console.WriteLine() 及 Console.ReadLine() 方法输出读取的 ISpinnerShape 内容。
- C#
- VB.NET
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("sample.xlsx");
            //获取第1张工作表
            Worksheet sheet = wb.Worksheets[0];
            //获取TextBox
            ITextBox textbox = sheet.TextBoxes[0];          
            string textcontent = textbox.Text;
            Console.WriteLine("TextBox: "+ textcontent);
            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();               
            }
        }
    }
}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("sample.xlsx") '获取第1张工作表
			Dim sheet As Worksheet = wb.Worksheets(0)
			'获取TextBox Dim textbox As ITextBox = sheet.TextBoxes(0) Dim textcontent As String = textbox.Text Console.WriteLine(Convert.ToString("TextBox: ") & textcontent) 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

申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



